This question already has answers here:
What is a NullPointerException, and how do I fix it?

(12个答案)


4年前关闭。




我在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驱动程序注册!

但不返回连接状态。我要去哪里错了?

谢谢

最佳答案

可以查找驱动程序类,否则他将抛出ClassNotFoundException。他尝试初始化JdbcOdbcDriver,并在NullPointerException的第453行中抛出initialize()。我想这是一个配置问题。也许他使用了一些配置文件来进行JDBC初始化,而不是您认为的那样。

不幸的是,我的机器上没有sun.jdbc.odbc.JdbcOdbcDriver类,否则我可以查询453行。

提示:在代码中放置了许多日志记录行,然后在另一台机器上打开日志记录并观察他尝试初始化JDBC的参数。

关于java - Mine以外的其他机器上的Null指针异常,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38636003/

10-08 22:28