Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

int cannot be derefrenced

 
theodore sklavenitis
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


I get an error telling me that int cannot be derefrenced.I a, a begginer and i really cant see what i am doing wrong
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the ranch !

You probably wanted to loop through the array, so n.length should be numArray.length, or simply use n.

Next time, please UseCodeTags, and tell us exactly where the error happens (line number and exact error message).
 
Alex Hurtt
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, first of all you have this a couple of times:

for (int i = 0; i <n.length;i++)

when what you probably mean is this:

for (int i = 0; i < numArray.length; i++)

Your variable n is just an integer primitive...it has no length property.
As a small matter of coding practice consider changing the following:


to just


No need to declare the local variable without initializing it to some value here.
I'm not sure what is giving you an error saying 'cannot be dereferenced' but for me when I popped your code into Eclipse it instantly pointed out to me that
"The primitive type int of n does not have a field length"
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alex Hurtt wrote:
I'm not sure what is giving you an error saying 'cannot be dereferenced' but for me when I popped your code into Eclipse it instantly pointed out to me that
"The primitive type int of n does not have a field length"


Try to compile from the command line.
 
Alex Hurtt
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Christophe Verré wrote:
Alex Hurtt wrote:
I'm not sure what is giving you an error saying 'cannot be dereferenced' but for me when I popped your code into Eclipse it instantly pointed out to me that
"The primitive type int of n does not have a field length"


Try to compile from the command line.


Ok I did and I got the authors error with the code as it was written...rather wierd way for the compiler to tell me that a primitive int doesn't have a .length property. When I make the modifications I suggested to the code, it compiles fine.
 
Campbell Ritchie
Sheriff
Posts: 48917
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Writing error messages which users can understand is the most difficult part of creating a compiler. Eclipse is much better for error messages than the Sun/Oracle compiler.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic