Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Error : int cannot be dereferenced.

 
Shiveen Pandita
Greenhorn
Posts: 25
Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello.
I needed some advice on why the compare method at the bottom of this code, gives me an int cannot be derefernced error. I was studying the Comparator and Comparable interfaces to learn about sorting.



I'm sorry for the lack of indentation in the code in advance.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And which line did it happen on?

The usual cause of that error is trying to call a method on an int variable. Which you can't do, because it's a primitive variable. The compiler will tell you exactly which line it happened on.

(Rather than apologising for the lack of indentation, a better approach would be to indent it! )
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In this case I can see the problem, though.
getBalance() returns an int. int doesn't have a compareTo() method (or any other methods).
 
R. Jain
Ranch Hand
Posts: 375
1
Java Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew Brown wrote:And which line did it happen on?

The usual cause of that error is trying to call a method on an int variable. Which you can't do, because it's a primitive variable. The compiler will tell you exactly which line it happened on.

(Rather than apologising for the lack of indentation, a better approach would be to indent it! )

I don't know why this doubt came in my mind just now.. But why Java didn't allow Boxing in this case??
It could have created a Wrapper object at runtime and used that to invoke the method..
 
Matthew Brown
Bartender
Posts: 4568
9
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The compiler will autobox an int to an Integer if it knows that it's meant to be an Integer. So, for instance, assigning it to an Integer variable, or calling a method that takes an Integer parameter.

But what you've suggested would be a step further than that. No context is given other than a method being called. It would have to guess that Integer was intended, box it, and then see if compareTo exists in that type (which is does). That would probably be possible, but more complicated, and it isn't included in the language specification.
 
R. Jain
Ranch Hand
Posts: 375
1
Java Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew Brown wrote:The compiler will autobox an int to an Integer if it knows that it's meant to be an Integer. So, for instance, assigning it to an Integer variable, or calling a method that takes an Integer parameter.

But what you've suggested would be a step further than that. No context is given other than a method being called. It would have to guess that Integer was intended, box it, and then see if compareTo exists in that type (which is does). That would probably be possible, but more complicated, and it isn't included in the language specification.

Thanks. Got it
 
Shiveen Pandita
Greenhorn
Posts: 25
Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew Brown wrote:The compiler will autobox an int to an Integer if it knows that it's meant to be an Integer. So, for instance, assigning it to an Integer variable, or calling a method that takes an Integer parameter.

But what you've suggested would be a step further than that. No context is given other than a method being called. It would have to guess that Integer was intended, box it, and then see if compareTo exists in that type (which is does). That would probably be possible, but more complicated, and it isn't included in the language specification.


Thanks that cleared my doubts. I'll keep that in mind now on-wards. And yes, I will keep the indenting advice too...
 
Shiveen Pandita
Greenhorn
Posts: 25
Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
R. Jain wrote:
Matthew Brown wrote:And which line did it happen on?

The usual cause of that error is trying to call a method on an int variable. Which you can't do, because it's a primitive variable. The compiler will tell you exactly which line it happened on.

(Rather than apologising for the lack of indentation, a better approach would be to indent it! )

I don't know why this doubt came in my mind just now.. But why Java didn't allow Boxing in this case??
It could have created a Wrapper object at runtime and used that to invoke the method..


I had the same thing in mind. Looks like we both got our doubts cleared.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic