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:
|
Object ob = new Object(); String st = ob.toString(); // ‘ob’ is dereferenced. |
If the reference has the null value, at that time the result of dereferencing will be a “Null Pointer Exception.”
Example:
|
Object ob =null; Ob.toString(); // when this statement is executed It will throw a NullpointerException. |
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:
|
public class Abc { public static void main ( String[] args) { int a=1; System.out.println(a.length); } } |
Output:
Abc.java : 5 : error int cannot be dereferenced
System.out.println(x.length);
1 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.
|
int id = 1; id.toString(); |
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:
|
int id = 1; String i = id + “ ”; |
Or
|
String i = Integer.toString(id); |
Example:
|
int id = 111; System.out.println(id.lenght()); |
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 :
|
int id = 111; String i = id + «»; |
Or
|
String i = Integer.toString(id); System.out.println (i.lenght()); |
Example:
|
public int example() { int sum = 0; for ( int i = 0; i < ar.length; i++); sum = sum + ar[i]; } |
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:
|
int sum = 0; int[] ar = new int[5]; for ( int i = 0; i < ar.length; i++) { sum = sum + ar[i]; } |
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.
Here, we will follow the below-mentioned points to understand and eradicate the error alongside checking the outputs with minor tweaks in our sample code
- Introduction about the error with example
- Explanation of dereferencing in detail
- Finally, how to fix the issue with Example code and output.
If You Got this error while you’re compiling your code? Then by the end of this article, you will get complete knowledge about the error and able to solve your issue, lets start with an example.
Example 1:
Java
import java.io.*;
import java.util.*;
class GFG {
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
int sum = a + b;
System.out.println(sum.length);
}
}
Output:
So this is the error that occurs when we try to dereference a primitive. Wait hold on what is dereference now?. Let us do talk about that in detail. In Java there are two different variables are there:
- Primitive [byte, char, short, int, long, float, double, boolean]
- Objects
Since primitives are not objects so they actually do not have any member variables/ methods. So one cannot do Primitive.something(). As we can see in the example mentioned above is an integer(int), which is a primitive type, and hence it cannot be dereferenced. This means sum.something() is an INVALID Syntax in Java.
Explanation of Java Dereference and Reference:
- Reference: A reference is an address of a variable and which doesn’t hold the direct value hence it is compact. It is used to improve the performance whenever we use large types of data structures to save memory because what happens is we do not give a value directly instead we pass to a reference to the method.
- Dereference: So Dereference actually returns an original value when a reference is Dereferenced.
What dereference Actually is?
Dereference actually means we access an object from heap memory using a suitable variable. The main theme of Dereferencing is placing the memory address into the reference. Now, let us move to the solution for this error,
How to Fix “int cannot be dereferenced” error?
Note: Before moving to this, to fix the issue in Example 1 we can print,
System.out.println(sum); // instead of sum.length
Calling equals() method on the int primitive, we encounter this error usually when we try to use the .equals() method instead of “==” to check the equality.
Example 2:
Java
public class GFG {
public static void main(String[] args)
{
int gfg = 5;
if (gfg.equals(5)) {
System.out.println("The value of gfg is 5");
}
else {
System.out.println("The value of gfg is not 5");
}
}
}
Output:
Still, the problem is not fixed. We can fix this issue just by replacing the .equals() method with”==” so let’s implement “==” symbol and try to compile our code.
Example 3:
Java
public class EqualityCheck {
public static void main(String[] args)
{
int gfg = 5;
if (gfg == 5)
{
System.out.println("The value of gfg is 5");
}
else
{
System.out.println("The value of gfg is not 5");
}
}
}
Output
The value of gfg is 5
This is it, how to fix the “int cannot be dereferenced error in Java.
Last Updated :
10 Jan, 2022
Like Article
Save Article
I’m fairly new to Java and I’m using BlueJ. I keep getting this «Int cannot be dereferenced» error when trying to compile and I’m not sure what the problem is. The error is specifically happening in my if statement at the bottom, where it says «equals» is an error and «int cannot be dereferenced.» Hope to get some assistance as I have no idea what to do. Thank you in advance!
public class Catalog {
private Item[] list;
private int size;
// Construct an empty catalog with the specified capacity.
public Catalog(int max) {
list = new Item[max];
size = 0;
}
// Insert a new item into the catalog.
// Throw a CatalogFull exception if the catalog is full.
public void insert(Item obj) throws CatalogFull {
if (list.length == size) {
throw new CatalogFull();
}
list[size] = obj;
++size;
}
// Search the catalog for the item whose item number
// is the parameter id. Return the matching object
// if the search succeeds. Throw an ItemNotFound
// exception if the search fails.
public Item find(int id) throws ItemNotFound {
for (int pos = 0; pos < size; ++pos){
if (id.equals(list[pos].getItemNumber())){ //Getting error on "equals"
return list[pos];
}
else {
throw new ItemNotFound();
}
}
}
}
asked Oct 1, 2013 at 6:08
1
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"
answered Oct 1, 2013 at 6:10
Juned AhsanJuned Ahsan
67.8k12 gold badges98 silver badges136 bronze badges
2
Basically, you’re trying to use int as if it was an Object, which it isn’t (well…it’s complicated)
id.equals(list[pos].getItemNumber())
Should be…
id == list[pos].getItemNumber()
answered Oct 1, 2013 at 6:10
MadProgrammerMadProgrammer
344k22 gold badges231 silver badges367 bronze badges
4
Dereferencing is the process of accessing the value referred to by a reference . Since, int is already a value (not a reference), it can not be dereferenced.
so u need to replace your code (.) to(==).
answered Feb 21, 2022 at 5:20
Assuming getItemNumber() returns an int, replace
if (id.equals(list[pos].getItemNumber()))
with
if (id == list[pos].getItemNumber())
answered Oct 1, 2013 at 6:10
John3136John3136
28.9k4 gold badges51 silver badges69 bronze badges
Change
id.equals(list[pos].getItemNumber())
to
id == list[pos].getItemNumber()
For more details, you should learn the difference between the primitive types like int, char, and double and reference types.
answered Oct 1, 2013 at 6:11
Code-ApprenticeCode-Apprentice
81.8k23 gold badges145 silver badges268 bronze badges
As your methods an int datatype, you should use «==» instead of equals()
try replacing this
if (id.equals(list[pos].getItemNumber()))
with
if (id.equals==list[pos].getItemNumber())
it will fix the error .
answered Jun 23, 2018 at 14:59
AshitAshit
596 bronze badges
I think you are getting this error in the initialization of the Integer somewhere
answered Jan 29 at 6:18
1
try
id == list[pos].getItemNumber()
instead of
id.equals(list[pos].getItemNumber()
answered Oct 1, 2013 at 6:12
Ali HashemiAli Hashemi
3,1783 gold badges34 silver badges48 bronze badges
In this post, we will see how to resolve error int cannot be dereferenced in java.
Table of Contents
- Example 1 : Calling toString() method on primitive type int
- Solution 1
- Change the int[] array to Integer[]
- Cast int to Integer before calling toString() method
- Use Integer.toString()
- Example 2 : Calling equals() method on primitive type int
- Solution 2
As we know that there are two data types in java
- primitive such as int, long, char etc
- Non primitive such as String, Character etc.
One of the common reasons for this error is calling method on a primitive datatype int. As type of int is primitive, it can not dereferenced.
Dereference is process of getting the value referred by a reference. Since int is primitive and already have value, int can not be dereferenced.
Read also: Char cannot be dereferenced
Let’s understand this with the help of simple examples:
Example 1 : Calling toString() method on primitive type int
Let’s say you want to copy int array to String array and you have written below code:
|
package org.arpit.java2blog; public class CopyIntArrayToString { public static void main(String[] args) { int[] arr=new int[] {1,2,3}; String[] arrStr=new String[arr.length]; for (int i=0;i<arr.length;i++) { arrStr[i]=arr[i].toString(); } System.out.println(Arrays.toString(arrStr)); } } |
When you will compile the code, you will get below error:
C:\Users\Arpit\Desktop\javaPrograms>javac CopyIntArrayToString.java
CopyIntArrayToString.java:10: error: int cannot be dereferenced
arrStr[i]=arr[i].toString();
^
1 error
Solution 1
We are getting this error because we are calling toString() method on primitive data type int.
There are 2 ways to fix the issue.
Change the int[] array to Integer[]
We can change int[] array to Integer[] to resolve the issue.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
package org.arpit.java2blog; import java.util.Arrays; public class CopyIntArrayToString { public static void main(String[] args) { Integer[] arr=new Integer[] {1,2,3}; String[] arrStr=new String[arr.length]; for (int i=0;i<arr.length;i++) { arrStr[i]=arr[i].toString(); } System.out.println(Arrays.toString(arrStr)); } } |
Output:
[1, 2, 3]
Cast int to Integer before calling toString() method
We can cast int to Integer to solve int cannot be dereferenced issue.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
package org.arpit.java2blog; import java.util.Arrays; public class CopyIntArrayToString { public static void main(String[] args) { int[] arr=new int[] {1,2,3}; String[] arrStr=new String[arr.length]; for (int i=0;i<arr.length;i++) { arrStr[i]=((Integer)arr[i]).toString(); } System.out.println(Arrays.toString(arrStr)); } } |
Output:
[1, 2, 3]
Use Integer.toString()
We can use Integer.toString() method to convert int to String.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
package org.arpit.java2blog; import java.util.Arrays; public class CopyIntArrayToString { public static void main(String[] args) { int[] arr=new int[] {1,2,3}; String[] arrStr=new String[arr.length]; for (int i=0;i<arr.length;i++) { arrStr[i]=Integer.toString(arr[i]); } System.out.println(Arrays.toString(arrStr)); } } |
Output:
[1, 2, 3]
Example 2 : Calling equals() method on primitive type int
We can get this error while using equals method rather than == to check equality.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
package org.arpit.java2blog; public class IntEqualityCheckMain { public static void main(String[] args) { int i=10; if(i.equals(10)) { System.out.println(«value of i is 10»); } else { System.out.println(«value of i is not 10»); } } } |
When you will compile the code, you will get below error:
C:\Users\Arpit\Desktop\javaPrograms>javac IntEqualityCheckMain.java
IntEqualityCheckMain.java:7: error: int cannot be dereferenced
if(i.equals(10))
^
1 error
Solution 2
We can resolve this issue by replacing equals method with ==.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
package org.arpit.java2blog; public class IntEqualityCheckMain { public static void main(String[] args) { int i=10; if(i==10) { System.out.println(«value of i is 10»); } else { System.out.println(«value of i is not 10»); } } } |
Output:
value of i is 10
That’s all about how to fix int cannot be dereferenced in java.
I am beginning to program War (the card game) and the methods have already been instantiated I need to know why I keep getting these errors.
import java.util.*;
public class CardGame {
public static void main(String[] args) {
CardDeck CardDeckA = new CardDeck();
//creates a standard card deck with 52 cards 1 - 10, J, Q, K, A diamond, spade, club, heart
//Card( int value, int suit)
int[] player1 = new int[52];
int[] player2 = new int[52];
int a = player1.length;
int b = player2.length;
for (int i = 0; a <= 26; i++) {
player1[i].deal(); //Error: int cannot be dereferenced
//deal( int n):Deals n cards from the top of the CardDeck, returns Card[]
}
for (int j = 0; a <= 26; j++) {
player2[j].deal();//Error: int cannot be dereferenced
}
}
}
asked May 23, 2013 at 0:55
4
You should call the method as something like
player = CardDeckA.deal(1)
instead of
player1[i].deal()
since player1[i] is a primitive int, it does not have methods.
deal returns int[], depending on how you use it, I suspect it would something similar to:
player = CardDeckA.deal(26)
answered May 23, 2013 at 0:56
zw324zw324
26.8k16 gold badges85 silver badges118 bronze badges
3


