This week's book / license giveaways are in the JDBC and Relational Databases and Java in General forums.
We're giving away four copies each of PostGIS in Action and Java Advanced Topics Training and have the authors on-line!
See this thread and this one for details.
The moose likes Struts and the fly likes NullPointerException in ActionForm.validate() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Frameworks » Struts
Bookmark "NullPointerException in ActionForm.validate()" Watch "NullPointerException in ActionForm.validate()" New topic
Author

NullPointerException in ActionForm.validate()

Juhan Voolaid
Ranch Hand

Joined: Nov 18, 2003
Posts: 179
I have a strange problem and I don't know how to solve it. It is maybe because I am using ActionForm first time and doing something wrong.

The problem is that I get a NullPointerException for a String variable that is a parameter in my form-bean - Private variable in my ActionForm class, that is a POST parameter from my (html)form.

This is the ActionForm class that I am having problems with:


Here is the Exception:


I have no idea what I am doing wrong.
[ May 02, 2006: Message edited by: Juhan Voolaid ]
Ravi Seshu
Ranch Hand

Joined: Mar 19, 2005
Posts: 38
hey better initialize variables... with some value as local vbles need to be initialized.....for boolean ... try now..

-eswar
Juhan Voolaid
Ranch Hand

Joined: Nov 18, 2003
Posts: 179
OK - this is embarrasing.
Problem was with my html/form. The Exception was caught to line:
bupdated2=notEmpty(updated2);
Where the variable "updated2" was apsent in my html/form page.

sry
Jason Rodrigues
Greenhorn

Joined: Oct 20, 2005
Posts: 21
The reason you are getting NPE is because you havent initailzed your object
is you try to do s.length on a null object it will give you NPE . to avoid that you need to have another check to make sure if it is null and return
false . Replace this with your function and you should be good

private boolean notEmpty(String s){
boolean b=true;
if (s == null) b=false;
if(s.length()==0){ b=false;}return b;
}
Jason Rodrigues
Greenhorn

Joined: Oct 20, 2005
Posts: 21
sorry it wont work
you will need in the function
if (s == null) return false;
else if (s.trim().length() == 0) return false
else
return true;
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: NullPointerException in ActionForm.validate()