0

I have a file with gps coords in every line:

9.34669876098644,48.2405319213867
9.36384963989269,48.2296714782715
9.3639497756958,48.2259712219238
9.36956977844238,48.2260589599609
9.36404991149908,48.2222709655763
9.36975955963135,48.2186508178712

now I want to parse the file and make geo points. First I open the file and read the line, than I split the line and want to make the string to an int. But than i got an NullPointerException after Log.d("4:",point_t[0] + " - " + point_t[1]);

try{
    InputStream koord = getAssets().open("bib.txt");
    if (koord != null) {
        InputStreamReader input = new InputStreamReader(koord);
        BufferedReader buffreader = new BufferedReader(input);
        Log.d("2: ", "Read File");
        String line;
        while (( line = buffreader.readLine()) != null) {
            Log.d("3: ", line);
            String[] point_t = line.split(",");
            Log.d("4:",point_t[0] + " - " + point_t[1]);
            int x = Integer.parseInt(point_t[0]);
            int y = Integer.parseInt(point_t[1]);
            Log.d("Geopoint:",x + " - " + y);
            points.add(new GeoPoint((int)(x*1e6), (int)(y*1e6)));
            Log.d("5:","saved");
        }
        koord.close();
        polygon = new Polygon(points);
    }
}catch (Exception e) {
    // TODO: handle exception
}  

06-20 08:45:21.701: E/ActivityThread(231): Failed to find provider info for com.google.settings
06-20 08:45:21.711: E/ActivityThread(231): Failed to find provider info for com.google.settings
06-20 08:45:21.721: E/ActivityThread(231): Failed to find provider info for com.google.settings
06-20 08:45:21.971: D/dalvikvm(231): GC freed 3008 objects / 190136 bytes in 60ms
06-20 08:45:22.071: D/Checkpoint:(231): Start ArrayList
06-20 08:45:22.081: I/global(231): Default buffer size used in BufferedReader constructor. It would be better to be explicit if an 8k-char buffer is required.
06-20 08:45:22.081: D/Checkpoint:(231): Read File
06-20 08:45:22.081: D/Checkpoint:(231): 9.34669876098644,48.2405319213867
06-20 08:45:22.081: D/Point:(231): 9.34669876098644 - 48.2405319213867
06-20 08:45:22.161: I/MapActivity(231): Handling network change notification:CONNECTED
06-20 08:45:22.171: E/MapActivity(231): Couldn't get connection factory client
06-20 08:45:22.381: D/dalvikvm(231): GC freed 983 objects / 342808 bytes in 62ms
06-20 08:45:22.391: D/AndroidRuntime(231): Shutting down VM
06-20 08:45:22.401: W/dalvikvm(231): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
06-20 08:45:22.401: E/AndroidRuntime(231): Uncaught handler: thread main exiting due to uncaught exception
06-20 08:45:22.425: E/AndroidRuntime(231): java.lang.NullPointerException
06-20 08:45:22.425: E/AndroidRuntime(231):  at com.google.android.maps.OverlayBundle.draw(OverlayBundle.java:42)
06-20 08:45:22.425: E/AndroidRuntime(231):  at com.google.android.maps.MapView.onDraw(MapView.java:476)
06-20 08:45:22.425: E/AndroidRuntime(231):  at android.view.View.draw(View.java:6535)
06-20 08:45:22.425: E/AndroidRuntime(231):  at android.view.ViewGroup.drawChild(ViewGroup.java:1531)
06-20 08:45:22.425: E/AndroidRuntime(231):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258)
06-20 08:45:22.425: E/AndroidRuntime(231):  at android.view.ViewGroup.drawChild(ViewGroup.java:1529)
06-20 08:45:22.425: E/AndroidRuntime(231):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258)
06-20 08:45:22.425: E/AndroidRuntime(231):  at android.view.View.draw(View.java:6641)
06-20 08:45:22.425: E/AndroidRuntime(231):  at android.widget.FrameLayout.draw(FrameLayout.java:352)
06-20 08:45:22.425: E/AndroidRuntime(231):  at android.view.ViewGroup.drawChild(ViewGroup.java:1531)
06-20 08:45:22.425: E/AndroidRuntime(231):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258)
06-20 08:45:22.425: E/AndroidRuntime(231):  at android.view.View.draw(View.java:6538)
06-20 08:45:22.425: E/AndroidRuntime(231):  at android.widget.FrameLayout.draw(FrameLayout.java:352)
06-20 08:45:22.425: E/AndroidRuntime(231):  at android.view.ViewGroup.drawChild(ViewGroup.java:1531)
06-20 08:45:22.425: E/AndroidRuntime(231):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258)
06-20 08:45:22.425: E/AndroidRuntime(231):  at android.view.ViewGroup.drawChild(ViewGroup.java:1529)
06-20 08:45:22.425: E/AndroidRuntime(231):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258)
06-20 08:45:22.425: E/AndroidRuntime(231):  at android.view.View.draw(View.java:6538)
06-20 08:45:22.425: E/AndroidRuntime(231):  at android.widget.FrameLayout.draw(FrameLayout.java:352)
06-20 08:45:22.425: E/AndroidRuntime(231):  at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:1830)
06-20 08:45:22.425: E/AndroidRuntime(231):  at android.view.ViewRoot.draw(ViewRoot.java:1349)
06-20 08:45:22.425: E/AndroidRuntime(231):  at android.view.ViewRoot.performTraversals(ViewRoot.java:1114)
06-20 08:45:22.425: E/AndroidRuntime(231):  at android.view.ViewRoot.handleMessage(ViewRoot.java:1633)
06-20 08:45:22.425: E/AndroidRuntime(231):  at android.os.Handler.dispatchMessage(Handler.java:99)
06-20 08:45:22.425: E/AndroidRuntime(231):  at android.os.Looper.loop(Looper.java:123)
06-20 08:45:22.425: E/AndroidRuntime(231):  at android.app.ActivityThread.main(ActivityThread.java:4363)
06-20 08:45:22.425: E/AndroidRuntime(231):  at java.lang.reflect.Method.invokeNative(Native Method)
06-20 08:45:22.425: E/AndroidRuntime(231):  at java.lang.reflect.Method.invoke(Method.java:521)
06-20 08:45:22.425: E/AndroidRuntime(231):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
06-20 08:45:22.425: E/AndroidRuntime(231):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
06-20 08:45:22.425: E/AndroidRuntime(231):  at dalvik.system.NativeStart.main(Native Method)
06-20 08:45:22.442: I/dalvikvm(231): threadid=7: reacting to signal 3
06-20 08:45:22.461: I/dalvikvm(231): Wrote stack trace to '/data/anr/traces.txt'
06-20 08:45:24.701: I/Process(231): Sending signal. PID: 231 SIG: 9

