How to solve NumberFormatException?

220 Views Asked by At

I'm kind of new with Java, so bear with me... I'm trying to write data from a CSV-file to an Access database using the Jackcess library. The first line of the CSV-file is written perfectly to the database, but from the second line on, it keeps throwing the NumberFormatException. Here's the code:

public void GPXtoAccess() {
    try {
        Access = new Scanner(DummyCSV);
        Access.useDelimiter(";");
        while(Access.hasNextLine()) {
            GPXlat = Access.next();
            GPXlon = Access.next();
            GPXtime = Access.next();
            GPXname = Access.next();
            GPXdesc = Access.next();

            try {
                GPXTable.addRow(Column.AUTO_NUMBER, GPXlat, GPXlon, GPXtime, GPXname, GPXdesc);
            } catch (IOException E) {
                System.out.println("Error: " + E);
                System.out.println("Error is thrown while writing data to table");
            }
        }
    } catch (FileNotFoundException ex) {
        System.out.println("Error: " + ex);
    }
}

It keeps throwing the NumberFormatException:

Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "2016-11-28T11:36:34"
at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:2043)
at sun.misc.FloatingDecimal.parseDouble(FloatingDecimal.java:110)
at java.lang.Double.parseDouble(Double.java:538)
at java.lang.Double.valueOf(Double.java:502)
at com.healthmarketscience.jackcess.impl.ColumnImpl.toNumber(ColumnImpl.java:1443)
at com.healthmarketscience.jackcess.impl.ColumnImpl.writeFixedLengthField(ColumnImpl.java:1105)
at com.healthmarketscience.jackcess.impl.ColumnImpl.writeFixedLengthField(ColumnImpl.java:1072)
at com.healthmarketscience.jackcess.impl.ColumnImpl.writeRealData(ColumnImpl.java:1029)
at com.healthmarketscience.jackcess.impl.ColumnImpl.write(ColumnImpl.java:1021)
at com.healthmarketscience.jackcess.impl.ColumnImpl.write(ColumnImpl.java:1003)
at com.healthmarketscience.jackcess.impl.TableImpl.createRow(TableImpl.java:2708)
at com.healthmarketscience.jackcess.impl.TableImpl.createRow(TableImpl.java:2660)
at com.healthmarketscience.jackcess.impl.TableImpl.addRows(TableImpl.java:2193)
at com.healthmarketscience.jackcess.impl.TableImpl.addRow(TableImpl.java:2086)
at GPX.GPX_Parser.GPXtoAccess(GPX_Parser.java:196)
at GPX.GUI.GPX_Handle.btnExportDatabaseActionPerformed(GPX_Handle.java:234)
at GPX.GUI.GPX_Handle.access$300(GPX_Handle.java:21)
at GPX.GUI.GPX_Handle$4.actionPerformed(GPX_Handle.java:111)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6533)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
at java.awt.Component.processEvent(Component.java:6298)
at java.awt.Container.processEvent(Container.java:2236)
at java.awt.Component.dispatchEventImpl(Component.java:4889)
at java.awt.Container.dispatchEventImpl(Container.java:2294)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
at java.awt.Container.dispatchEventImpl(Container.java:2280)
at java.awt.Window.dispatchEventImpl(Window.java:2746)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.awt.EventQueue$4.run(EventQueue.java:731)
at java.awt.EventQueue$4.run(EventQueue.java:729)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
2

There are 2 best solutions below

0
Juan Acevedo On

I believe what is happening here is your GPXTime does not conform to Access database standards so it doesn't know how to deal with it. You might want to look at the apis and see what format it takes and then you do the conversion from your csv file that has a format '2016-11-28T11:36:34' to whatever format Access database accepts. Try it by converting your dates to milliseconds before calling addRow()

0
Johan Witters On

No worries, we've all started with limited knowledge.

I start by giving some feedback on your code:

1) you seem to have a lot of member variables, possibly even static. E.g the variable Access. There's no reason to do do, if you only use it in the scope you shared with us, i.e. in that method.

2) java naming convention: I strongly recommend you follow java naming conventions, as described here: http://www.oracle.com/technetwork/java/codeconventions-135099.html Following these naming conventions makes the code more readable for other people, e.g. us, here

3) before getting the next(), you should use the hasNext() to reassure there is actually a token available. I recommend you try to debug and fix your issue by doing this:

while (Access.hasNext()) {
    System.out.println(Access.next());
}

See how that goes.