Solomon Ucko Solomon Ucko - 12 days ago 4
Java Question

SQLite NullPointerException error on connection creation

I am getting a NullPointerException in this code:

update.jsp:

<%@ page language="java" import="com.solly.CaliforniaWaterDataVisualizationJSPCode" %>
<title>Updating database</title>
<a href="index.html">Return to home page</a>
<br>
Updating database...<br>
<pre>
<%
CaliforniaWaterDataVisualizationJSPCode.update(request, out);
%>
</pre>


CaliforniaWaterDataVisualizationJSPCode.java:

package com.solly;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspWriter;

import com.solly.parser.PSVParser;

public abstract class CaliforniaWaterDataVisualizationJSPCode {
static public void update(HttpServletRequest request, JspWriter out) throws IOException, SQLException, ClassNotFoundException {
try(Connection dbConnection = DriverManager.getConnection("jdbc:sqlite:California_Water_Data.db")) {
if(request.getParameter("caldata-surface-toxic") != null) {
URL url = new URL("https://data.ca.gov/sites/default/files/Surface_Water_Toxicity_2001_2015.txt");
URLConnection urlConnection = url.openConnection();
try(InputStream inputStream = urlConnection.getInputStream()) {
byte[] data = new byte[urlConnection.getContentLength()];
inputStream.read(data);
String content = new String(data);
PSVParser.parseFile(content);
}
}
out.println("Finished retrieving data from data.ca.gov's 2001 to 2015 surface water toxicity record<br>");
} catch(Throwable e) {
out.println("Error: <br>" + e + "<br><br>" + "Stack trace: <br>");
for(StackTraceElement element : e.getStackTrace()) {
out.println(element + "<br>");
}
out.println("<br>Cause: <br>" + e.getCause());
}
}
}


I am using Java EE 7 update 21. I am using SQLite JDBC 3.7.2's compiled and source JARs (sqlite-jdbc-3.7.2.jar and sqlite-jdbc-3.7.2-sources.jar) from java2s.com running on GlassFish 4.1.1 build 1. You can access my code on GitHub at github.com/dhDZsG9QfQK3VTiF5KOs/California_Water_Data_Visualization. I have created a GitHub issue about this at github.com/dhDZsG9QfQK3VTiF5KOs/California_Water_Data_Visualization/issues/1.

Stack trace:

org.sqlite.NestedDB$CausedSQLException.fillInStackTrace(NestedDB.java:649)
java.lang.Throwable.(Throwable.java:250)
java.lang.Exception.(Exception.java:54)
java.sql.SQLException.(SQLException.java:140)
org.sqlite.NestedDB$CausedSQLException.(NestedDB.java:626)
org.sqlite.NestedDB._open(NestedDB.java:63)
org.sqlite.DB.open(DB.java:86)
org.sqlite.Conn.open(Conn.java:140)
org.sqlite.Conn.(Conn.java:57)
org.sqlite.JDBC.createConnection(JDBC.java:77)
org.sqlite.JDBC.connect(JDBC.java:64)
java.sql.DriverManager.getConnection(DriverManager.java:579)
java.sql.DriverManager.getConnection(DriverManager.java:243)
com.solly.CaliforniaWaterDataVisualizationJSPCode.update(CaliforniaWaterDataVisualizationJSPCode.java:18)
org.apache.jsp.update_jsp._jspService(update_jsp.java:53)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:111)
javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:411)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:473)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:377)
javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:601)
org.apache.catalina.security.SecurityUtil$1.run(SecurityUtil.java:323)
org.apache.catalina.security.SecurityUtil$1.run(SecurityUtil.java:321)
java.security.AccessController.doPrivileged(Native Method)
javax.security.auth.Subject.doAsPrivileged(Subject.java:536)
org.apache.catalina.security.SecurityUtil.execute(SecurityUtil.java:356)
org.apache.catalina.security.SecurityUtil.doAsPrivilege(SecurityUtil.java:214)
org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1676)
org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:318)
org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:416)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:283)
com.sun.enterprise.v3.services.impl.ContainerMapper$HttpHandlerCallable.call(ContainerMapper.java:459)
com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:167)
org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:206)
org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:180)
org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:235)
org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:283)
org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:200)
org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:132)
org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:111)
org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:536)
org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:112)
org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:117)
org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:56)
org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:137)
org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:591)
org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:571)
java.lang.Thread.run(Thread.java:722)

Answer

I solved it by adding DriverManager.registerDriver(new JDBC()); before the try block. What inspired me was the new error message (after restarting the server): "java.sql.SQLException: No suitable driver found for jdbc:sqlite:California_Water_Data.db".

Comments