Solve Error “int cannot be dereferenced” in Java

Dereferencing means accessing an object from the heap using a reference variable Or we can say It is a process of accessing the referred value by a reference.

Dereferencing’s main purpose is to place the memory address (where the actual object presents) into the reference.

Example:

If the reference has the null value, at that time the result of dereferencing will be a “Null Pointer Exception.”

Example:

Reference: A reference is an address of a variable and it is an easy, compact scalar value that’s why it does not contain data directly. It is also used for improve the performance when we are using large types of data structures and it saves memory because instead of passing a single value it passes a reference to a subroutine or method.

Deference: Deference returns a actual value when a reference is dereferenced.

Reference Variables: In java There are two types of variables, primitive types and object types. So the object type of variables called reference variables. Int is a primitive data type, it is not an object or reference. Because int is already a value and not a reference that’s why it can not be dereferenced.

Example:

Output:

Abc.java : 5 : error int cannot be dereferenced
System.out.println(x.length);
1 error

How to Fix “int cannot be dereferenced” Error?

In Java programming, there are two different types of variables: primitive data type and object type. The primitive data is different from objects because they do not behave as objects. The primitive data types variable considered as local variables or as a field. it allocates on the stack. And objects always allocates on the heap area. If you are declaring a local variable as an object then it will be allocated in the heap area. The stack only contains the references.

We use reference to store the address to variable or object. If we want to get or set the value for that variable that we have to need de-referenced that it means we need to get memory location where it is actually placed in the memory. “So, we can say that using dot(.) operator, accessing the state or behavior of an object is called dereferencing”.

Here we are taking some possible examples where this error “int cannot be dereferenced” can occur.

Example: 

In this example, we are trying to call a method but id is a primitive type. So we cannot call the methods on a primitive type. That’s why it will through an error that int cannot be dereferenced. Instead of the equals method, we can take “==” operators.

In this example, we are trying to do the same thing as above. So here, there are several ways to fix this error. If we want to convert int into a String then we could concatenate it with an empty string, or we could use the Integer.toString() method if we want to convert it explicitly.

Solution:

Or

Example: 

Here, we want to get the length of the value which contains by the “id” variable, So, getting the length of value here we are using length() method but we can not use the method with primitive type so we have to convert it first into the String and after that, we can apply this method on it. To convert it into the String we should follow the same ways as the above example.

Solution :

Or

Example:

In this example, we have done some mistakes that’s why it will through error that int cannot be dereferenced. So, the first error is I have written “; “ after for loop that is wrong because for loop has a body so we have to open this using braces “ { “ and in the last, we have to close this using “ } “. And the second mistake is we have not declared ar as an Array but we are storing the value in it as Array so we have to declare it otherwise it will through the same error as before.

Solution:

So I hope, after reading this article you’ve got the idea that why “Int cannot be dereferenced” error occurs and how to solve it.

If you’re still struggling with this error then please let us know in the comment box, we would love to help.

Leave a Comment

Your email address will not be published. Required fields are marked *