Reputation: 39
I'm making this class to get current location of the user and compares it to the longitudes and latitudes in the database, it gives me null pointer exception, can someone please help me? I only want to compare with latitudes and I want to get all objects within 50 range.
public class locationFinder extends ListActivity implements LocationListener {
EgyptDataSource datasource;
Location locObject;
String loc;
double lat;
double longi;
List<Integer> indexes;
List<Hotel> newHotels = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LocationManager locManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
Intent i = getIntent();
String category = i.getStringExtra("category");
if(category.equals("hotels"))
{
List<Hotel> Latvalues = datasource.getAllHotelsLat();
float[] distance = new float[Latvalues.size()];
for (int j = 0; j < Latvalues.size(); j++) {
Location.distanceBetween(lat, longi, Latvalues.get(j).toDoubleLat(), 0.0, distance);
if(distance[0] <50)
{
indexes.add(j);
}
}
List<Hotel> allHotels = datasource.getAllHotels();
for (int q = 0; q < allHotels.size(); q++) {
for (int w = 0; w < indexes.size(); w++) {
if(q == indexes.get(w))
{
newHotels.add(allHotels.get(q));
}
}
}
if (newHotels.isEmpty())
{
Toast.makeText(this, "No results found!", Toast.LENGTH_LONG).show();
Intent i1 = new Intent(locationFinder.this, hotelSearch.class);
startActivity(i1);
}
ArrayAdapter<Hotel> adapter = new ArrayAdapter<Hotel>(this, android.R.layout.simple_list_item_1, newHotels);
setListAdapter(adapter);
}
}
public void onLocationChanged(Location location) {
lat = location.getLatitude();
longi = location.getLongitude();
}
public void onProviderDisabled(String provider) {
}
public void onProviderEnabled(String provider) {
}
public void onStatusChanged(String provider, int status, Bundle extras) {
}
}
Here is the LogCat
06-22 18:29:04.261: E/AndroidRuntime(30183): FATAL EXCEPTION: main
06-22 18:29:04.261: E/AndroidRuntime(30183): java.lang.RuntimeException: Unable to start activity ComponentInfo{egypt.interfaceAct/egypt.interfaceAct.locationFinder}: java.lang.NullPointerException
06-22 18:29:04.261: E/AndroidRuntime(30183): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2781)
06-22 18:29:04.261: E/AndroidRuntime(30183): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2797)
06-22 18:29:04.261: E/AndroidRuntime(30183): at android.app.ActivityThread.access$2300(ActivityThread.java:135)
06-22 18:29:04.261: E/AndroidRuntime(30183): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2132)
06-22 18:29:04.261: E/AndroidRuntime(30183): at android.os.Handler.dispatchMessage(Handler.java:99)
06-22 18:29:04.261: E/AndroidRuntime(30183): at android.os.Looper.loop(Looper.java:143)
06-22 18:29:04.261: E/AndroidRuntime(30183): at android.app.ActivityThread.main(ActivityThread.java:4914)
06-22 18:29:04.261: E/AndroidRuntime(30183): at java.lang.reflect.Method.invokeNative(Native Method)
06-22 18:29:04.261: E/AndroidRuntime(30183): at java.lang.reflect.Method.invoke(Method.java:521)
06-22 18:29:04.261: E/AndroidRuntime(30183): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
06-22 18:29:04.261: E/AndroidRuntime(30183): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
06-22 18:29:04.261: E/AndroidRuntime(30183): at dalvik.system.NativeStart.main(Native Method)
06-22 18:29:04.261: E/AndroidRuntime(30183): Caused by: java.lang.NullPointerException
06-22 18:29:04.261: E/AndroidRuntime(30183): at egypt.interfaceAct.locationFinder.onCreate(locationFinder.java:47)
06-22 18:29:04.261: E/AndroidRuntime(30183): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1065)
06-22 18:29:04.261: E/AndroidRuntime(30183): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2745)
06-22 18:29:04.261: E/AndroidRuntime(30183): ... 11 more
Upvotes: 0
Views: 419
Reputation: 185
You can find your error in LogCat by finding row that contains class that you "know". For example in your LogCat there is a row "at egypt.interfaceAct.locationFinder.onCreate(locationFinder.java:47)"
from all the class in LogCat this is the most well known class so you can check the class locationFinder at row 47 and find what wrong with the row 47.
I assume that your List newHotels is still null. so just initialize the newsHotels with
newHotels = new ArrayList<Hotel>();
add this code in onCreate
Upvotes: 1
Reputation: 572
You may also need to check category before using it:
String category = i.getStringExtra("category");
if(category.equals("hotels"))
Notice category may be null, so you better do something like:
if(category != null && category.equals("hotels"))
Upvotes: 1
Reputation: 7184
You have not initialized the newHotels
variable. Add the following line at the beginning of your onCreate
method:
newHotels = new ArrayList<Hotel>();
Upvotes: 2