Fix int cannot be dereferenced error

When we tried to use Primitive types as objects and perform operations on them that are applicable to objects, we ran into dereferenced problems.

Before we get into the specifics of what's causing the current dereferenced situation, let's define what dereferenced implies.

When we construct an object, heap memory is allocated and a reference to this heap memory is generated in the stack.

Once we create an object it allocates space in heap memory and a reference is created in the stack pointing to this heap memory. To put it another way, an object reference is used to set or get the values of an object.

However, because a primitive type variable is a value rather than an object, it cannot be used with object methods such as length(), equals(), getClass(), toString(), and other object methods.

It also tells us that everything in java is not an object :)

Let's look at some common issues that result int cannot be dereferenced and understand the root cause with solutions.

1. How to solve int cannot be dereferenced toString in Java?

I'm assuming we're already aware that the toString method converts applied objects to strings. Please take note of the word "applied objects," which implies that toString should only be used with objects.

To illustrate the reason and possible remedy, consider the following example, which includes generating a dereferenced error.

Solution: int cannot be dereferenced

Explanation :

  • checkMe is an int primitive type that has been defined.
  • Applied toString method on the primitive type.
  • Received dereferenced error because checkMe is not representing reference of object hence it companies and raises an error.

While we develop programs as described above, most of the newer IDEs prompt errors. Using the valueOf method, one of the possible solutions to the above failure is shown below.

int testMe = 10;

System.out.println(Integer.valueOf(testMe).getClass().getSimpleName());

System.out.println(Integer.valueOf(testMe).toString());

System.out.println(Integer.valueOf(testMe).toString().getClass().getSimpleName());


Result:
Integer
10
String

Explanation:

  • We can use the Integer.valueOf method to acquire the Integer instance of the specified int value.
  • After receiving an instance via Integer.valueOf method we can access an object inbuilt method like getClass(), getSimpleName() and toString() to get the desired Class simple name and string version of Integer object.

2. What is Null pointer dereference?

When objects are created, they are pointed by references in the stack. So, in order to access an object's inbuilt method, it should be dereferenced. When we try to manipulate non-reference objects data, we receive a NullPointerException, which causes the program to exit/crashes.

Consider the following scenario:

public class Professor {

    public static void main(String[] args) {

        Professor obj = new Professor();

        System.out.println("Before setting obj null: " + System.identityHashCode(obj));

        System.out.println("Class name: " + obj.getClass().getSimpleName());

        obj = null; // Removing reference 

        System.out.println("After setting obj null: " + System.identityHashCode(obj));

        System.out.println("Class name: " + obj.getClass().getSimpleName());

    }
}


Result:

Before setting obj null: 1304836502

Class name: Professor

After setting obj null: 0

Exception in thread "main" java.lang.NullPointerException: Cannot invoke "Object.getClass()" because "obj" is null

Explanation:

  • Created class Professor for demonstration purpose
  • Using the System.identityHashCode method to check for Hash Code before and after setting null reference.
  • We can see that there is a valid reference to object obj in the result, and we can print the associated class name as Professor.
Once we've set a reference to null . This indicates that the object no longer has a valid reference pointing to it. As a result, if we try to use the object member method on a null reference, it will complain since it is attempting to dereference, which is not possible with primitive and null references. As shown above, a NullPointerException will be thrown.

3. How to fix the dereferenced error with the equals method in java?

It's possible that the reason for dereferenced is the same. Consider the following scenario:

 int var = 10;
if (var.equals(10) ) {
}
Result: dereferenced error

Explanation:

  • We're trying to use primitive type variables (var) to access an Integer class method equals to compare two int values.
  • Primitive types, as we learned at the beginning of this post, do not contain any member methods for manipulating values, such as equals, toString, and so on. As a result, a dereferenced error occurs.

Let's look at how equals can be used to compare int numbers.

int var = 101;

Integer test = 10;

if (Integer.valueOf(var).equals(test) ) {
  System.out.println("Given number is equal to 10");
} else {
  System.out.println("Given number is not equal to 10");
}

Result: Given number is not equal to 10

Integer.valueOf returns an integer representation of an int value, which can then be compared using the equals method.

Was this post helpful?

Send Feedback

Practice Multiple-Choice Questions

  • What will the outcome of the following numerical comparison done in a loop be?
    int[] intArr=new int[] {10,52};
    
    Integer checkWith = 10;
    
    
    for (int i=0;i<intArr.length;i++) {
        if (intArr[i].equals(checkWith)) {
            System.out.println("Matched");
        } else {
            System.out.println("UnMatched");
        }
    }
    
    
    Options are:

Connect With Us

We hope you find this Post Insightful.

Please Follow Us and Stay tuned with upcoming blog posts and updates.

You May Like to Read

JAVA Articles for Beginners & Advanced Learners