Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"int cannot be dereferenced" in Java

Tags:

java

int

bluej

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();             }         }     } } 
like image 369
BBladem83 Avatar asked Oct 01 '13 06:10

BBladem83


People also ask

What can be dereferenced in Java?

Dereferencing follows the memory address stored in a reference, to the place in memory where the actual object resides. When an object has been found, the requested method is called ( toString in this case). When a reference has the value null , dereferencing results in a NullPointerException: Object obj = null; obj.

What does it mean Cannot be dereferenced?

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.

What does double Cannot be dereferenced mean in Java?

double cannot be dereferenced is the error some Java compilers give when you try to call a method on a primitive. It seems to me double has no such method would be more helpful, but what do I know. From your code, it seems you think you can copy a text representation of hours into hoursminfield by doing hours.


1 Answers

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" 
like image 109
Juned Ahsan Avatar answered Sep 21 '22 07:09

Juned Ahsan