@stuckless

Log.d("1: ", "Start ArrayList");
ArrayList<GeoPoint> points = new ArrayList<GeoPoint>();
try{
    InputStream koord = getAssets().open("bib.txt");
    if (koord != null) {
        InputStreamReader input = new InputStreamReader(koord);
        BufferedReader buffreader = new BufferedReader(input);
        Log.d("2: ", "Read File");
        String line;
        while (( line = buffreader.readLine()) != null) {
            Log.d("3: ", line);
            String[] point_t = line.split(",");
            Log.d("4:",point_t[0] + " - " + point_t[1]);
            double x = Double.parseDouble(point_t[0]);
            double y = Double.parseDouble(point_t[1]);
            Log.d("Geopoint:",x + " - " + y);
            points.add(new GeoPoint((int)(x*1e6), (int)(y*1e6)));
            Log.d("5:","saved");
        }
        koord.close();
        polygon = new Polygon(points);
    }
}catch (Exception e) {
    Log.e("APP","Failed", e);
    }        
    mapView.getOverlays().clear();
    mapView.getOverlays().add(polygon);
    mapView.invalidate();
}

Error:

06-20 09:47:27.382: E/ActivityThread(324): Failed to find provider info for com.google.settings
06-20 09:47:27.382: E/ActivityThread(324): Failed to find provider info for com.google.settings
06-20 09:47:27.392: E/ActivityThread(324): Failed to find provider info for com.google.settings
06-20 09:47:27.702: D/dalvikvm(324): GC freed 3148 objects / 195472 bytes in 68ms
06-20 09:47:27.822: D/1:(324): Start ArrayList
06-20 09:47:27.822: I/global(324): Default buffer size used in BufferedReader constructor. It would be better to be explicit if an 8k-char buffer is required.
06-20 09:47:27.822: D/2:(324): Read File
06-20 09:47:27.822: D/3:(324): 9.34669876098644,48.2405319213867
06-20 09:47:27.822: D/4:(324): 9.34669876098644 - 48.2405319213867
06-20 09:47:27.952: E/APP(324): Failed
06-20 09:47:27.952: E/APP(324): java.lang.NumberFormatException: 
06-20 09:47:27.952: E/APP(324):     at org.apache.harmony.luni.util.FloatingPointParser.parseDblImpl(Native Method)
06-20 09:47:27.952: E/APP(324):     at org.apache.harmony.luni.util.FloatingPointParser.parseDouble(FloatingPointParser.java:283)
06-20 09:47:27.952: E/APP(324):     at java.lang.Double.parseDouble(Double.java:285)
06-20 09:47:27.952: E/APP(324):     at de.bodprod.rettinfo.BOSLstItemDetail.onCreate(BOSLstItemDetail.java:93)
06-20 09:47:27.952: E/APP(324):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
06-20 09:47:27.952: E/APP(324):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
06-20 09:47:27.952: E/APP(324):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
06-20 09:47:27.952: E/APP(324):     at android.app.ActivityThread.access$2200(ActivityThread.java:119)
06-20 09:47:27.952: E/APP(324):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
06-20 09:47:27.952: E/APP(324):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-20 09:47:27.952: E/APP(324):     at android.os.Looper.loop(Looper.java:123)
06-20 09:47:27.952: E/APP(324):     at android.app.ActivityThread.main(ActivityThread.java:4363)
06-20 09:47:27.952: E/APP(324):     at java.lang.reflect.Method.invokeNative(Native Method)
06-20 09:47:27.952: E/APP(324):     at java.lang.reflect.Method.invoke(Method.java:521)
06-20 09:47:27.952: E/APP(324):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
06-20 09:47:27.952: E/APP(324):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
06-20 09:47:27.952: E/APP(324):     at dalvik.system.NativeStart.main(Native Method)
06-20 09:47:28.043: I/MapActivity(324): Handling network change notification:CONNECTED
06-20 09:47:28.052: E/MapActivity(324): Couldn't get connection factory client
06-20 09:47:28.272: D/dalvikvm(324): GC freed 1440 objects / 382480 bytes in 66ms
06-20 09:47:28.292: D/AndroidRuntime(324): Shutting down VM
06-20 09:47:28.292: W/dalvikvm(324): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
06-20 09:47:28.292: E/AndroidRuntime(324): Uncaught handler: thread main exiting due to uncaught exception
06-20 09:47:28.302: E/AndroidRuntime(324): java.lang.NullPointerException
06-20 09:47:28.302: E/AndroidRuntime(324):  at com.google.android.maps.OverlayBundle.draw(OverlayBundle.java:42)
06-20 09:47:28.302: E/AndroidRuntime(324):  at com.google.android.maps.MapView.onDraw(MapView.java:476)
06-20 09:47:28.302: E/AndroidRuntime(324):  at android.view.View.draw(View.java:6535)
06-20 09:47:28.302: E/AndroidRuntime(324):  at android.view.ViewGroup.drawChild(ViewGroup.java:1531)
06-20 09:47:28.302: E/AndroidRuntime(324):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258)
06-20 09:47:28.302: E/AndroidRuntime(324):  at android.view.ViewGroup.drawChild(ViewGroup.java:1529)
06-20 09:47:28.302: E/AndroidRuntime(324):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258)
06-20 09:47:28.302: E/AndroidRuntime(324):  at android.view.View.draw(View.java:6641)
06-20 09:47:28.302: E/AndroidRuntime(324):  at android.widget.FrameLayout.draw(FrameLayout.java:352)
06-20 09:47:28.302: E/AndroidRuntime(324):  at android.view.ViewGroup.drawChild(ViewGroup.java:1531)
06-20 09:47:28.302: E/AndroidRuntime(324):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258)
06-20 09:47:28.302: E/AndroidRuntime(324):  at android.view.View.draw(View.java:6538)
06-20 09:47:28.302: E/AndroidRuntime(324):  at android.widget.FrameLayout.draw(FrameLayout.java:352)
06-20 09:47:28.302: E/AndroidRuntime(324):  at android.view.ViewGroup.drawChild(ViewGroup.java:1531)
06-20 09:47:28.302: E/AndroidRuntime(324):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258)
06-20 09:47:28.302: E/AndroidRuntime(324):  at android.view.ViewGroup.drawChild(ViewGroup.java:1529)
06-20 09:47:28.302: E/AndroidRuntime(324):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258)
06-20 09:47:28.302: E/AndroidRuntime(324):  at android.view.View.draw(View.java:6538)
06-20 09:47:28.302: E/AndroidRuntime(324):  at android.widget.FrameLayout.draw(FrameLayout.java:352)
06-20 09:47:28.302: E/AndroidRuntime(324):  at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:1830)
06-20 09:47:28.302: E/AndroidRuntime(324):  at android.view.ViewRoot.draw(ViewRoot.java:1349)
06-20 09:47:28.302: E/AndroidRuntime(324):  at android.view.ViewRoot.performTraversals(ViewRoot.java:1114)
06-20 09:47:28.302: E/AndroidRuntime(324):  at android.view.ViewRoot.handleMessage(ViewRoot.java:1633)
06-20 09:47:28.302: E/AndroidRuntime(324):  at android.os.Handler.dispatchMessage(Handler.java:99)
06-20 09:47:28.302: E/AndroidRuntime(324):  at android.os.Looper.loop(Looper.java:123)
06-20 09:47:28.302: E/AndroidRuntime(324):  at android.app.ActivityThread.main(ActivityThread.java:4363)
06-20 09:47:28.302: E/AndroidRuntime(324):  at java.lang.reflect.Method.invokeNative(Native Method)
06-20 09:47:28.302: E/AndroidRuntime(324):  at java.lang.reflect.Method.invoke(Method.java:521)
06-20 09:47:28.302: E/AndroidRuntime(324):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
06-20 09:47:28.302: E/AndroidRuntime(324):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
06-20 09:47:28.302: E/AndroidRuntime(324):  at dalvik.system.NativeStart.main(Native Method)
06-20 09:47:28.323: I/dalvikvm(324): threadid=7: reacting to signal 3

