37

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();
            }
        }
    }
}
Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
BBladem83
  • 603
  • 2
  • 7
  • 17
  • 3
    You're trying to using a `int` where an `Integer`, `Number` or `Object` is expected...`int` does not have any methods – MadProgrammer Oct 01 '13 at 06:09

8 Answers8

31

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"
Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136
  • 1
    What can I do if I need to use `Integer.compareTo`? – Roy Cohen Dec 03 '20 at 17:53
  • @RoyCohen [java - compareTo with primitives -> Integer / int - Stack Overflow](https://stackoverflow.com/questions/9150446/compareto-with-primitives-integer-int) – user202729 Feb 19 '22 at 13:18
6

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()
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • One doubt: == compares reference for objects and compares values for primitives, right? Please correct if I am wrong. – rohan-patel Jan 10 '14 at 04:40
  • Yes. Primitives are special. – MadProgrammer Jan 10 '14 at 04:41
  • Actually studying interface I received this error and googling brought me to this answer. Kindly look if you can: `error:int cannot be dereferenced` `System.out.println("A = " + A.AB);` The class in which SOP is called implements one `interface C` and A is super interface of C. int AB is defined in both interface. Error occurs at `A.AB`. – rohan-patel Jan 10 '14 at 04:46
  • `int AB` can't be declared in any `interface`, it could only possibly be declared within a `class`... – MadProgrammer Jan 10 '14 at 04:52
1

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(==).

0

Assuming getItemNumber() returns an int, replace

if (id.equals(list[pos].getItemNumber()))

with

if (id == list[pos].getItemNumber())

John3136
  • 28,809
  • 4
  • 51
  • 69
0

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.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
0

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 .

Ashit
  • 59
  • 6
0

I think you are getting this error in the initialization of the Integer somewhere

  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 30 '23 at 21:47
-1

try

id == list[pos].getItemNumber()

instead of

id.equals(list[pos].getItemNumber()
Ali Hashemi
  • 3,158
  • 3
  • 34
  • 48