Solving Int Cannot Be Dereferenced in Java

If you are getting “int cannot be dereferenced” error in Java, it means that you are attempting to call a method or an attribute on a int type value. You should note that int is a primitive type and its variables do not store reference, they contain the actual value, so you cannot dereference using int primitive variable unlike wrapper class Integer reference variable.

int n = 5;
System.out.println(n.doubleValue());

Above code will give us “int cannot be dereferenced” compile error. Because, variable n is not a reference variable, it is a primitive variable. You can dereference a Integer reference variable, but not using a int type primitive variable.

You might be knowing it already, but sometimes it may happen unknowingly. Let us see few examples.

error: int cannot be dereferenced – on equals

Integer n = new Integer(100);
System.out.println(Integer.parseInt("100").equals(n));

You might be thinking above example would print “true”. Here, we want to convert a number which is in String format to Integer and then compare it with another Integer object. However, we would get “error: int cannot be dereferenced” compile error on equals method. Why? Method parseInt doesn’t return Integer object reference, it returns int value and we cannot call equals method on int. Due to this we get “int cannot be dereferenced”.

You can fix “int cannot be dereferenced” error by replacing parseInt with valueOf as below:

Integer n = new Integer(100);
System.out.println(Integer.valueOf("100").equals(n));

error: int cannot be dereferenced – on compareTo

Integer n = new Integer(200);
System.out.println(Integer.parseInt("100").compareTo(n));

Here, this code is expecting that parseInt returns Integer and it is attempting to call compareTo method on top of it. But if you check the documentation, parseInt doesn’t return Integer, it returns int type value, and you cannot call methods on int. Initially, it looks like it would print 1. Here, we want to convert a number which is in String format to Integer and then compare it with another Integer object using compareTo method. However, we would get “error: int cannot be dereferenced” compile error on compareTo method as parseInt doesn’t return Integer object reference, it returns int value and we cannot call compareTo method on int.

Similar to the above example, we can fix “int cannot be dereferenced” error by replacing parseInt with valueOf as below:

Integer n = new Integer(100);
System.out.println(Integer.valueOf("100").compareTo(n));

error: int cannot be dereferenced – on toString

Integer n = new Integer(100);
System.out.println(n.intValue().toString());

Looking at above code snippet, it appears that we get Integer when calling intValue and we are trying to call toString on top of it. However, we get “int cannot be dereferenced” error on toString method. This is because, intValue doesn’t return Integer, it returns int type value.

To fix this issue, all you need to do is, you can directly call the toString method on top of Integer reference variable as below:

Integer n = new Integer(100);
System.out.println(n.toString());

error: int cannot be dereferenced within for loop

This is yet another confusing scenario where you must be iterating over an array thinking that the array is of Integer type only to find that the array is actually of type int primitive type.

package com.techstackjournal;

public class Main {

	public static void main(String[] args) {

		Integer n = new Integer(100);
		for (int i=0;i<getNumbers().length;i++) {
			System.out.println(getNumbers()[i].compareTo(n));
		}
	}
	
	public static int[] getNumbers() {
		int[] arr = {1,2,3,4};
		return arr;
	}

}

In this example, there is a getNumbers method which actually returns int array. We are accessing the elements of the array directly using the method call and our code assumes that the array element returns Integer object. However, this results in the compilation error. To fix the issue, we have to wrap getNumbers()[i] inside Integer.valueOf method.

Leave a Comment