• Post Reply Bookmark Topic Watch Topic
  • New Topic

Comparable not working.  RSS feed

 
Achin Gupta
Greenhorn
Posts: 28
Java Netbeans IDE Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The code is getting compiled successfully but returning a NULL POINTER EXCEPTION at the run time. can't get what the problem is... Please help..!!
Collections.sort method is returning the problem i think because it is using the interface but dont know why..??
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you look more closely, you'll see that it's inside Collections.sort(), where it calls your compareTo() method, and you'll see the stack trace indicate a line inside that method that's causing the NPE.

And here's why:


What, exactly, do you think name = name; does?
 
Achin Gupta
Greenhorn
Posts: 28
Java Netbeans IDE Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
name=name;
means this.name=name;
 
Achin Gupta
Greenhorn
Posts: 28
Java Netbeans IDE Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sir i still dont get what are you trying to tell me...

ohkay i changed the code to
StudentInfo(String n,int r)
{
name=n;
rollNo=r;
}
but the previous should also have worked it works other wise..
this is working but it is still printing the hash codes of the objects. can't figure out why?
Sir please please help.. i am getting all confused.
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Achin Gupta wrote:name=name;
means this.name=name;

No it doesn't. name=name takes the value of the parameter name, and assigns it to that same parameter name. The this.name=name you mentioned will take the same value, but the extra this. causes that value to be assigned to the instance field, not the parameter.
 
Achin Gupta
Greenhorn
Posts: 28
Java Netbeans IDE Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
okay i get it.. i forgot to override toString() method.. now the hashcodes are not coming.,....
thank you so much sir for your support...!!!
 
Achin Gupta
Greenhorn
Posts: 28
Java Netbeans IDE Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Spoor wrote:
Achin Gupta wrote:name=name;
means this.name=name;

No it doesn't. name=name takes the value of the parameter name, and assigns it to that same parameter name. The this.name=name you mentioned will take the same value, but the extra this. causes that value to be assigned to the instance field, not the parameter.


yes sir i just tested it.. thank you..
 
Achin Gupta
Greenhorn
Posts: 28
Java Netbeans IDE Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you sir Rob and sir Jeff.... everything is resolved.. thank you for your support....
Awesome language(java)+ Awesome Javaranch(www.javaranch.com)=Awesome People(You All).
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!