Emil Ivanov Emil Ivanov - 1 year ago 133
Java Question

int cannot be dereferenced java

Currently, a beginner in java. I am working on a code that will search for a value in an array and then return how many values it has found from that array.

public int getNumberOfReadings(int location)
int numberOfEntries=-1;
for(Reading readings : readings) {

if(readings.getLocation().contains(location)) {

return numberOfEntries;


It is telling me that int cannot be dereferenced which makes sense because int is primitive. Even though I know this, I still cannot get my head around coding in a way that will fix this error.

Answer Source

Your error is this part:

for (Reading readings : readings)

You kept calling both of them as readings; where you cannot do that and you need to keep separate names.

for (Reading reading : readings)

Try doing that instead. Regarding your error, I assume that because Reading.getLocation() is returning an int which is a primitive, and hence you cannot call contains() on it.

And for more good answers, we require you to give the correct error returned.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download