Chan Chan - 4 months ago 7x
Java Question

What are possible exceptions can be thrown while running executeQuery()

I'm running JConnector to get data from database( I use mysql ). I checked my data from running a query from Secure Shell, it worked fine. However, it threw NullPointerException when I run my program, it only read the first row. I have just learned Java for a month, so sorry if my question sounds strange to you guys.
This is my code:

private void loadNodesFromDatabase()
Statement stmt = Database.connect();
String query = "SELECT * FROM Node";
ResultSet res;
res = stmt.executeQuery( query );
while( )
Integer id = res.getInt( "Id" );
String position = res.getString( "Position" );
String rule = res.getString( "RuleOnMap" );
Integer foodTax = res.getInt( "FoodTax" );
boolean mapValue = res.getBoolean( "MapValue" );

Node n = new Node( id, PointOnGraph.parse( position ), Node.RuleOnMap.valueOf( rule ), foodTax, mapValue );
System.out.println( n );
nodes.add( n );
catch( Exception e )
System.out.println( "Selection Error: " + e );

This is the query that I created my table:

mysql> CREATE TABLE Node (Id INT(3), Position VARCHAR(12), RuleOnMap VARCHAR(6), FoodTax INT(2), MapValue TINYINT(1), PRIMARY KEY(Id));

And this is the error message from Eclipse:

( 1, '20,20,88', PAY, 10, false )
Selection Error: java.lang.NullPointerException


After calling e.printStackTrace();

( 1, '20,20,88', PAY, 10, true )
Selection Error: java.lang.NullPointerException
at Game.Game.loadNodesFromDatabase(
at Game.Game.<init>(
at Test.GameLoadDatabseTest.loadNodesTest(
at Program.main(
Exception in thread "main" java.lang.NullPointerException
at Test.GameLoadDatabseTest.loadNodesTest(
at Program.main(

Best regards,
Chan Nguyen


It looks like first n is printed out, right?

In this case my bet is that nodes is null. Did you forget to initialize the List?