user2803753
user2803753

Reputation: 1

NullPointerException when creating a database

I' m creating an SQL database via Java. The problem is that it always shows mistake when opening the whole project. Please help.Computer writes something like that:

Exception in thread "main" java.lang.NullPointerException
    at Oceny.Sql.<init>(Sql.java:16)

Here is the code for class containing this mistake.

   public class Sql {
   public static final String DRIVER="org.sqlite.JDBC";
   public static final String DB_URL ="jdbc:sqlite:sqlDB/szkola.db";       
   private Connection con;
   private Statement stat;
public Sql() {
    try{
        Class.forName(Sql.DRIVER);
         String sql = "CREATE DATABASE szkola";
         try {
            stat.executeUpdate(sql);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            System.out.println("jebut");
            e.printStackTrace();
        }
    }catch(ClassNotFoundException e){
        System.err.println("Brak sterownika JDBC");
        e.printStackTrace();
    }

        try {
            con = DriverManager.getConnection(DB_URL);
            stat = (Statement) con.createStatement();
        } catch (SQLException e) {
            System.err.println("Problem z otwarciem polaczenia");
            e.printStackTrace();
    }

}

public void createtable(){

    try{
        String przedmiot="CREATE TABLE IF NOT EXISTS przedmiot (id INTEGER PRIMARY KEY AUTOINCREMENT, nazwa varchar(20), sprawdziany int(6), kartkowy int(6), pd int(6))"; //prace = połączenie z inną tabelą prac dodatkowych
        String prace="CREATE TABLE IF NOT EXISTS prace (id INTEGER PRIMARY KEY AUTOINCREMENT, przedmiot varchar(20), praca(250), czas date(100))";
        String plan="CREATE TABLE IF NOT EXISTS plan (id INTEGER PRIMARY KEY AUTOINCREMENT, day varchar(20))";
        String godzina="CREATE TABLE IF NOT EXISTS godzina(id INTEGER PRIMARY KEY AUTOINCREMENT, day varchar(20), godzina varchar(5), przedmiot varchar(20))";//chodiz o godzinę lekcyjną
        stat.execute(przedmiot);
        stat.execute(prace);
        stat.execute(plan);
        stat.execute(godzina);

    }catch(SQLException e){
        System.out.print("problem z tworzeniem tabel");
    }
}
}

Upvotes: 0

Views: 398

Answers (3)

ceperman
ceperman

Reputation: 415

The try/catch blocks in your constructor are in the wrong order. This one should be first so stat is initialised:

try {
  con = DriverManager.getConnection(DB_URL);
  stat = (Statement) con.createStatement();
} catch (SQLException e) {
  System.err.println("Problem z otwarciem polaczenia");
  e.printStackTrace();
}

Upvotes: 0

duffymo
duffymo

Reputation: 308998

peter has sorted out your obvious NPE problem. Next time you should be able to figure it out easily by looking at the stack trace. It tells you the line at which it occurred. One of the references on that line is null. It's not hard to find.

Lots of mistakes to correct:

  1. Don't print messages to System.out if you catch an exception; print or log the stack trace. It gives far more information.
  2. It's not common to create a database in Java. You usually create the database and schema using database admin tools and only use Java to interact with it.
  3. You don't deal with closing Connection, Statement, or ResultSet properly. They should be closed in method scope in finally blocks.
  4. Externalize the database connection information.

Upvotes: 0

peter.petrov
peter.petrov

Reputation: 39477

The variable

stat

is null in your case

Initialize it first.

Seems you first use it, then initialize it.

Upvotes: 4

Related Questions