1

I'm breaking my head with this:

protected void setParameters(HttpServletRequest request) {

    try {
        page = Integer.parseInt(request.getParameter("p"));
    }
    catch (NumberFormatException | NullPointerException e) {
        messages.add("Invalid page.");
    }

    try {
        status = Integer.parseInt(request.getParameter("s"));
    }
    catch (NumberFormatException | NullPointerException e) {
        messages.add("Invalid status.");
    }

}

Before I placed this method into a super class it was working as expected; but now when I have a query string without the p parameter (for example), the exception (NumberFormatException) is thrown (as expected), then caught by the first catch (as expected) and then rethrown and caught by the calling function (this is not expected). Since the exception was already caught here, the execution should just continue.

I'm attaching a screenshot where you can see:

  • in pink: my breakpoint
  • in green: my current execution line
  • as a balloon: the current type of the exception which clearly says it is a NumberFormatException

enter image description here

Any ideas are very appreciated

Update:

Thank you, this is my calling stack: Servlet > UserListHandler > setParameters

UserListHandler() is a contructor and looks like this:

enter image description here

Servlet looks like this:

enter image description here

I find interesting that the exception the servlet is catching is a NullPointerException.

The top of my stack trace looks like this:

Mar 31, 2014 5:13:06 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [UserListServlet] in context with path [/App.Web] threw exception [java.lang.NullPointerException] with root cause
java.lang.NullPointerException
Andres
  • 1,090
  • 14
  • 30
  • 1
    Can you show the calling code and any evidence that it catches the handled exception too? – Sotirios Delimanolis Mar 31 '14 at 23:01
  • Where does your stack trace indicates your exception was raised? – Edwin Dalorzo Mar 31 '14 at 23:06
  • 2
    Evidently somewhere else is also throwing an exception. The first exception, the one in the screen shot, it won't escape the catch block. If another one is being caught somewhere it means there are two exceptions. – Radiodef Mar 31 '14 at 23:08
  • possible duplicate of [What is a Null Pointer Exception?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception) – Sotirios Delimanolis Mar 31 '14 at 23:19
  • Thank you so much to everyone... Yes, there was a null pointer exception because there was a variable that was never initialized. – Andres Mar 31 '14 at 23:24

1 Answers1

1

It is clear that there is another exception.

Maybe "messages", inside the catch is null and it is throwing a new NullPointerException?

alfcope
  • 2,327
  • 2
  • 13
  • 21