I'm trying to edit a JSP for a project and I'm getting a NullPointerException somewhere in the JSP when it's requested from my server.
My web server (JBoss) is reporting the exception, but it's giving me a bogus line number. It's reporting that the exception happened on line 702, but my JSP is only 146 lines long, so I'm unable to identify which line is choking.
What are some good techniques to debug errors in JSPs? I'm using IntelliJ 9 Ultimate as my IDE.
There are several options that can help you:
This answer actually explains how to debug JSPs specifically: https://stackoverflow.com/questions/33739/jsp-debugging-in-intellij-idea. I haven't ever tried this so I followed the next three suggestions when I was using JSP, JBoss, and IntelliJ...
Remember that JSPs are compiled to classes. When it says line 702, it means in the compiled class. If you have
test.jsp, the class name is probably
test_jsp, so open your JBoss work directory and search for
test_jsp.java (once you find the right directory, you'll see that the directory structure matches your JSP directory structure). Whenever I had a JSP exception I could find the line quite easily and usually match it up to the corresponding line in the JSP.
Breakpoints work just fine in Java classes called from a JSP. So, maybe you can move the Java script code that you have in your JSP into a class and debug from the entry point. In the future you can also make a habit of rearranging your logic so that the majority of it is in a class that is being called from the JSP, and the JSP is simple, straightforward and hopefully not throwing any exceptions. This is good practice anyway.
Better yet, remove all (or virtually all) Java logic into Java classes, leaving JSPs for HTML and JSP tags. I know this isn't feasible right away, but again, it's a good idea long term to avoid problems like this.
You can debug a JSP page easily just like a java code:
"a JSP file is the same like debugging a Java class, but the most features (Watch, Inspect, etc) are not implemented. The only way to get the values of variables is the Variables view."
See the link below: