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
This is the stackTrace of the nullPointer:
java.lang.NullPointerException
at java.io.Reader.<init>(Unknown Source)
at java.io.InputStreamReader.<init>(Unknown Source)
at integration_tier.OggettiDAO.MySqlConnection.connect(MySqlConnection.java:70)
at integration_tier.OggettiDAO.CategoriaDAO.create(CategoriaDAO.java:28)
at business_tier.ApplicationService.add(ApplicationService.java:21)
at presentation_tier.ApplicationController.handleRequest(ApplicationController.java:41)
at presentation_tier.FrontController.handleRequest(FrontController.java:29)
at presentation_tier.GuiController.ControllerSchermataImpiegato.catdao(ControllerSchermataImpiegato.java:45)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.reflect.misc.Trampoline.invoke(Unknown Source)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.reflect.misc.MethodUtil.invoke(Unknown Source)
at javafx.fxml.FXMLLoader$MethodHandler.invoke(Unknown Source)
at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(Unknown Source)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventUtil.fireEventImpl(Unknown Source)
at com.sun.javafx.event.EventUtil.fireEvent(Unknown Source)
at javafx.event.Event.fireEvent(Unknown Source)
at javafx.scene.Scene$ClickGenerator.postProcess(Unknown Source)
at javafx.scene.Scene$ClickGenerator.access$8100(Unknown Source)
at javafx.scene.Scene$MouseHandler.process(Unknown Source)
at javafx.scene.Scene$MouseHandler.access$1500(Unknown Source)
at javafx.scene.Scene.impl_processMouseEvent(Unknown Source)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Unknown Source)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(Unknown Source)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$355(Unknown Source)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(Unknown Source)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(Unknown Source)
at com.sun.glass.ui.View.handleMouseEvent(Unknown Source)
at com.sun.glass.ui.View.notifyMouse(Unknown Source)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$149(Unknown Source)
at java.lang.Thread.run(Unknown Source)