Solution:

Now it works, i create the file with the coords in eclipse new and use this now:

ArrayList<GeoPoint> points = new ArrayList<GeoPoint>();
try{
    InputStream koord = getAssets().open("biberach");
    if (koord != null) {
        InputStreamReader input = new InputStreamReader(koord);
        BufferedReader buffreader = new BufferedReader(input);
        String line;
        while (( line = buffreader.readLine()) != null) {
            String[] point_t = line.split(",");
                double y = Double.parseDouble(point_t[0]);
                double x = Double.parseDouble(point_t[1]);
                points.add(new GeoPoint((int)(x*1e6), (int)(y*1e6)));
        }
        koord.close();
        polygon = new Polygon(points);
    }
}catch (Exception e) {
    Log.e("APP","Failed", e);
} 

Was an error with the file

Laire
  • 1,290
  • 6
  • 22
  • 49

3 Answers3

1

Since you are catching and ignoring any exceptions in that block, try logging out the exception in the empty catch area, and you'll see that you are getting a NumberFormatException

Exception in thread "main" java.lang.NumberFormatException: For input string: "9.34669876098644"
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
    at java.lang.Integer.parseInt(Integer.java:481)
    at java.lang.Integer.parseInt(Integer.java:514)
    at TestParseInt.main(TestParseInt.java:4)

