This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Beginning Java and the fly likes Error : int cannot be dereferenced. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Error : int cannot be dereferenced. " Watch "Error : int cannot be dereferenced. " New topic
Author

Error : int cannot be dereferenced.

Shiveen Pandita
Greenhorn

Joined: Jul 30, 2012
Posts: 25

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

Joined: Apr 06, 2010
Posts: 4367
    
    8

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

Joined: Apr 06, 2010
Posts: 4367
    
    8

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

Joined: Aug 11, 2012
Posts: 372
    
    1

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

Joined: Apr 06, 2010
Posts: 4367
    
    8

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

Joined: Aug 11, 2012
Posts: 372
    
    1

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

Joined: Jul 30, 2012
Posts: 25

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

Joined: Jul 30, 2012
Posts: 25

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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Error : int cannot be dereferenced.