File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Struts and the fly likes How to handle java.lang.NullPointerException in Struts2?.. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "How to handle java.lang.NullPointerException in Struts2?.." Watch "How to handle java.lang.NullPointerException in Struts2?.." New topic
Author

How to handle java.lang.NullPointerException in Struts2?..

Leonardo Carreira
Ranch Hand

Joined: Apr 07, 2009
Posts: 489

Hi Friends...

i have a little question..
how to handle the java.lang.NullPointerException..
iam sorry if this is probably common question first..

example i have made 2 Actions class..
its named NewCategory, and InsertCategory..

NewCategory is Parent Class, which provide all properties, example
private String categoryCode;
private String categoryName;
..

InsertCategory is Sub Class

in the NewCategory i just write code like this :


and in the InsertCategory, i override validate method and execute method, example..


i configure both of NewCategory and AddCategory, so i just call them like this one :
http://localhost:8080/MyApplication/Category/newCategory.action -> (For NewCategory Action)
http://localhost:8080/MyApplication/Category/insertCategory.action -> (For InsertCategory Action)

when i go to URL http://localhost:8080/MyApplication/Category/newCategory.action, and the Form displayed..
then i submit.. its run well..

but..
when i direct go to URL http://localhost:8080/MyApplication/Category/insertCategory.action, and the Message Error (NullPointerException) displayed..

is it any way to handle the NullPointerException?
example i want to if any error happened i want to redirect it to some page..

Thanks in advance..


Sorry, perhaps my english language isn't too good.. Prepare for SCJP 6, Please God help me.. ☼
References : [Java.Boot] [JavaChamp] [JavaPrepare]
Mudit Srivastava
Ranch Hand

Joined: Apr 30, 2007
Posts: 38
you could use a global exception handler through the Exception Handler Interceptor.

in your struts-default.xml, use the following tags,



http://struts.apache.org/2.0.6/struts2-core/apidocs/com/opensymphony/xwork2/interceptor/ExceptionMappingInterceptor.html

Hope this solves a part of the problem.

BR
Leonardo Carreira
Ranch Hand

Joined: Apr 07, 2009
Posts: 489

@Mudit..

Thanks for your post reply first..

i have tried it, but its still doesn't work..

The Message Error which displayed is still java.lang.NullPointerException...


Maybe the cause is The Parent Class (NewCategory Action) does not instantiated first..

Thanks in advance..
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Parent class not instantiated first? I'm not sure what you think you mean by that.

It tells you the *exact* line the NPE is occurring on--it's not to hard to figure out where the code is failing. We, however, *don't* know which exact line the NPE is occurring on, so it's impossible to help. Without knowing more I'd say that the getters are returning null.
Leonardo Carreira
Ranch Hand

Joined: Apr 07, 2009
Posts: 489

Thanks David for your post reply :D .....

The problem is when i go to URL http://localhost:8080/MyApplication/Category/insertCategory.action directly...

example my parent class designed like this one :


its accessed with URL : http://localhost:8080/MyApplication/Category/newCategory.action

my sub class designed like this one :


and its accessed with URL : http://localhost:8080/MyApplication/Category/insertCategory.action


when i access http://localhost:8080/MyApplication/Category/newCategory.action first, then i fill new category form
then i click Submit button which action http://localhost:8080/MyApplication/Category/insertCategory.action... its run well..


but when i directly go to URL http://localhost:8080/MyApplication/Category/insertCategory.action.. it display Error.. :banghead:

Thanks in advance :)
Mudit Srivastava
Ranch Hand

Joined: Apr 30, 2007
Posts: 38
I hope you also tried this



when the previous one didnt work. It's strange to me that the global exceptions arent being handled. Can you also show me a sample of the struts-default and the interceptor stack that you are using.

Regards
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

I wrote: It tells you the *exact* line the NPE is occurring on--it's not to hard to figure out where the code is failing. We, however, *don't* know which exact line the NPE is occurring on, so it's impossible to help. Without knowing more I'd say that the getters are returning null.
Leonardo Carreira
Ranch Hand

Joined: Apr 07, 2009
Posts: 489

I got it.. :D

The solution is use try.. catch block.. like the following code :


Thanks David and Mudit.. :)
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

That's a solution, but it seems like it'd make a lot more sense to use something like Apache Commons' StringUtils.isBlank().
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to handle java.lang.NullPointerException in Struts2?..
 
Similar Threads
How to hide action "extension" in URL? (Struts2)
Struts2 A Query regarding Validation and displaying Error messages
Struts 2 + Spring + Interceptors + Bean Null
validation problem.
[Ask] Handle Wrong Actions?" Error Code 404 (Http Not Found)