You cannot parseInt on a double number (and even it did work, you'd only end up with the integer portion, which is not what you want). Try using Double.parseDouble() instead.

In other words

        double x = Double.parseDouble(point_t[0].trim());
        double y = Double.parseDouble(point_t[1].trim());

and instead of this

}catch (Exception e) {
    // TODO: handle exception
}

try using

}catch (Exception e) {
    Log.e("APP","Failed", e);
}

UPDATE1: Maybe ensure that you've removed whitespace so use split("\\s*,\\s*) and then .trim() on the strings. Double.parseDouble() should allow for whitespace, though.

UPDATE2: In your locale, is the decimal a valid separator, or is a comma? If so, then you may have to use DecimalFormat to parse the values and force a US locale

I see from your profile that you are in Germany, but the numbers you are parsing are not in German. So, try this..

DecimalFormat parser = (DecimalFormat)DecimalFormat.getInstance(Locale.US);
double x = (Double)parser.parse("48.2405319213867");

This forces the parser to use a US locale for parsing the number (ie, . is decimal).

stuckless
  • 6,515
  • 2
  • 19
  • 27
  • +1 well taken point ......even I think where the flow went after printing D/Point:(231): 9.34669876098644 - 48.2405319213867 – Dheeresh Singh Jun 20 '12 at 09:30
  • @DheereshSingh : can u see my answer bez i think he is spiting string in wrong way – ρяσѕρєя K Jun 20 '12 at 09:39
  • yes Imran, but I think here Laire performing operation on each line get from buffreader ...... and the flow is also going to Log.d("4:",point_t[0] + " - " + point_t[1]); but not to Log.d("Geopoint:",x + " - " + y); as per log cat so issue should be in between these lines..... – Dheeresh Singh Jun 20 '12 at 09:44
  • The Double values look good, and I parsed them on my device (just to make sure), so I'm at a loss. You could try changing your split and parse statements to remove whitespace and see if that helps. (I updated the answer to include this) – stuckless Jun 20 '12 at 10:20
  • In your locale, is a decimal a valid numeric placeholder for the "decimal" or it is a comma. ie, would a typical double look like, "9.5" or "9,5", if it's the latter, then check out DecimalFormat (I've updated the answer to reflect this) – stuckless Jun 20 '12 at 10:28
  • I don't know what i can do more... I used line.split("\\s*,\\s*"); I changed . into , I trim but I still get the error java.lang.NumberFormat.Exception – Laire Jun 20 '12 at 13:08
  • Can you try to parse using the DecimalFormat class. (I added a sample to the answer). Since you are in Germany, and the numbers you are parsing are in US (general statement) then that may be the issue. – stuckless Jun 20 '12 at 13:38
  • That's great... but since the "fix" was the first part of my answer (ie, use Double.parseDouble), why did you accept the other answer that was simply a duplicate of mine (but was provided later??) – stuckless Jun 20 '12 at 13:49
