Results 1 to 5 of 5
- 08-18-2011, 02:32 PM #1
Member
- Join Date
- Aug 2011
- Posts
- 4
- Rep Power
- 0
PreparedStatement or Connection threw Nullpointer exc
Hello
I try to find the problem since a few days. i cant find it...
I am shore that my db-Connection usually works (tried another - working - class.)
here the sc:
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class LoginServlet extends javax.servlet.http.HttpServlet {
/**
* @author Jan
*/
private static final long serialVersionUID = -657338316901350363L;
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession();
// auslesen des Kennworts
String passwort = request.getParameter("passwort");
// auslesen der Kennung
String kennung = request.getParameter("kennung");
//Connection aufbauen
Connection conn = datenbankverbindung.DBConnect.getConnection();
// SQL-Statement definieren
PreparedStatement stmt = null;
String statement = "SELECT benutzername, passwort, admin from benutzer WHERE kennung = ? " +
"and passwort = ? LIMIT 1";
try {
ResultSet rs = null;
stmt = conn.prepareStatement(statement);
rs = stmt.executeQuery();
while(rs.next()) {
String name = rs.getString("vorname") + " " + rs.getString("name");
}
// Resultset instanziieren
//variablen setzen
stmt.setString(1, kennung);
stmt.setString(2, passwort);
} catch (SQLException e2) {
e2.printStackTrace();
}
}}
I get the Connection from the followin class:
package datenbankverbindung;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
/**
* @author Jan
*/
public class DBConnect {
/**
* @return db - connection
*/
public static Connection getConnection() {
Connection conn = null;
String dbname = "postgres";
String nutzer = "postgres";
String server = "localhost:5432";
String passwort = "pw";
String url = "jdbc:postgresql://" + server + "/" + dbname;
try {
Class.forName("org.postgresql.Driver");
conn = DriverManager.getConnection(url, nutzer, passwort);
} catch (ClassNotFoundException exc) {
System.out.println(exc.getMessage());
} catch (SQLException exc) {
System.out.println(exc.getMessage());
}
return conn;
}
}
NPE appears at this line:
"stmt = conn.prepareStatement(statement);"
i hope you can help me...
Jan
- 08-18-2011, 02:57 PM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
conn is null.
It's the only thing that will throw your NPE on that line (as statement is clearly not null having been declared with the SQL string).
I would guess that you are getting another exception, in the getConnection code. That should really be throwing those exceptions, because a failure to connect means you really don't want to be continuing with your code.
- 08-19-2011, 02:10 AM #3
Member
- Join Date
- Aug 2011
- Posts
- 4
- Rep Power
- 0
hm.. here is the hole error stacktrace
19.08.2011 02:06:24 org.apache.catalina.core.AprLifecycleListener init
INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: C:\Program Files\Java\jre6\bin;C:\Windows\Sun\Java\bin;C:\Win dows\system32;C:\Windows;C:\Windows\system32;C:\Wi ndows;C:\Windows\System32\Wbem;C:\Windows\System32 \WindowsPowerShell\v1.0\;C:\Program Files (x86)\ATI Technologies\ATI.ACE\Core-Static;.
19.08.2011 02:06:24 org.apache.tomcat.util.digester.SetPropertiesRule begin
WARNUNG: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:test' did not find a matching property.
19.08.2011 02:06:24 org.apache.tomcat.util.digester.SetPropertiesRule begin
WARNUNG: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:Trikotwebshop' did not find a matching property.
19.08.2011 02:06:24 org.apache.tomcat.util.digester.SetPropertiesRule begin
WARNUNG: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:Trikotwelt' did not find a matching property.
19.08.2011 02:06:25 org.apache.coyote.http11.Http11Protocol init
INFO: Initializing Coyote HTTP/1.1 on http-8080
19.08.2011 02:06:25 org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 1506 ms
19.08.2011 02:06:25 org.apache.catalina.core.StandardService start
INFO: Starting service Catalina
19.08.2011 02:06:25 org.apache.catalina.core.StandardEngine start
INFO: Starting Servlet Engine: Apache Tomcat/6.0.32
19.08.2011 02:06:26 org.apache.coyote.http11.Http11Protocol start
INFO: Starting Coyote HTTP/1.1 on http-8080
19.08.2011 02:06:26 org.apache.jk.common.ChannelSocket init
INFO: JK: ajp13 listening on /0.0.0.0:8009
19.08.2011 02:06:26 org.apache.jk.server.JkMain start
INFO: Jk running ID=0 time=0/47 config=null
19.08.2011 02:06:26 org.apache.catalina.startup.Catalina start
INFO: Server startup in 1106 ms
org.postgresql.Driver
19.08.2011 02:07:08 org.apache.catalina.core.StandardWrapperValve invoke
SCHWERWIEGEND: Servlet.service() for servlet LoginServlet threw exception
java.lang.NullPointerException
at LoginServlet.doPost(LoginServlet.java:43)
at javax.servlet.http.HttpServlet.service(HttpServlet .java:637)
at javax.servlet.http.HttpServlet.service(HttpServlet .java:717)
at org.apache.catalina.core.ApplicationFilterChain.in ternalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.do Filter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invo ke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invo ke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke( StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke (ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invok e(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.servic e(CoyoteAdapter.java:298)
at org.apache.coyote.http11.Http11Processor.process(H ttp11Processor.java:859)
at org.apache.coyote.http11.Http11Protocol$Http11Conn ectionHandler.process(Http11Protocol.java:588)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run( JIoEndpoint.java:489)
at java.lang.Thread.run(Unknown Source)
i have a working class that do what i want but i would/have to outsource the connection method...
also when i copy exactly the same code from this class it failedLast edited by Jan.dbae; 08-19-2011 at 02:12 AM.
- 08-19-2011, 09:18 AM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
"org.postgresql.Driver"
You are getting that line because that is the result of e.getMessage().
Which is why I suggested at the minimum using e.printStackTrace(), but even better throw those exceptions, don't catch them.
- 08-20-2011, 12:51 AM #5
Member
- Join Date
- Aug 2011
- Posts
- 4
- Rep Power
- 0
Similar Threads
-
javax.servlet.ServletException Servlet Execution threw an Exception. PLEASE HELP***
By mkjp2011 in forum Advanced JavaReplies: 1Last Post: 08-23-2011, 11:12 AM -
NullPointer help
By moosick in forum AWT / SwingReplies: 1Last Post: 04-02-2011, 02:35 AM -
NullPointer Exeption, but why?
By warchieflll in forum New To JavaReplies: 10Last Post: 12-09-2010, 04:34 PM -
Nullpointer error
By teppel in forum New To JavaReplies: 0Last Post: 07-10-2010, 08:38 PM -
nullpointer exception in jsp
By fiero in forum JavaServer Pages (JSP) and JSTLReplies: 6Last Post: 11-07-2008, 01:44 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks