• 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

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

 
Ranch Hand
Posts: 494
Eclipse IDE Postgres Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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..
 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 494
Eclipse IDE Postgres Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@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..
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 494
Eclipse IDE Postgres Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 494
Eclipse IDE Postgres Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got it.. :D

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


Thanks David and Mudit.. :)
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's a solution, but it seems like it'd make a lot more sense to use something like Apache Commons' StringUtils.isBlank().
reply
    Bookmark Topic Watch Topic
  • New Topic