• 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

Help with NullPointerException

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am in a jsp class they are using tomcat 4 or 3 can't member. I am using 5.5.7 and when I do this assignment i keep getting this for an error.

HTTP Status 500 -

--------------------------------------------------------------------------------

type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

org.apache.jasper.JasperException
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:373)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:295)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:245)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)


root cause

java.lang.NullPointerException
org.apache.jsp.chapter05.sras.main_jsp._jspService(org.apache.jsp.chapter05.sras.main_jsp:61)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:99)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:325)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:295)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:245)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)


note The full stack trace of the root cause is available in the Apache Tomcat/5.5.7 logs.


--------------------------------------------------------------------------------

Apache Tomcat/5.5.7

i have traced to my sqlconn.jsp file.

which looks like this

<%@ page import="java.sql.*" %>
<% String url = "jdbc dbc:SRASDSN";
String usernamedb="";
String passworddb="";
Connection connection=null;
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
connection= DriverManager.getConnection(url,usernamedb,passworddb);
}catch(Exception exc){
out.println(exc.toString());
}
Statement stm= connection.createStatement();
ResultSet rst = null;
%>

so what is it that i am doing wrong. Help me please evne the instructor can't help.
[ February 24, 2005: Message edited by: Bear Bibeault ]
 
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
Hi Tony, a couple of things:

1) I updated the title of your post: "help" isn't a very useful title.

2) This is just one of the many very good reasons why doing this type of processing in a JSP is a very bad idea: it's incredibly difficult to debug. My first piece of advice would be to factor the Java out of your page into a servlet controller, or at least a bean.

Failing that, here's a strong clue:

org.apache.jsp.chapter05.sras.main_jsp._jspService(org.apache.jsp.chapter05.sras.main_jsp:61)



The NPE is occurring at line 61 of the java class that the JSP container created on behalf of your page (main.jsp). You can find this class as a debugging aid.
[ February 24, 2005: Message edited by: Bear Bibeault ]
 
Tony Lavalle
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you, but in this class this is what we are stuck with. the instructor chose this book from course technolgy. not a very good book. how do i turn this thing to a bean?
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Tony,
A bean is just a java class. You stick all the java code into that class and then call the class from a JSP.

For a homework assignment, it isn't as important to keep code out of the JSP. Especially if you are just getting started. But it is something to keep in mind for when you work an a real project and it is a good habit to get into.

As Bear said, look to see what line 61 is in the compiled JSP class. I suspect it is throwing an exception when getting the connection. Then it finishes the try/catch block and has a null connection.

If you are leaving the code in the JSP, try rewriting it as:

Also, note that it is good practice to have a finally block to close the connections.
 
Tony Lavalle
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok so your saying that somewhere in my class in line 61 it is causing this npe? ok so here is what line 61 looks like:

Statement stm= connection.createStatement();

I guess i am missing the point. the instructor is no help so where should i look now?
 
Jeanne Boyarsky
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Tony,
This is occuring because an exception is being thrown. I reposted your code with some comments as to what is happening.



Since an uncaught exception (null pointer) is thrown the stream never gets to flush the output of why it could not obtain a connection. The helpful message as to why is being lost. Try rewriting the code as in my previous post so you can see the reason the connection cannot be obtained. This will be immensely valuable in determining the real problem.
 
Tony Lavalle
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I will try this thanks
 
Tony Lavalle
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
actually I figured it out by mistake.

I had to set a dsn for user and system. funning thing is the book we are using only called for user, not system.

But thanks for you help.
 
Jeanne Boyarsky
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Tony,
Thanks for providing the answer so future people reading this thread can see it. I'm glad to hear you got it working.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic