How to Fix int cannot be dereferenced Error in Java?


"int cannot be dereferenced", a common error in Java that may occur while converting a variable of integer type into String or while comparing it with other primitive type variables. It might be difficult to debug for a beginner programmer, but once we get the alternative ways of converting and comparing integers, it will become a piece of cake for us. Try to stick with us till the end of this article to find the reasons and possible solutions for fixing the 'int cannot be dereferenced error'.

How to Fix int cannot be dereferenced Error in Java

Before fixing the 'int cannot be dereferenced' error, let's familiarize ourselves with the basics of reference and dereferencing in Java.

Reference and Dereferencing

In Java, whenever we define an object with the help of class name, this definition is called a reference to that object. The reference is created so that we can access the properties and methods of that object using the dot (.) operator. This whole process is termed as dereferencing.

Let's have a look at an example.

Example 1

The following example illustrates the dereferencing in Java.

public class Example1 {
   void showMsg() {
      System.out.println("Tutorialspoint Welcomes you!!");
   }
   public static void main(String[] args) {
      Example1 obj = new Example1(); // creating a reference
      obj.showMsg(); // dereferencing to access the method
   }
}

Output

Tutorialspoint Welcomes you!!

In the above example, we have created a reference named 'obj' and using this reference, we have accessed the 'showMsg()' method.

Reason for 'int cannot be dereferenced'

There exist two types of variables in Java: primitive type such as integer, double, float and so forth and reference type variables such as String. Since the integer is not a reference type variable we will encounter the 'int cannot be dereferenced' error whenever we try to use it with methods that work with reference types only.

Here are the possible situations that may cause the 'int cannot be dereferenced' error:

Calling equals() method with integer

The 'equlas()' method returns true if we compare two objects or reference type variables like String and they are logically the same otherwise false. However, a few of us use this method with primitives like integer that causes 'int cannot be dereferenced' error.

Example 2

In this example, we will use 'equals()'' method with integers to show the error.

public class Example2 {
   public static void main(String[] args) {
      int nums = 567;
      System.out.println(nums.equals(765));
   }
}

Output

Example2.java:4: error: int cannot be dereferenced
        System.out.println(nums.equals(765));
^
1 error

Example 3

In this example, we will use '==' operator instead of 'equals()' method to fix the error.

public class Example3 {
   public static void main(String[] args) {
      int nums = 567;
      Boolean isEqual = (nums == 765); // comparing using == operator
      System.out.println("Is 567 is equal to 765: " + isEqual);
   }
}  

Output

Is 567 is equal to 765: false

Calling compareTo() method with integer

The 'compareTo()' method returns 0 when two objects are equal, 1 if the first object is greater otherwise -1. Like the 'equlas()' method, it is also used with reference types only.

Example 4

The following example illustrates whether we get any error or not while using 'compareTo()' method with integers.

public class Example4 {
   public static void main(String[] args) {
      int nums1 = 765;
      int nums2 = 567;
      System.out.println(nums1.compareTo(nums2));
   }
}

Output

Example4.java:6: error: int cannot be dereferenced
        System.out.println(nums1.compareTo(nums2));
^
1 error

Example 5

In the following example, instead of 'compareTo()'method we will use the comparison operator to fix the error.

public class Example5 {
   public static void main(String[] args) {
      int nums1 = 765;
      int nums2 = 567;
      System.out.println(nums1 > nums2);
   }
}

Output

true 

Calling toString() method with integer

We can't convert the primitives into a String using 'toString()' method, because it also works with reference types only. Instead of primitives, we can use their corresponding wrapper class objects to avoid the 'int cannot be dereferenced' error.

Example 6

The following example demonstrates how to use wrapper class object of the integer to avoid the 'int cannot be dereferenced' error.

 public class Example6 {
   public static void main(String[] args) {
      int nums = 765;
      System.out.println(Integer.toString(nums));
   }
}  

Output

765

Conclusion

In this section, we will conclude our discussion. We started this article by specifying the reasons for “int cannot be dereferenced” error and in the next section, we learned the reasons along with the solutions in detail. We also discovered the term reference and dereferencing in Java.

Updated on: 19-Jul-2023

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements