• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Comparable not working.

 
Greenhorn
Posts: 28
Netbeans IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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..??
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Netbeans IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
name=name;
means this.name=name;
 
Achin Gupta
Greenhorn
Posts: 28
Netbeans IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Netbeans IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Netbeans IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Netbeans IDE Tomcat Server Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic