开发者

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

开发者 https://www.devze.com 2023-01-27 08:11 出处:网络
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

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;
        try 
        {
            res = stmt.executeQuery( query );
            while( res.next() ) 
            {
                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 );
            }
            res.close();
         }
         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
...

Thanks,

After calling e.printStackTrace();

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

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?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号