Note that there are some explanatory texts on larger screens.

plurals
  1. PONullPointerException in the SQLite query and LocationListener
    primarykey
    data
    text
    <p>I want to get the users location and compare it to the locations in my database, this is the code I came up with but it is still not working, it gives me null pointer exception in this line:</p> <pre><code>Cursor cursor = database.query(MySQLiteHelper.TABLE_Hotel, new String[]{MySQLiteHelper.H_ID, MySQLiteHelper.H_Name, MySQLiteHelper.H_Longitude, MySQLiteHelper.H_Latitude}, null, null, null, null, null); </code></pre> <p>Here is the whole class:</p> <pre><code>public class locationFinder extends ListActivity implements LocationListener { List&lt;Hotel&gt; hotelsInRange = new ArrayList&lt;Hotel&gt;(); MySQLiteHelper mySQL; SQLiteDatabase database; EgyptDataSource datasource; Location locObject; String loc; double lat; double longi; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); LocationManager locManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); locManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this); } public void onLocationChanged(Location location) { // TODO Auto-generated method stub setListOfHotelsInRange(location); } private void setListOfHotelsInRange(Location userLocation) { Cursor cursor = database.query(MySQLiteHelper.TABLE_Hotel, new String[]{MySQLiteHelper.H_ID, MySQLiteHelper.H_Name, MySQLiteHelper.H_Longitude, MySQLiteHelper.H_Latitude}, null, null, null, null, null); while(cursor.moveToNext()) { double longi = cursor.getDouble(cursor.getColumnIndex(MySQLiteHelper.H_Longitude)); double lat = cursor.getDouble(cursor.getColumnIndex(MySQLiteHelper.H_Latitude)); Location currentHotelLocation = new Location("Current Hotel"); currentHotelLocation.setLatitude(lat); currentHotelLocation.setLongitude(longi); double distanceInMeters = userLocation.distanceTo(currentHotelLocation); if (distanceInMeters &lt; 500000000) { //hotel is in range Hotel hotel = new Hotel(); hotel.set_id( cursor.getInt(0)); hotel.setName(cursor.getString(0)); hotelsInRange.add(hotel); } } ArrayAdapter&lt;Hotel&gt; adapter = new ArrayAdapter&lt;Hotel&gt;(this, android.R.layout.simple_list_item_1, hotelsInRange); setListAdapter(adapter); } public void onProviderDisabled(String provider) { // TODO Auto-generated method stub } public void onProviderEnabled(String provider) { // TODO Auto-generated method stub } public void onStatusChanged(String provider, int status, Bundle extras) { // TODO Auto-generated method stub } } </code></pre> <p>This is the LogCat:</p> <pre><code>06-22 22:55:32.984: E/AndroidRuntime(2378): FATAL EXCEPTION: main 06-22 22:55:32.984: E/AndroidRuntime(2378): java.lang.NullPointerException 06-22 22:55:32.984: E/AndroidRuntime(2378): at egypt.interfaceAct.locationFinder.setListOfHotelsInRange(locationFinder.java:99) 06-22 22:55:32.984: E/AndroidRuntime(2378): at egypt.interfaceAct.locationFinder.onLocationChanged(locationFinder.java:93) 06-22 22:55:32.984: E/AndroidRuntime(2378): at android.location.LocationManager$ListenerTransport._handleMessage(LocationManager.java:191) 06-22 22:55:32.984: E/AndroidRuntime(2378): at android.location.LocationManager$ListenerTransport.access$000(LocationManager.java:124) 06-22 22:55:32.984: E/AndroidRuntime(2378): at android.location.LocationManager$ListenerTransport$1.handleMessage(LocationManager.java:140) 06-22 22:55:32.984: E/AndroidRuntime(2378): at android.os.Handler.dispatchMessage(Handler.java:99) 06-22 22:55:32.984: E/AndroidRuntime(2378): at android.os.Looper.loop(Looper.java:143) 06-22 22:55:32.984: E/AndroidRuntime(2378): at android.app.ActivityThread.main(ActivityThread.java:4914) 06-22 22:55:32.984: E/AndroidRuntime(2378): at java.lang.reflect.Method.invokeNative(Native Method) 06-22 22:55:32.984: E/AndroidRuntime(2378): at java.lang.reflect.Method.invoke(Method.java:521) 06-22 22:55:32.984: E/AndroidRuntime(2378): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858) 06-22 22:55:32.984: E/AndroidRuntime(2378): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 06-22 22:55:32.984: E/AndroidRuntime(2378): at dalvik.system.NativeStart.main(Native Method) </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload