This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Servlets and the fly likes NullPointerException... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "NullPointerException..." Watch "NullPointerException..." New topic
Author

NullPointerException...

Bryce Martin
Ranch Hand

Joined: Nov 19, 2007
Posts: 269
I'm getting a java.lang.NullPointerException and I can't figure out why. Here is the Stack Trace. Its happening at line 63 in my doPost of my servlet..



This is the code...



the problem is in the else statement, I just don't know why my getServletContext() is causing this error...
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Which is line 63?


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Bryce Martin
Ranch Hand

Joined: Nov 19, 2007
Posts: 269
Its the getServletContext() in my else statement.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60737
    
  65

Here's your problem:

You override the base init and discard the passed config. So it never gets stored and you get an NPE when you try to access it.

The rules:

1) Don't override init if you don't have anything to do.

2) If you do have somethig to do, override the parameterless init().

See the Servlets FAQ for more info.
[ November 21, 2007: Message edited by: Bear Bibeault ]

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bryce Martin
Ranch Hand

Joined: Nov 19, 2007
Posts: 269
I just figured that out and when I came here to post it that is exactly what you had just said. I had started coding my servlet from an example I had found somewhere online, and they had overridden the init(). I didn't have anything to override so I just left it sitting there blank like you would expect of a greenhorn.

I got it to forward on, but when I do the "<p>$(modelBean.Po)</p>" in my jsp it actually prints out "$(modelBean.Po)". Now I assumed that this was some sort of value reference. How exactly do I access my bean data? Or should I, instead of writing a bean, just set a session attribute. I found that doing a session.setAttribute from within my FormManager controller doesn't work. The Simple MVC example uses a bean to control data, so that is what I did to get it working.

My results.jsp page looks like this...



Am I missing something? I just want to see the values and prove that things are working before I go ahead and start to implement more of my logic.

Thanks for the help
Bryce
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

EL expressions use curly braces.
${}

not parenthesis
Bryce Martin
Ranch Hand

Joined: Nov 19, 2007
Posts: 269
Yep, my eyes deceived me when I looked at the example code. My font is pretty small and {} looks a lot like (). Thanks!
 
Consider Paul's rocket mass heater.
 
subject: NullPointerException...
 
Similar Threads
ArrayList not clearing
Eating the Beans with a side of Servlets
Method visibility among classes...
Can't perform logic on my bean class getter methods
Clear bean values onLoad