• 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

Unexpected NullPointer Going from Servlet to JSP

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm validating that a given number is within a SQL database I have stored locally. If it is, I'm storing that row's data in an object, storing in an ArrayList and passing to the JSP. If not, I'm setting a boolean variable to true and passing that to my JSP.

When doing so, I'm getting a NullPointerException with my boolean variable and I do not know why. I do know what that exception means, I'm just not sure why it applies to my situation. Any advice, or alternatives to how I'm approaching this would be great.

My JSP



My Servlet

 
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Nicholas, welcome to the Ranch!



The session.getAttribute() method will return an Object representing the attribute referred to in the parameter, or null if there is no such attribute. It's quite likely that "no such attribute" will happen in the code you posted. And when you try to cast null to a boolean primitive value, you get... a NullPointerException!

You asked for alternatives. Let me point out that JSP scriptlets have been obsolete for well over a decade now, so I'd suggest you rewrite that code to use the EL. It's actually a lot easier to use than scriptlets, too.

And in your servlet, don't declare productsList as an instance variable. In real systems it's possible for two requests to be using the servlet simultaneously, which would cause products chosen by both requests to be mixed up together and sent to both clients. You also don't ever clear the list, which means the second request, no matter who sent it, is going to get its products along with the products from the first request, no matter who sent it. And the third request will get its products along with all of the others. But declaring it as a local variable in the method which fills the list takes care of both of those problems.
 
Nicholas Mercurio
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:Hi Nicholas, welcome to the Ranch!



The session.getAttribute() method will return an Object representing the attribute referred to in the parameter, or null if there is no such attribute. It's quite likely that "no such attribute" will happen in the code you posted. And when you try to cast null to a boolean primitive value, you get... a NullPointerException!

You asked for alternatives. Let me point out that JSP scriptlets have been obsolete for well over a decade now, so I'd suggest you rewrite that code to use the EL. It's actually a lot easier to use than scriptlets, too.

And in your servlet, don't declare productsList as an instance variable. In real systems it's possible for two requests to be using the servlet simultaneously, which would cause products chosen by both requests to be mixed up together and sent to both clients. You also don't ever clear the list, which means the second request, no matter who sent it, is going to get its products along with the products from the first request, no matter who sent it. And the third request will get its products along with all of the others. But declaring it as a local variable in the method which fills the list takes care of both of those problems.



Hi Paul.

I have heard that it is outdated, unfortunately, my JavaEE class requires it.

If I have the session.getAttribute() tags in their respective if statements, won't they not run if the particular if isn't entered? For example, if itemExists(rs) returns true, the else if will not run, thus not setting the invalidCode attribute? I do see what you're speaking of within the JSP, but not sure how else I would validate the 2nd else if statement checking if invalidCode is true or false if I don't getAttribute before running the elseif.

 
Paul Clapham
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Nicholas Mercurio wrote:I have heard that it is outdated, unfortunately, my JavaEE class requires it.



You're kidding! That's pretty close to professional malpractice in my book.

If I have the session.getAttribute() tags in their respective if statements, won't they not run if the particular if isn't entered? For example, if itemExists(rs) returns true, the else if will not run, thus not setting the invalidCode attribute? I do see what you're speaking of within the JSP, but not sure how else I would validate the 2nd else if statement checking if invalidCode is true or false if I don't getAttribute before running the elseif.



I didn't really follow all of that, but the fix you need is to always set the invalidCode attribute in the servlet regardless of what path you take through the code. Setting it to false at the beginning would be one way. And by the way, why are you using a session attribute for that? To me it doesn't seem like the kind of data which needs to be preserved from one request to the next.
 
Nicholas Mercurio
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:

Nicholas Mercurio wrote:I have heard that it is outdated, unfortunately, my JavaEE class requires it.



You're kidding! That's pretty close to professional malpractice in my book.

If I have the session.getAttribute() tags in their respective if statements, won't they not run if the particular if isn't entered? For example, if itemExists(rs) returns true, the else if will not run, thus not setting the invalidCode attribute? I do see what you're speaking of within the JSP, but not sure how else I would validate the 2nd else if statement checking if invalidCode is true or false if I don't getAttribute before running the elseif.



I didn't really follow all of that, but the fix you need is to always set the invalidCode attribute in the servlet regardless of what path you take through the code. Setting it to false at the beginning would be one way. And by the way, why are you using a session attribute for that? To me it doesn't seem like the kind of data which needs to be preserved from one request to the next.



Thanks Paul. I've been working on this for so long I don't even make sense to myself! But I did get that working. I have come across a new issue (of course!). My JSP does not want to loop through and print all of the correct entries. For example, if I enter 3 correct codes consecutively, I'd want it to print all of the rows on the page.

This is what I'm working with now:

Servlet:



Servlet:

/**
* Servlet implementation class eShop
*/
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:You're kidding! That's pretty close to professional malpractice in my book.


Not to pile on -- but I will -- it's like teaching an electronics course using vacuum tubes.

I know you have no control over the curriculum, but please do be aware that, as Paul said, JSP scriptlets have been obsolete since 2002. When you get a chance (if servlets and JSP will be important to you going forward) study up on the EL (Expression Language) and JSTL (JSP Standard Tag Library) as the modern equivalents.

Good luck with your studies.
 
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Nicholas Mercurio wrote:
Thanks Paul. I've been working on this for so long I don't even make sense to myself! But I did get that working. I have come across a new issue (of course!). My JSP does not want to loop through and print all of the correct entries. For example, if I enter 3 correct codes consecutively, I'd want it to print all of the rows on the page.



First off, do not have the ProductsList as an attribute of the servlet.
If you need to remember the value in the List between requests the simplest thing would be to put it in the session.

Can you show what is being logged during your run?
You might also want to log what values are in the product list at the start and end of a request.
reply
    Bookmark Topic Watch Topic
  • New Topic