androidobjectnumberformatexceptionobjectinstantiation

NumberFormatException when trying to create an object in Android


I am trying to develop an app for my university project which tracks airplanes that have been picked up by an ADS-B receiver, which is connected to a server which streams the data to my app.

To do this, I've created an "Aircraft" class to keep track of the aircraft's position, identifiable by the unique Mode-S hexadecimal code of each one, and an ArrayList of each aircraft discovered.

This is the code which is supposed to create the Aircraft object:

Aircraft aircraftToAdd = new Aircraft(sbsMessageArray[4], //Mode-S hex code
            sbsMessageArray[10], //callsign
            Integer.parseInt(sbsMessageArray[11]), //altitude
            Integer.parseInt(sbsMessageArray[12]), //ground speed
            Integer.parseInt(sbsMessageArray[13]), //track
            Double.parseDouble(sbsMessageArray[14]), //latitude
            Double.parseDouble(sbsMessageArray[15])); //longitude

Which is then added to the ArrayList like so:

aircraftArrayList.add(aircraftToAdd);

But then, this happens:

02-26 16:09:26.869 14873-14873/com.example.se415017.maynoothskyradar E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                   Process: com.example.se415017.maynoothskyradar, PID: 14873
                                                                                   java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.se415017.maynoothskyradar/com.example.se415017.maynoothskyradar.activities.MainActivity}: java.lang.NumberFormatException: Invalid int: ""
                                                                                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2358)
                                                                                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2420)
                                                                                       at android.app.ActivityThread.access$900(ActivityThread.java:154)
                                                                                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
                                                                                       at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                       at android.os.Looper.loop(Looper.java:135)
                                                                                       at android.app.ActivityThread.main(ActivityThread.java:5294)
                                                                                       at java.lang.reflect.Method.invoke(Native Method)
                                                                                       at java.lang.reflect.Method.invoke(Method.java:372)
                                                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
                                                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)
                                                                                    Caused by: java.lang.NumberFormatException: Invalid int: ""
                                                                                       at java.lang.Integer.invalidInt(Integer.java:138)
                                                                                       at java.lang.Integer.parseInt(Integer.java:358)
                                                                                       at java.lang.Integer.parseInt(Integer.java:334)
                                                                                       at com.example.se415017.maynoothskyradar.activities.MainActivity.addAircraftToList(MainActivity.java:525)
                                                                                       at com.example.se415017.maynoothskyradar.activities.MainActivity.parseSBSMessage(MainActivity.java:516)
                                                                                       at com.example.se415017.maynoothskyradar.activities.MainActivity.readFromTextFile(MainActivity.java:419)
                                                                                       at com.example.se415017.maynoothskyradar.activities.MainActivity.onCreate(MainActivity.java:147)
                                                                                       at android.app.Activity.performCreate(Activity.java:5990)
                                                                                       at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
                                                                                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2311)
                                                                                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2420) 
                                                                                       at android.app.ActivityThread.access$900(ActivityThread.java:154) 
                                                                                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321) 
                                                                                       at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                       at android.os.Looper.loop(Looper.java:135) 
                                                                                       at android.app.ActivityThread.main(ActivityThread.java:5294) 
                                                                                       at java.lang.reflect.Method.invoke(Native Method) 
                                                                                       at java.lang.reflect.Method.invoke(Method.java:372) 
                                                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904) 
                                                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699) 

I'm just fed up with these errors. I even tried replacing sbsMessageArray[4] by creating a String from it and inserting that in its place, but that didn't help.


Solution

  • As Vasily Kabunov said in the comment, one (or all) of the strings passed into Integer.parseInt() is (are) empty.

    Integer.parseInt("") will throw an exception since there is nothing to parse into an integer. Check your data source or do an empty string test using yourstring.equals("").