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.
Nicholas Mercurio wrote: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 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.
Paul Clapham wrote:You're kidding! That's pretty close to professional malpractice in my book.
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.
Did you see how Paul cut 87% off of his electric heat bill with 82 watts of micro heaters? |