Note that there are some explanatory texts on larger screens.

plurals
  1. PONullPointerException - Database Cursor in Android - Workaround
    primarykey
    data
    text
    <p>I'm having an issues with a null pointer exception that I'm trying debug/work around in the following code. I'm trying to initialize the waypointCursor to null and I need to check if it is equal to null later in the code. Do this though is giving me a NullPointerExeception. I need help trying to work around it. Here is the following code and details.</p> <p>Here is the code that is calling the method:</p> <pre><code>private void displayCursor(Cursor tracksCursor) { UnitsI18n mUnits; mUnits = new UnitsI18n( this, new UnitsI18n.UnitsChangeListener() { @Override public void onUnitsChange() { //Do Nothing } } ); Log.i(TAG, "TRACKS CONTENT URI:"+Tracks.CONTENT_URI); StatisticsCalculator stats = new StatisticsCalculator(this, mUnits, this); //UNCOMMENT THIS ONCE I FIGURE OUT WHAT IS GOING ON if (Tracks.CONTENT_URI != null) { stats.updateCalculations(Tracks.CONTENT_URI); //stats.updateCalculations(Uri.withAppendedPath(Tracks.CONTENT_URI, TRACKURI)); } </code></pre> <p>Here is the code in StatisticsCalculator that is where the NullPointerException is happening:</p> <pre><code>public void updateCalculations( Uri trackUri ) { mStarttime = -1; mEndtime = -1; mMaxSpeed = 0; mAverageActiveSpeed = 0; mMaxAltitude = 0; mMinAltitude = 0; mAscension = 0; mDistanceTraveled = 0f; mDuration = 0; long duration = 1; double ascension = 0; ContentResolver resolver = mContext.getContentResolver(); Cursor waypointsCursor = null; try { waypointsCursor = resolver.query( Uri.withAppendedPath( trackUri, "waypoints" ), new String[] { "max (" + Waypoints.TABLE + "." + Waypoints.SPEED + ")" , "max (" + Waypoints.TABLE + "." + Waypoints.ALTITUDE + ")" , "min (" + Waypoints.TABLE + "." + Waypoints.ALTITUDE + ")" , "count(" + Waypoints.TABLE + "." + Waypoints._ID + ")" }, null, null, null ); if( waypointsCursor.moveToLast() ) { mMaxSpeed = waypointsCursor.getDouble( 0 ); mMaxAltitude = waypointsCursor.getDouble( 1 ); mMinAltitude = waypointsCursor.getDouble( 2 ); long nrWaypoints = waypointsCursor.getLong( 3 ); waypointsText = nrWaypoints + ""; } waypointsCursor.close(); waypointsCursor = resolver.query( Uri.withAppendedPath( trackUri, "waypoints" ), new String[] { "avg (" + Waypoints.TABLE + "." + Waypoints.SPEED + ")" }, Waypoints.TABLE + "." + Waypoints.SPEED +" &gt; ?", new String[] { ""+Constants.MIN_STATISTICS_SPEED }, null ); if( waypointsCursor.moveToLast() ) { mAverageActiveSpeed = waypointsCursor.getDouble( 0 ); } } finally { if( waypointsCursor != null ) { waypointsCursor.close(); } } </code></pre> <p>The line of code in the StatisticsCalculator class(line 75) is this line of code:</p> <pre><code>if( waypointsCursor.moveToLast() ) </code></pre> <p>It doesn't like the waypointsCursor being set equal to null on this line:</p> <pre><code>Cursor waypointsCursor = null; </code></pre> <p>Right above the try statement above. I do need to have it set to null though so I can check if waypointsCursor is equal to null on this line: if( waypointsCursor != null ) in the finally statement.</p> <p>Here is the logcat output: </p> <pre><code>04-12 18:29:16.795: I/OGT.TrackList(6115): TRACKS CONTENT URI:content://com.android.gpstracker/tracks 04-12 18:29:16.795: E/OGT.GPStrackingProvider(6115): Unable to come to an action in the query uri: content://com.android.gpstracker/tracks/waypoints 04-12 18:29:16.795: D/AndroidRuntime(6115): Shutting down VM 04-12 18:29:16.807: W/dalvikvm(6115): threadid=1: thread exiting with uncaught exception (group=0x40a71930) 04-12 18:29:16.915: E/AndroidRuntime(6115): FATAL EXCEPTION: main 04-12 18:29:16.915: E/AndroidRuntime(6115): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.gpstracker/com.polaris.epicriders.Rides.RidesActivity}: java.lang.NullPointerException 04-12 18:29:16.915: E/AndroidRuntime(6115): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180) 04-12 18:29:16.915: E/AndroidRuntime(6115): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) 04-12 18:29:16.915: E/AndroidRuntime(6115): at android.app.ActivityThread.access$600(ActivityThread.java:141) 04-12 18:29:16.915: E/AndroidRuntime(6115): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) 04-12 18:29:16.915: E/AndroidRuntime(6115): at android.os.Handler.dispatchMessage(Handler.java:99) 04-12 18:29:16.915: E/AndroidRuntime(6115): at android.os.Looper.loop(Looper.java:137) 04-12 18:29:16.915: E/AndroidRuntime(6115): at android.app.ActivityThread.main(ActivityThread.java:5041) 04-12 18:29:16.915: E/AndroidRuntime(6115): at java.lang.reflect.Method.invokeNative(Native Method) 04-12 18:29:16.915: E/AndroidRuntime(6115): at java.lang.reflect.Method.invoke(Method.java:511) 04-12 18:29:16.915: E/AndroidRuntime(6115): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 04-12 18:29:16.915: E/AndroidRuntime(6115): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 04-12 18:29:16.915: E/AndroidRuntime(6115): at dalvik.system.NativeStart.main(Native Method) 04-12 18:29:16.915: E/AndroidRuntime(6115): Caused by: java.lang.NullPointerException 04-12 18:29:16.915: E/AndroidRuntime(6115): at com.android.gpstracker.actions.utils.StatisticsCalculator.updateCalculations(StatisticsCalculator.java:75) 04-12 18:29:16.915: E/AndroidRuntime(6115): at com.polaris.epicriders.Rides.RidesActivity.displayCursor(RidesActivity.java:901) 04-12 18:29:16.915: E/AndroidRuntime(6115): at com.polaris.epicriders.Rides.RidesActivity.displayIntent(RidesActivity.java:880) 04-12 18:29:16.915: E/AndroidRuntime(6115): at com.polaris.epicriders.Rides.RidesActivity.access$6(RidesActivity.java:832) 04-12 18:29:16.915: E/AndroidRuntime(6115): at com.polaris.epicriders.Rides.RidesActivity$8.onCheckedChanged(RidesActivity.java:198) 04-12 18:29:16.915: E/AndroidRuntime(6115): at android.widget.RadioGroup.setCheckedId(RadioGroup.java:174) 04-12 18:29:16.915: E/AndroidRuntime(6115): at android.widget.RadioGroup.access$600(RadioGroup.java:54) 04-12 18:29:16.915: E/AndroidRuntime(6115): at android.widget.RadioGroup$CheckedStateTracker.onCheckedChanged(RadioGroup.java:358) 04-12 18:29:16.915: E/AndroidRuntime(6115): at android.widget.CompoundButton.setChecked(CompoundButton.java:129) 04-12 18:29:16.915: E/AndroidRuntime(6115): at com.polaris.epicriders.Rides.RidesActivity.onCreate(RidesActivity.java:222) 04-12 18:29:16.915: E/AndroidRuntime(6115): at android.app.Activity.performCreate(Activity.java:5104) 04-12 18:29:16.915: E/AndroidRuntime(6115): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080) 04-12 18:29:16.915: E/AndroidRuntime(6115): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144) </code></pre> <p>Any help would be appreciated.</p>
    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