Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

NullpointerException

 
Arun R. S. Chandran
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am trying to save my table into a list. and I am checking the size of the list is zero or not. But the code shows NullpointerException.
The code is given below.

code:
___________________________

List<Shellmoldtable> shellmoldlabels=getHibernateTemplate().find("select sm from Shellmoldtable sm where (sm.smno=? and sm.identity=? and sm.shopindex=?) ",new Object[]{smno,identity,id});
if( shellmoldlabels.size != 0){
---------------
}

___________________________

In the if statement it is showing the Exception.
Can anybody tell the reason why it is showing Exception.

I know I can use the same like:

//shellmoldlabels.size != 0

But I wish to know why the other one is throwing exception.

-Arun
 
mort sahl
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if shellmoldlabels is null you're going to get the NPE ... test it this way ...

if(shellmoldlabels != null && shellmoldlabels.size != 0) {
doSomething();
}
 
Arun R. S. Chandran
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks..
I am asking that why it is throwing exception. The reason behind the exception. Can you please tell me that also?

-Arun
 
mort sahl
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Simple ... you're doing a shellmoldlabels.size ... shellmoldlabels is null so you can't reference an attribute of a null object
 
Arun R. S. Chandran
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok thank you.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic