我正在Eclipse中编写Java Swing应用程序。我已经导出为一个可运行的jar。当我运行它。一切都很好。所以我把相同的JAR发送到我的第二台机器上。当我把它跑到惊喜中时,这就是null pointer Exception
。
以下是我的一段代码。
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());
}
这在我的机器上工作正常,但是在我的第二台机器中,它会在下面的行中抛出异常。
Connection myConn = DriverManager.getConnection(dburl);
这是我的 SetTheExcelPath 文件
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;";
}
}
请告诉我出错的地方以及如何解决这个问题。
在这里,我没有在我的机器上获得异常,它被抛到另一台机器上,如果问题出现在我的机器上,我会调试。
这是我的堆栈跟踪
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)
用于调试我在文件中添加了以下代码。
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");
}
当我在我的机器上运行时,我得到了
JDBC ODBC驱动程序已注册!你做到了,控制你的数据库 现在!
当我在第二台机器上执行相同操作时,它会给出
注册JDBC ODBC驱动程序!
但是没有返回连接状态。我哪里错了?
由于
答案 0 :(得分:2)
他可以找到驱动程序类,否则他会抛出ClassNotFoundException
。他尝试初始化 JdbcOdbcDriver
并在NullPointerException
的第453行中引发initialize()
。我想这是一个配置问题。也许他使用一些配置文件进行JDBC初始化,而不是你认为的那样。
不幸的是我的机器上没有sun.jdbc.odbc.JdbcOdbcDriver
课程,否则我可以查看第453行。
提示:将大量日志记录行放入代码中,然后打开另一台计算机上的日志记录并观察他尝试初始化JDBC的参数。
答案 1 :(得分:0)
你可能没有在第二台机器上设置驱动程序,因为它无法加载驱动程序,所以它会抛出NullPointerException
。
答案 2 :(得分:0)
我发现您使用的Excel驱动程序指向数据库的quality_template.xlsx。您的其他计算机上没有该文件,或者dburl的路径不正确。
MultiParamTypeClasses
在获取DBurl或在其中放置断点时,使用日志检查生成的路径。