使用外部文件的空指针异常

时间:2015-11-05 14:43:54

标签: java mysql file path nullpointerexception

当我尝试使用其他文件执行脚本文件来查找脚本时,我得到private static readonly HashSet<char> _nameChars = new HashSet<char>("abcde...");

以下是使用文件DB.conf执行与MySql DB的连接的代码。如果数据库不存在,则使用DB.conf文件,代码应该提取脚本的路径(脚本的路径与DB.conf相同)。

这是我的代码:

NullPointerException

这是DB.conf文件的内容

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;
}

}

这是nullPointer的stackTrace:

localhost:3306
CarLoan
root
root
CarLoan.sql

1 个答案:

答案 0 :(得分:0)

根据您的堆栈跟踪,您在MySqlConnection.java的第70行有一个例外。根据你的说法,这条线是

BufferedReader reader = new BufferedReader(new InputStreamReader(input2));

getClass().getResourceAsStream(sqlScriptPath);的调用返回null值,将InputStream input2设置为null。您无法使用new InputStreamReader(input2)变量null创建input2

根据您的文件DB.confsqlScriptPath的值为"CarLoan.sql"。考虑到您对getClass().getResourceAsStream("/DB.conf");的致电成功,sqlScriptPath中的DB.conf值可能为"/CarLoan.sql"

确认sqlScriptPath的值设置为"/CarLoan.sql",并且从getClass().getResourceAsStream(sqlScriptPath);

检索为非空

确认明确命名为"CarLoan.sql"且文件大小相同的文件与"DB.conf"位于同一文件夹中,并具有与"DB.conf"文件相同的安全访问权限。

另外,请确保所有IO .close()调用均在finally子句中完成。