Reputation: 21
I am having a problem with some Android code. After searching for a way to transform a GeoPoint into a Location I found this question that seems to give me the answer but when I use the information my program fails with a null pointer exception.
My code:
lat = 52.3725979;
longt = 4.8998594;
final GeoPoint geo_1 = new GeoPoint((int) (lat * 1E6),(int) (longt * 1E6));
double latitude = geo_1.getLatitudeE6() / 1E6;
double longitude = geo_1.getLongitudeE6() / 1E6;
endLoc.setLatitude(latitude);
endLoc.setLongitude(longitude);
Line 171:
endLoc.setLatitude(latitude);
Initialization of the endLoc variable:
public class VibroNavActivity extends Activity implements SensorListener{
final static String TAG = "VibroNavActivity";
//location variables
GeoPoint startGeo;
GeoPoint endGeo;
Location loc = null;
Location startLoc;
Location endLoc;
float totalDistance;//the distance between the startpoint end the endpoint
The Log:
05-02 12:40:56.789: W/dalvikvm(27983): threadid=1: thread exiting with uncaught exception (group=0x40a4b1f8)
05-02 12:40:56.800: E/AndroidRuntime(27983): FATAL EXCEPTION: main
05-02 12:40:56.800: E/AndroidRuntime(27983): java.lang.RuntimeException: Unable to start activity ComponentInfo{hcm.haska/hcm.haska.VibroNavActivity}: java.lang.NullPointerException
05-02 12:40:56.800: E/AndroidRuntime(27983): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
05-02 12:40:56.800: E/AndroidRuntime(27983): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
05-02 12:40:56.800: E/AndroidRuntime(27983): at android.app.ActivityThread.access$600(ActivityThread.java:123)
05-02 12:40:56.800: E/AndroidRuntime(27983): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
05-02 12:40:56.800: E/AndroidRuntime(27983): at android.os.Handler.dispatchMessage(Handler.java:99)
05-02 12:40:56.800: E/AndroidRuntime(27983): at android.os.Looper.loop(Looper.java:137)
05-02 12:40:56.800: E/AndroidRuntime(27983): at android.app.ActivityThread.main(ActivityThread.java:4424)
05-02 12:40:56.800: E/AndroidRuntime(27983): at java.lang.reflect.Method.invokeNative(Native Method)
05-02 12:40:56.800: E/AndroidRuntime(27983): at java.lang.reflect.Method.invoke(Method.java:511)
05-02 12:40:56.800: E/AndroidRuntime(27983): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787)
05-02 12:40:56.800: E/AndroidRuntime(27983): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554)
05-02 12:40:56.800: E/AndroidRuntime(27983): at dalvik.system.NativeStart.main(Native Method)
05-02 12:40:56.800: E/AndroidRuntime(27983): Caused by: java.lang.NullPointerException
05-02 12:40:56.800: E/AndroidRuntime(27983): at hcm.haska.VibroNavActivity.onCreate(VibroNavActivity.java:171)
05-02 12:40:56.800: E/AndroidRuntime(27983): at android.app.Activity.performCreate(Activity.java:4465)
05-02 12:40:56.800: E/AndroidRuntime(27983): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
05-02 12:40:56.800: E/AndroidRuntime(27983): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
05-02 12:40:56.800: E/AndroidRuntime(27983): ... 11 more
Upvotes: 2
Views: 338
Reputation: 2625
Location constructor needs provider string but i seen somewhere it can be empty string
loc = new Location("");
Testing it in a moment;
Upvotes: 0
Reputation: 79
You have only declared endLoc
, but the memory won't be allocated at this point. You need to initialize the memory by calling:
endLoc = new Location(....)
Upvotes: 0
Reputation: 46415
You aren't initialising endLoc
you are only declaring it as a variable.
In your constructor, or somewhere lese in a method before line 171 is called, you need endLoc = new Location();
Upvotes: 3