1

You are doing wrong.as in your text file string first spilt it on the bases of space and then bases of " ; " as:

    String line="";
    while (( line = buffreader.readLine()) != null) {
        Log.d("3: ", line);
        line +=line;
    }
    String[] point_arr = line.split(" ");
    for (int i = 0; i <= point_arr.Length - 1; i++)
    {
        String[] point_t = point_arr[i].split(",");
        Log.d("4:",point_t[0] + " - " + point_t[1]);
        int x = Integer.parseInt(point_t[0]);
        int y = Integer.parseInt(point_t[1]);
        Log.d("Geopoint:",x + " - " + y);
        points.add(new GeoPoint((int)(x*1e6), (int)(y*1e6)));
        Log.d("5:","saved");
    }

EDIT: or it maybe possible first split your string on the baes of " ; " and then bases of space as you put in file

ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
0

First, you have to get all lines String[]. Then,as you see, you have to split by " " (space) to get two coords and THEN those got strings split again by "," to get logtitude and latitute. So with one line you get two coords, not one

Also, this part

        int x = Integer.parseInt(point_t[0]);
        int y = Integer.parseInt(point_t[1]);
        Log.d("Geopoint:",x + " - " + y);
        points.add(new GeoPoint((int)(x*1e6), (int)(y*1e6)));

is not good, it should be:

        double x = Double.parseDouble(point_t[0]);
        double y = Double.parseDouble(point_t[1]);
        Log.d("Geopoint:",x + " - " + y);
        points.add(new GeoPoint((int)(x*1e6), (int)(y*1e6)));

because, when you have 20.123456 number, you get 20000000, while my method gets 20123456

also, before catch, add one more catch

catch(NumberFormatException e) {
    //this occurs when can't convert String to specified number format, so it means, that you give bad number to parse
}

It doesn't answer your exact question, but it helps you with your work

Paulius Vindzigelskis
  • 2,121
  • 6
  • 29
  • 41