Forums Register Login

Error : int cannot be dereferenced.

+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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! )
+Pie Number of slices to send: Send
In this case I can see the problem, though.
getBalance() returns an int. int doesn't have a compareTo() method (or any other methods).
+Pie Number of slices to send: Send
 

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..
3
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

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
+Pie Number of slices to send: Send
 

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...
+Pie Number of slices to send: Send
 

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.
Hey, check out my mega multi devastator cannon. It's wicked. It makes this tiny ad look weak:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1999 times.
Similar Threads
Collections
Need Help in Sorting Multiple columns!
Use of comparator and comparable interface
Garbage collection query
compareTo()
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 04:30:08.