0

I'm trying to get an activity retrieve data from a content provider and then update my parent view but for some reason this line

if (c.moveToFirst()== false)

Is throwing a NullPointerException Whenever I'm trying to run the app. Although I have added an assertation to the cursor object before initializing

 Cursor c = cr.query(EarthquakeProvider.CONTENT_URI, null, null, null, null, null);
            assert c != null;

Yet my android Debugger identifies it as a Null value. Here's the full method..

    @TargetApi(16)
    private void loadQuakesFromProvider() {
// Clear the existing earthquake array
        quakeArrayList.clear();
        ContentResolver cr = getContentResolver();
        // Return all the saved earthquakes
        Cursor c = cr.query(EarthquakeProvider.CONTENT_URI, null, null, null, null, null);
        assert c != null;
        if (c.moveToFirst()== false) {
            do {
                // Extract the quake details.
                Long datems = c.getLong(EarthquakeProvider.DATE_COLUMN);
                String details;
                details = c.getString(EarthquakeProvider.DETAILS_COLUMN);
                Float lat = c.getFloat(EarthquakeProvider.LATITUDE_COLUMN);
                Float lng = c.getFloat(EarthquakeProvider.LONGITUDE_COLUMN);
                Double mag = c.getDouble(EarthquakeProvider.MAGNITUDE_COLUMN);
                String link = c.getString(EarthquakeProvider.LINK_COLUMN);
                Location location = new Location("dummy");
                location.setLongitude(lng);
                location.setLatitude(lat);
                Date date = new Date(datems);
                Quake q = new Quake(date, details, location, mag, link);
                addQuakeToArray(q);
            } while (c.moveToNext());
        }
        c.close();
    }

Also here is the stack-trace of the problem

07-20 17:46:23.526 20526-20526/com.quakes.user.mapquake E/ActivityThread: Failed to find provider info for com.paad.provider.earthquake
    07-20 17:46:23.526 20526-20526/com.quakes.user.mapquake E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.quakes.user.mapquake, PID: 20526
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.quakes.user.mapquake/com.quakes.user.mapquake.MainActivity}: java.lang.NullPointerException
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2200)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2249)
    at android.app.ActivityThread.access$800(ActivityThread.java:141)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1212)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:136)
    at android.app.ActivityThread.main(ActivityThread.java:5113)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609)
    at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.NullPointerException
    at com.quakes.user.mapquake.MainActivity.loadQuakesFromProvider(MainActivity.java:331)
    at com.quakes.user.mapquake.MainActivity.onCreate(MainActivity.java:105)
    at android.app.Activity.performCreate(Activity.java:5248)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2164)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2249) 
    at android.app.ActivityThread.access$800(ActivityThread.java:141) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1212) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:136) 
    at android.app.ActivityThread.main(ActivityThread.java:5113) 
    at java.lang.reflect.Method.invokeNative(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:515) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609) 
    at dalvik.system.NativeStart.main(Native Method) 
    07-20 17:46:23.556 20526-20542/com.quakes.user.mapquake W/System.err: java.net.UnknownHostException: Unable to resolve host "earthquake.usgs.gov": No address associated with hostname
    07-20 17:46:23.556 20526-20542/com.quakes.user.mapquake W/System.err:     at java.net.InetAddress.lookupHostByName(InetAddress.java:424)
    07-20 17:46:23.556 20526-20542/com.quakes.user.mapquake W/System.err:     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
    07-20 17:46:23.556 20526-20542/com.quakes.user.mapquake W/System.err:     at java.net.InetAddress.getAllByName(InetAddress.java:214)
    07-20 17:46:23.556 20526-20542/com.quakes.user.mapquake W/System.err:     at com.android.okhttp.internal.Dns$1.getAllByName(Dns.java:28)
    07-20 17:46:23.556 20526-20542/com.quakes.user.mapquake W/System.err:     at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:216)
    07-20 17:46:23.556 20526-20542/com.quakes.user.mapquake W/System.err:     at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:122)
    07-20 17:46:23.556 20526-20542/com.quakes.user.mapquake W/System.err:     at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:292)
    07-20 17:46:23.556 20526-20542/com.quakes.user.mapquake W/System.err:     at com.android.okhttp.internal.http.HttpEngine.sendSocketRequest(HttpEngine.java:255)
    07-20 17:46:23.556 20526-20542/com.quakes.user.mapquake W/System.err:     at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:206)
    07-20 17:46:23.556 20526-20542/com.quakes.user.mapquake W/System.err:     at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:345)
    07-20 17:46:23.556 20526-20542/com.quakes.user.mapquake W/System.err:     at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:296)
    07-20 17:46:23.556 20526-20542/com.quakes.user.mapquake W/System.err:     at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:503)
    07-20 17:46:23.556 20526-20542/com.quakes.user.mapquake W/System.err:     at com.quakes.user.mapquake.MainActivity$retrieveEarthquakeData.doInBackground(MainActivity.java:211)
    07-20 17:46:23.556 20526-20542/com.quakes.user.mapquake W/System.err:     at com.quakes.user.mapquake.MainActivity$retrieveEarthquakeData.doInBackground(MainActivity.java:197)
    07-20 17:46:23.556 20526-20542/com.quakes.user.mapquake W/System.err:     at android.os.AsyncTask$2.call(AsyncTask.java:288)
    07-20 17:46:23.556 20526-20542/com.quakes.user.mapquake W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
    07-20 17:46:23.556 20526-20542/com.quakes.user.mapquake W/System.err:     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
    07-20 17:46:23.556 20526-20542/com.quakes.user.mapquake W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
    07-20 17:46:23.556 20526-20542/com.quakes.user.mapquake W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
    07-20 17:46:23.556 20526-20542/com.quakes.user.mapquake W/System.err:     at java.lang.Thread.run(Thread.java:841)
    07-20 17:46:23.556 20526-20542/com.quakes.user.mapquake W/System.err: Caused by: libcore.io.GaiException: getaddrinfo failed: EAI_NODATA (No address associated with hostname)
    07-20 17:46:23.556 20526-20542/com.quakes.user.mapquake W/System.err:     at libcore.io.Posix.getaddrinfo(Native Method)
    07-20 17:46:23.556 20526-20542/com.quakes.user.mapquake W/System.err:     at libcore.io.ForwardingOs.getaddrinfo(ForwardingOs.java:61)
    07-20 17:46:23.556 20526-20542/com.quakes.user.mapquake W/System.err:     at java.net.InetAddress.lookupHostByName(InetAddress.java:405)
    07-20 17:46:23.556 20526-20542/com.quakes.user.mapquake W/System.err:   ... 19 more
LaurentY
  • 7,495
  • 3
  • 37
  • 55

1 Answers1

0

First, check that you have the Assert function correclt imported

import static junit.framework.Assert.*;

You can always use if(c != null){...} else {Log.e("src","msg")} instead, having the same overall efficiency.

Also note that your earthquake.usgs.gov results in:

java.net.UnknownHostException: Unable to resolve host "earthquake.usgs.gov": No address associated with hostname

Use the full URL instead.

Community
  • 1
  • 1
Bonatti
  • 2,778
  • 5
  • 23
  • 42
  • why do you think I should use a jUnit assertation? i'm not runniing a unit test.. – Anirudh Srivastav Jul 20 '16 at 13:27
  • I went for the most likely scenario. The two most probable issues you had, are NPE and UnknownHostException, The NPE could be from an invalid URI, or querying db column that does not exist. The host, is likely an spelling error. State the full address (and the protocol) to fix it, if that is the issue. – Bonatti Jul 20 '16 at 16:57