int cannot be dereferenced in Java
id
is of primitive type int
and not an Object
. You cannot call methods on a primitive as you are doing here :
id.equals
Try replacing this:
if (id.equals(list[pos].getItemNumber())){ //Getting error on "equals"
with
if (id == list[pos].getItemNumber()){ //Getting error on "equals"
Int cannot be dereferenced Java
Integer.parseInt(left) - Integer.parseInt(right)
is an int
. An int
is a primitive, so it has no methods, therefore you can't call toString()
on an int
value.
You can use the static
Integer.toString()
method instead.
For example:
return Integer.toString(Integer.parseInt(left) - Integer.parseInt(right));
int cannot be dereferenced
The g
in drawString
is the color value you've passed in, not your Graphics
reference. So the error is when you're trying to call a method on an int
, which you can't do.
// Passing an integer 'g' into the function here |
// V
public void drawString(String str, int x, int y, int r, int g, int b){
// | This 'g' is the integer you passed in
// V
g.setColor(r, g, b);
g.drawString(str, x, y, 0);
}
Why does this error state int cannot be dereferenced for the if statement?
You have a problem here :
if (password.length() != 7){
int
doesn't have length()
method, not like String, if you want to check the length of the password field then you can use :
if (passwordTextField.getText().length() != 7) {
Or if you are using the password as an int, and you need to check the length of the int then use :
if (String.valueOf(password).length() != 7) {
How to solve the problem: int cannot be dereferenced
For the hashcode
method, you can just leave the ints as they are. Ints are their own hascodes. For the equals
method, just compare them using =
. So the code becomes:
public class Connection {
String srcAddr, dstAddr, protocol; int srcPort, dstPort;
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((dstAddr == null) ? 0 : dstAddr.hashCode());
result = prime * result + dstPort;
result = prime * result
+ ((protocol == null) ? 0 : protocol.hashCode());
result = prime * result + ((srcAddr == null) ? 0 : srcAddr.hashCode());
result = prime * result + srcPort;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Connection other = (Connection) obj;
if (dstAddr == null) {
if (other.dstAddr != null)
return false;
} else if (!dstAddr.equals(other.dstAddr))
return false;
if (dstPort != other.dstPort)
return false;
if (protocol == null) {
if (other.protocol != null)
return false;
} else if (!protocol.equals(other.protocol))
return false;
if (srcAddr == null) {
if (other.srcAddr != null)
return false;
} else if (!srcAddr.equals(other.srcAddr))
return false;
if (srcPort != other.srcPort)
return false;
return true;
}
}
Writing correct implementations of hashCode
and equals
is tricky. Better use your IDE to generate them. That's what I did here too.
Java Error: int cannot be dereferenced
x is an int
, a primitive, and therefore cannot be dereferenced - meaning x.anything is invalid syntax in Java.
I'm assuming what you meant to do is assign some user input to x :
Scanner sc = new Scanner(System.in);
int x = sc.nextInt();
int cannot be dereferenced while sorting list by Integer property
int cannot be dereferenced
suggest that getR()
returns int
not Integer
(despite what your title suggests). int
is primitive type so it doesn't have any methods like compareTo
. Easiest way to compare int values is by Integer.compare(a, b)
(by default it returns result according to ascending order, if you want to get result for descending order Integer.compare(b, a)
).
So change
return lhs.getR().compareTo(rhs.getR());
to
return Integer.compare(lhs.getR(), rhs.getR());
BTW since Java 8 we can use Lambdas and method references. Also lists now have sort(Comparator)
method which allows us to write code like:
data.sort(Comparator.comparingInt(Segment::getR));
instead of
Collections.sort(data, new Comparator<Segment>() {
@Override
public int compare(Segment lhs, Segment rhs) {
return Integer.compare(lhs.getR(), rhs.getR());
}
});
Related Topics
Bidirectional Multi-Valued Map in Java
Should Private Helper Methods Be Static If They Can Be Static
Format File Size as Mb, Gb, etc
Why F Is Placed After Float Values
Jschexception: Algorithm Negotiation Fail
How to Initialize Hashset Values by Construction
Dynamically Changing Log4J Log Level
How to Load a Large Xlsx File with Apache Poi
Using Jasperreports with a Relative Path
Functional Style of Java 8's Optional.Ifpresent and If-Not-Present
Differencebetween Closeablehttpclient and Httpclient in Apache Httpclient API
How to Get a List of Ip Connected in Same Network (Subnet) Using Java
How to Convert String to Date Without Knowing the Format
How to Connect to Postgres Db Due to the Authentication Type 10 Is Not Supported
Difference Between Arrays.Aslist(Array) and New Arraylist<Integer>(Arrays.Aslist(Array))