aspose file tools*
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 Java 8 in Action this week in the Java 8 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: 4246
    
    7

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: 4246
    
    7

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: 367
    
    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: 4246
    
    7

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: 367
    
    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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Error : int cannot be dereferenced.
 
Similar Threads
compareTo()
Collections
Garbage collection query
Need Help in Sorting Multiple columns!
Use of comparator and comparable interface