I'm writing a Java Swing app in Eclipse. I've exported as a runnable jar. And when I run it. Everything is working fine. So I've sent the same JAR to my second machine. And When I ran it, to my Surprise, this therw a null pointer Exception.
Below is a piece of my code.
try {
String dburl = path.getDBUrl();
System.out.println(dburl);
textArea.append(dburl + "\n");
System.out.println(path.getSystemId + " s id \n");
textArea.append(path.getSystemId + "\n");
// connect to Excel
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
Connection myConn = DriverManager.getConnection(dburl);
String queryString = "select sum(Pages) as totalUnitsCount,sum(TotalErrors) as totalError, Sum(IIF(Type Like 'Formatting Error ',(TotalErrors),0)) as FormsattingCount, Sum(IIF([Type] like 'X Ref Error ',(TotalErrors),0)) as [X ref CountErrs]," + "Sum(IIF(((Type Like 'Formatting Error ' and Critical<>0)),(Critical),0)) as FormsattingErrorCritical," + "Sum(IIF(((Type Like 'X Ref Error ' and Critical<>0)),(Critical),0)) as XRefErrorCritical," + "Count(IIF(((Type Like 'Formatting Error ' and NonCritical<>0)),1,null)) as FormsattingErrorNonCritical," + "Count(IIF(((Type Like 'X Ref Error ' and NonCritical<>0)),1,null)) as XRefErrorNonCritical" + " from [Quality Sheet$]";
statement = myConn.prepareStatement(queryString);
resultSet = statement.executeQuery();
ResultSetMetaData rsMetaData = resultSet.getMetaData();
System.out.println(rsMetaData.getColumnCount());
}
This is working fine on my Machine, But in my second machine it is throwing the exception in the below line.
Connection myConn = DriverManager.getConnection(dburl);
Here is my SetTheExcelPath file
public class SetTheExcelSrcPath {
String getSystemId = System.getProperty("user.name");
public String getDBUrl() {
return "jdbc:odbc:Driver={Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)};DBQ=C:\\Users\\" + getSystemId
+ "\\Desktop\\Quality Sheets\\quality_template.xlsx;";
}
}
Please let me know where Am I going wrong and how Can I fix this.
Here I'm not getting Exception on my machine, it is getting thrown on another machine, I would have debugged if the issue was on my Machine.
Here is my stack trace
java.lang.NullPointerException at
sun.jdbc.odbc.JdbcOdbcDriver.initialize(JdbcOdbcDriver.java: 453) at
sun.jdbc.odbc.JdbcOdbcDriver.connect(JdbcOdbcDriver.java: 153) at
java.sql.DriverManager.getConnection(Unknown Source) at
java.sql.DriverManager.getConnection(Unknown Source) at
Src.Files.TestExcel. < init > (TestExcel.java: 39) at
Src.Files.ReportsGeneratorGUI$3.actionPerformed(ReportsGeneratorGUI.java: 91)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source) at
javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source) at
javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source) at
javax.swing.DefaultButtonModel.setPressed(Unknown Source) at
javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source) at java.awt.Component.processMouseEvent(Unknown Source) at
javax.swing.JComponent.processMouseEvent(Unknown Source) at
java.awt.Component.processEvent(Unknown Source) at
java.awt.Container.processEvent(Unknown Source) at
java.awt.Component.dispatchEventImpl(Unknown Source) at
java.awt.Container.dispatchEventImpl(Unknown Source) at
java.awt.Component.dispatchEvent(Unknown Source) at
java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source) at
java.awt.LightweightDispatcher.processMouseEvent(Unknown Source) at
java.awt.LightweightDispatcher.dispatchEvent(Unknown Source) at
java.awt.Container.dispatchEventImpl(Unknown Source) at
java.awt.Window.dispatchEventImpl(Unknown Source) at
java.awt.Component.dispatchEvent(Unknown Source) at
java.awt.EventQueue.dispatchEventImpl(Unknown Source) at
java.awt.EventQueue.access$500(Unknown Source) at
java.awt.EventQueue$3.run(Unknown Source) at
java.awt.EventQueue$3.run(Unknown Source) at
java.security.AccessController.doPrivileged(Native Method) at
java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source) at
java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue$4.run(Unknown Source) at
java.awt.EventQueue$4.run(Unknown Source) at
java.security.AccessController.doPrivileged(Native Method) at
java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue.dispatchEvent(Unknown Source) at
java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at
java.awt.EventDispatchThread.run(Unknown Source)
for debugging I've added the below code in the file.
try {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
} catch (InstantiationException | IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (ClassNotFoundException e) {
textArea.append("Where is your Oracle JDBC Driver?\n");
e.printStackTrace();
return;
}
textArea.append("JDBC ODBC Driver Registered!\n");
Connection myConn;
try {
myConn = DriverManager.getConnection(dburl, "", "");
} catch (SQLException e) {
textArea.append("Connection Failed! Check output console\n");
e.printStackTrace();
return;
}
if (myConn != null) {
textArea.append("You made it, take control your database now!\n");
} else {
textArea.append("Failed to make connection!\n");
}
When I run this in my machine, I get
JDBC ODBC Driver Registered! You made it, take control your database now!
When I do the same on the second machine, it gives
JDBC ODBC Driver Registered!
But not returning the connection status. Where am I going wrong?
Thanks
He can find the driver class, otherwise he would throw a ClassNotFoundException. He tries to initialize the JdbcOdbcDriver and throws a NullPointerException in line 453 in the initialize(). I suppose that it is a configuration issue. Maybe he uses some configuration files for JDBC initialization other than you think he does.
Unfortunately I don't have the sun.jdbc.odbc.JdbcOdbcDriver class on my machine, otherwise I could look up line 453.
Hint: place lots of logging lines into the code, then switch on the logging on the other machine and watch, with what parameters he tries to initialize JDBC.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With