my dog learned polymorphism*
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


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
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;
 
 
subject: NullPointerException in ActionForm.validate()