null pointer exception in GeoPoint on android
in my app i am storing latitude and longitude in a List as GeoPoint as follows:
List<Ge开发者_如何学CoPoint>geo;
here i convert latitude and longitude into GeoPoint and store in list.
GeoPoint tmp;
new GeoPoint((int) ( 9.909228086471558 * 1E6), (int) ( 78.10081958770752 * 1E6));
geo.add(tmp); // i get null pointer exception . Line no:42
Logcat
05-30 18:33:25.203: ERROR/AndroidRuntime(6363): Caused by: java.lang.NullPointerException
05-30 18:33:25.203: ERROR/AndroidRuntime(6363): at net.learn2develop.GoogleMaps.main.onCreate(main.java:42)
05-30 18:33:25.203: ERROR/AndroidRuntime(6363): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1132)
05-30 18:33:25.203: ERROR/AndroidRuntime(6363): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2231)
how to clear the null pointer exception. please help me.
// if you don't do this, it could cause your NPE
List<GeoPoint>geo = new ArrayList<GeoPoint>();
Also, this looks better this way:
GeoPoint tmp = new GeoPoint((int) ( 9.909228086471558 * 1E6), (int) ( 78.10081958770752 * 1E6));
geo.add(tmp);
Instead of using .iterator()
this is recomended:
for(GeoPoint point : geo){
// use the `point` object as you would in a interator
}
精彩评论