3

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

user3872094
  • 3,269
  • 8
  • 33
  • 71

3 Answers3

2

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.

Sufian
  • 6,405
  • 16
  • 66
  • 120
Keksi
  • 78
  • 7
  • Hi @Keski, this is what is present in line 453 of the file `if (OdbcApi.getTracer().isTracing()) { OdbcApi.getTracer().trace("Unable to load JdbcOdbc library"); }` – user3872094 Jul 28 '16 at 12:42
  • @Keski, Can you please let m know on how can I do the logging thing? – user3872094 Jul 28 '16 at 12:55
  • The easiest way is: System.out.println("x=<" + x + ">"); A more sophisticated way is to use one of the famous logging tools like Log4J or JUL (= java.util.logging). – Keksi Jul 29 '16 at 11:17
  • Hm - somewhere in this line he encounters null. As far as I can judge, this can only be the expression 'OdbcApi.getTracer()'. So OdbcApi can not return a proper Tracer object. A matter of configuration maybe? On your machine, do step into the method "getTracer()" and try to find out, in which case null gets returned. – Keksi Jul 29 '16 at 11:29
0

You probably didn't set up the driver in the second machine, it's throwing a NullPointerException because it can't load the driver .

Gherbi Hicham
  • 2,416
  • 4
  • 26
  • 41
  • How do I set up the driver?, I packaged everything and exported as a `runnable jar` – user3872094 Jul 28 '16 at 12:16
  • Maybe you have a problem in the used libraries, check if you don't have any library errors in your project. – Gherbi Hicham Jul 28 '16 at 12:18
  • In Eclipse, before Export? – user3872094 Jul 28 '16 at 12:19
  • Yes in Eclipse, right click on your project-> build path-> configure build path and check if there are any errors inthe libraries. – Gherbi Hicham Jul 28 '16 at 12:25
  • There are no error in my libraries my friend :( – user3872094 Jul 28 '16 at 12:26
  • Maybe it`s caused due to is trying to read in other computers your excel file. – Manu AG Jul 28 '16 at 12:28
  • I'm not exactly sure but i think you didn't properly configure your path (dburl) etc.. in the second machine, with the error you are getting it must be a related matter, repeat the same steps you did to configure the odbc driver connection in the first machine. – Gherbi Hicham Jul 28 '16 at 12:32
  • Yes, I did the same in second machine, and the path to read is same on both the machines. I placed the same copy in second machine and pasted the file. When I did a `sysout`, even that pointed to the right path and right file in the second machine. – user3872094 Jul 28 '16 at 12:38
  • What is the value of dburl? – ultrajohn Jul 28 '16 at 15:00
0

I see that your are using a Excel Driver that point to the quality_template.xlsx for your db. Either the file is not there on your other machine or the path of the dburl is not correct.

public String getDBUrl() {
    return "jdbc:odbc:Driver={Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)};DBQ=C:\\Users\\" + getSystemId
            + "\\Desktop\\Quality Sheets\\quality_template.xlsx;";
}

You check the resulting path with a log when getting ther DBurl or put a breakpoint there.

Chris
  • 1,080
  • 20
  • 44