jeudi 5 novembre 2015

Null pointer exception using an external file

I get a NullPointerException when I try to execute a script file using another file to find the script.

Here is the code used to execute the connection to MySql DB using the file DB.conf. If the DB doesn't exists, using the DB.conf file, the code should extract the path of the script (the path of the script is the same of the DB.conf).

Here is my code :

public class MySqlConnection {

/**
 * campo per la connessione al database
 */
protected Connection connessione;

/**
 * campo statement
 */
protected Statement stat;

/**
 * campo per i risultati della query
 */
  protected ResultSet res;

    /**
 * campo di preparazione statement
 */
   protected PreparedStatement prepStat;

   protected boolean connect() throws ClassNotFoundException, SQLException,
        IOException {
    connessione = null;
    boolean connection = false;


    InputStream input = getClass().getResourceAsStream("/DB.conf");
    BufferedReader br = new BufferedReader(new InputStreamReader(input));
    new Driver();
    String src = br.readLine().toString();
    String db = br.readLine().toString();
    String user = br.readLine().toString();
    String password = br.readLine().toString();

    try {
        String jdbc = (new StringBuilder("jdbc:mysql://")).append(src)
                .append("/").append(db).toString();
        connessione = DriverManager.getConnection(jdbc, user, password);

    } catch (SQLException e) {
        if (e.getErrorCode() == 1049) {
            JOptionPane
                    .showMessageDialog(null,
                            "Database 'Carloan' non esistente! \nInstallazione del db in corso...");
            String jdbc = (new StringBuilder("jdbc:mysql://")).append(src)
                    .toString();
            connessione = DriverManager.getConnection(jdbc, user, password);
            //HERE I EXTRACT THE PATH OF THE SCRIPT FROM THE FILE DB.CONF
            //AND THAN I GET THE NULLPOINTER
            String sqlScriptPath = br.readLine().toString();
            InputStream input2 = getClass().getResourceAsStream(sqlScriptPath);
            BufferedReader reader = new BufferedReader(new InputStreamReader(input2));
            ScriptRunner sr = new ScriptRunner(connessione, false, false);
            sr.runScript(reader);

            JOptionPane
                    .showMessageDialog(null, "Installazione completata!");
        } else {
            if (e.getErrorCode() == 1045) {
                JOptionPane
                        .showMessageDialog(
                                null,
                                "Username o password del DB errati! \n Controllare il file di configurazione e riprovare.");
            } else {
                JOptionPane
                        .showMessageDialog(
                                null,
                                "Errore nella connessione al database! \n       Contattare l'amministratore di sistema.\nIl sistema verrà chiuso ora.");
            }
        }
    }
    br.close();
    input.close();
    connection = true;
    return connection;
  }

 protected boolean close() throws SQLException {
    boolean chiuso = false;
    connessione.close();
    chiuso = true;
    return chiuso;
}

}

This is the content of the DB.conf file

localhost:3306
CarLoan
root
root
CarLoan.sql

Aucun commentaire:

Enregistrer un commentaire