即時消息處理ArrayList過濾值並傳遞到數據庫,如果記錄重複,則代碼拋出異常,然後更新數據庫中的值並跳轉到下一個記錄。 在記錄90,91或92我得到此異常Java - 空指針將記錄傳遞到數據庫的異常
java.lang.NullPointerException
at com.icc.utilidades.Conexion.actualizarEstadoAlumno(Conexion.java:4296)
at com.icc.controlador.AjaxControlador.doGet(AjaxControlador.java:227)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1041)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:603)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
從conexion 4296的代碼是一個方法和例外是的preparedStatement時
public int actualizarEstadoAlumno(String rut,String estado)
{
try {
ConexionBd();
System.out.println("rut "+rut);
System.out.println("estado "+estado);
PreparedStatement enunciado = conexion.prepareStatement("update alumno set alu_estado = ? "
+ "where alu_rut = ? ;");
enunciado.setString(1, rut);
enunciado.setString(2, estado);
enunciado.execute();
conexion.close();
return 1;
} catch (SQLException e) {
return 0;
}
}
當我打印線路值,它們不是null,所以爲什麼我得到這個異常? ,數據庫連接我敢肯定,因爲當我在其他事情中使用該代碼時,這項工作很好。在記錄編號90之前,代碼將值正確傳遞給數據庫。 可能是什麼問題?請幫助。
這是我連接到數據庫
public void ConexionBd()
{
try
{
Class.forName("org.postgresql.Driver");
conexion = java.sql.DriverManager.getConnection("jdbc:postgresql://localhost:5432/final","postgres","*******");
consulta = conexion.createStatement();
} catch (Exception e) {
e.printStackTrace();
}
}
看起來像'connection'或任何其他變量都是'null'。你應該總是處理你的例外。在返回數據之前,至少在'catch'語句中使用naive'e.printStacktrace()'。 – 2014-10-20 20:18:52
哪一行是行號4296? – Victor2748 2014-10-20 20:20:45
我檢查所有的變量,如連接,這不是問題,因爲我在應用程序的其他部分使用該方法,工作正常。謝謝 – plarac 2014-10-20 20:23:10