File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JDBC and the fly likes Help with NullPointerException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Help with NullPointerException" Watch "Help with NullPointerException" New topic
Author

Help with NullPointerException

Tony Lavalle
Ranch Hand

Joined: Jan 18, 2005
Posts: 36
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 ]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61413
    
  67

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 ]

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Tony Lavalle
Ranch Hand

Joined: Jan 18, 2005
Posts: 36
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?
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30749
    
156

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.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Tony Lavalle
Ranch Hand

Joined: Jan 18, 2005
Posts: 36
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
Marshal

Joined: May 26, 2003
Posts: 30749
    
156

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

Joined: Jan 18, 2005
Posts: 36
I will try this thanks
Tony Lavalle
Ranch Hand

Joined: Jan 18, 2005
Posts: 36
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
Marshal

Joined: May 26, 2003
Posts: 30749
    
156

Tony,
Thanks for providing the answer so future people reading this thread can see it. I'm glad to hear you got it working.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Help with NullPointerException