0

I am trying to connect to a database located in my project directory and get data from it, but is give Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException error.

Here is my code:

public static Connection ConnectDB(){
        Connection conn = null;
        Statement stmt = null;
        try{
            Class.forName("org.sqlite.JDBC");
            conn = DriverManager.getConnection("jdbc:sqlite:database01s.sqlite");
            JOptionPane.showMessageDialog(null, "Connected");

        conn.setAutoCommit(false);
        System.out.println("Opened database successfully");

        ResultSet rs = stmt.executeQuery( "SELECT id FROM DAN" );

        while(rs.next()){
            System.out.println( rs.getInt("id") );
        }

        rs.close();
        stmt.close();
        conn.close();

    }catch(ClassNotFoundException | SQLException | HeadlessException e){
        JOptionPane.showMessageDialog(null, e);
        System.err.println( e.getClass().getName() + ": " + e.getMessage() );
        System.exit(0);
    }
    return null;
}

It does connect successfully, but it gives error when it reaches this line: ResultSet rs = stmt.executeQuery( "SELECT id FROM dan" );

And this is a picture of my database to to see if I am entering the right table and info:image

Dan
  • 577
  • 1
  • 12
  • 38
  • possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – m0skit0 Apr 11 '15 at 23:14
  • 1
    You don't appear to set `stmt` to anything other than `null`. An NPE seems inevitable. – Dave Newton Apr 11 '15 at 23:14
  • 1
    You should also consider using an ORM like Hibernate, which makes life much easier. – orbatschow Apr 11 '15 at 23:14
  • 1
    @Synturas Depends, really; when just starting out is good to know the basics. Otherwise when Hibernate explodes there's no context for debugging. – Dave Newton Apr 11 '15 at 23:21
  • @DaveNewton Yes, but i think it's also good to know what's possible, and it would be bad, to never have heard about it. – orbatschow Apr 11 '15 at 23:25
  • @Synturas Thanks for mentioning it. I will check it. – Dan Apr 11 '15 at 23:27

1 Answers1

1

You are getting a null pointer exception because stmt is equal to null, and you are calling executeQuery() on it. You can't call methods on a null object.

satnam
  • 10,719
  • 5
  • 32
  • 42
  • Instance methods, anyway; you can call static method on a null reference. – Dave Newton Apr 11 '15 at 23:15
  • @DaveNewton, you can't call a static method on a null reference. – satnam Apr 11 '15 at 23:16
  • Actually you can. Maybe try it first. – Dave Newton Apr 11 '15 at 23:17
  • So how can I initialize it? I mean what should I put instead of null ? – Dan Apr 11 '15 at 23:19
  • Maybe something like statement = conn.prepareStatement("SELECT id FROM DAN"); – satnam Apr 11 '15 at 23:21
  • @satnam E.g., http://stackoverflow.com/q/12665474/438992 and a ton of others here on SO. – Dave Newton Apr 11 '15 at 23:22
  • Thanks but didnt work. I got same error but this time for added line. – Dan Apr 11 '15 at 23:25
  • Then that means that your "conn" variable is null Dan, correct? – satnam Apr 11 '15 at 23:26
  • Dave Newton, trust me I know how statics work. I was calling you out on your wording. You can't call a static method on a "null reference". You CAN call a static method without a reference. For example, you CANNOT do "foo.bar()" when foo is null. You can however say "Foo.bar()" – satnam Apr 11 '15 at 23:27
  • @satnam yes `conn` is null too. – Dan Apr 11 '15 at 23:28
  • @Sanam Call me out all you want, but you're completely wrong, and there are a ton of references here on SO even if you're too lazy to just try it or check the JLS. That's why I provided you the links. – Dave Newton Apr 11 '15 at 23:28
  • @DaveNewton, go write this program and see if it works for you: Foo foo = null; foo.staticMethod(); – satnam Apr 11 '15 at 23:29
  • @Dan, aren't you setting conn like this: conn = DriverManager.getConnection("jdbc:sqlite:database01s.sqlite"); – satnam Apr 11 '15 at 23:30
  • @DaveNewton, just read it again. And tested it. You were right I was wrong. I did not know you could do that lol – satnam Apr 11 '15 at 23:32
  • @satnam with your help I could fix it. I moved `Statement stmt = conn.createStatement();` afte the line : `conn = DriverManager.getConnection("jdbc:sqlite:database01s.sqlite");` So `conn` isnt null anymore, and it did work. Thanks btw – Dan Apr 11 '15 at 23:32