adrian adrian - 1 year ago 85
Java Question

Null Pointer Exception in JDBC

I'm trying to execute a Java file, but when I do I get an error

in this part of the program.

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.DriverManager;
import java.sql.SQLException;

public class HiveClient {
// JDBC driver required for Hive connections
private static String driverName = "org.apache.hadoop.hive.jdbc.HiveDriver";
private static Connection con;

private static Connection getConnection(String ip, String port, String user, String password) {
try {
// dynamically load the Hive JDBC driver
} catch (ClassNotFoundException e) {
return null;
} // try catch

try {
// return a connection based on the Hive JDBC driver
return DriverManager.getConnection("jdbc:hive://" + ip + ":" + port + "/default?user=" + user + "&password=" + password);

} catch (SQLException e) {
return null;
} // try catch
} // getConnection

public static void main(String[] args) {
try {
// from here on, everything is SQL!
con = getConnection("", "10000", "myuser", "mypasswd");
Statement stmt = con.createStatement();
ResultSet res = stmt.executeQuery("select column1,column2,otherColumns " + "from mytable where column1='whatever' and columns2 like '%whatever%'");

// iterate on the result
while ( {
String column1 = res.getString(1);
Integer column2 = res.getInt(2);

// whatever you want to do with this row, here
} // while

// close everything

} catch (SQLException ex) {
} // try catch
// doQuery
} // HiveClient

This is the message:

Exception in thread "main" java.lang.NullPointerException
at HiveClient.main(
Java Result: 1

The line the exception is at:

Statement stmt = con.createStatement();

I've checked the
package and
is allowed in the Method Summary.

Answer Source

Let's apply some logic to the problem.

Q: Why is the NPE being thrown by that statement?

A: The only possible explanation is that con has the value null.

Q: Why does con have the value null.

A: The only explanation is that this statement is assigning null to con:

        con = getConnection("", "10000", "myuser", "mypasswd");

And that means that getConnection returns null.

Q: Why is getConnection returning null.

A: Look at the code for that method. There two places where it explicitly returns null. First is when Class.forName(driverName) throws a ClassNotFoundException. Second is when DriverManager.getConnection(...) throws an SQLException. In all other cases, it will return a non-null value. (Read the javadocs ...)

Q: So which is it?

A: Look at the code! Note that in both places where null is returned, you wrote a message to standard output. That message will answer your question!!

The reason you are getting into trouble here is that you are catching exceptions too soon, and returning null as a "remedy". It is that null that is causing the ultimate problem.

The correct way to implement this is to change the declaration for getConnection to say that it throws ClassNotFoundException and SQLException. Then remove the handlers for those exceptions in getConnection and handle them in main instead. And when you do handle them, make sure that you output (or log) the stack traces ... to make it easier to diagnose the real cause of your problems.

(To the authors of some of the other answers: Debugging should not be a matter of guessing what the problem is. You should look at the evidence, and draw logical conclusions from that evidence. When the evidence points to multiple possible causes, consider them all. Guesswork is at best a shortcut. At worst, it will cause you to waste lots of time pursuing "theories" that cannot possibly be true.)