0

我的代码中出现 NullPointerException,并且我用几种不同的方式编写了相同的预期结果,但我仍然总是以错误告终。

这是有问题的代码:

ArrayList<String> titleOne = new ArrayList<String>(); 
    titleOne.add(getFirstTitle.getFirstJSONTitle(creditOne));

这是包含有问题的方法的 getFristTitle 类:

public class getFirstTitle extends AsyncTask<String, Void, String > {

public static String TAG_CAST = "cast";
public static String TAG_TITLE = "title";

static ArrayList<String> firstCast = null;
static String title = null;
static JSONArray castArray;
String creditURLOne = null;



protected String doInBackground(String... creditOne) {
    // TODO Auto-generated method stub
    creditURLOne = creditOne.toString();

    return getFirstJSONTitle(creditURLOne);

}
final static String getFirstJSONTitle(String creditURLOne){


    Log.d("crediturl in title method contains", creditURLOne);
    JSONParser jParser = new JSONParser();

    JSONObject jSon = jParser.getJSONFromUrl(creditURLOne);

    try{
        castArray = jSon.getJSONArray(TAG_CAST);
        Log.d("castarray in title method contains", castArray.toString());
        for(int i=0; i < castArray.length(); i++){
            JSONObject f = castArray.getJSONObject(i);
            title = f.getString(TAG_TITLE);
            Log.d("title contains", title);
            firstCast.add(title);

            Log.d("firstCast contains", firstCast.toString());
        }


    }
    catch(JSONException e){
        Log.e("Error", e.toString());
        return null;
    }

    return firstCast.toString();

}
}

如您所见,我有一些 Logging 语句,这里是 logcat 的输出:

09-13 11:20:17.681: I/Process(837): Sending signal. PID: 837 SIG: 9
09-13 11:30:49.621: W/KeyCharacterMap(865): No keyboard for id 0
09-13 11:30:49.627: W/KeyCharacterMap(865): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
09-13 11:30:57.917: D/searchurlone(865): http://api.themoviedb.org/3/search/person?api_key=bb0b6d66c2899aefb4d0863b0d37dc4e&query=christopher_guest
09-13 11:30:58.027: D/dalvikvm(865): GC_FOR_MALLOC freed 8231 objects / 370392 bytes in 67ms
09-13 11:30:58.187: D/dalvikvm(865): GC_FOR_MALLOC freed 742 objects / 40504 bytes in 43ms
09-13 11:30:58.308: D/JSON Contents(865): {"page":1,"results":[{"adult":false,"id":13524,"name":"Christopher Guest","profile_path":"/fhEQq0q2aR2sh4HU824xunoyAce.jpg"}],"total_pages":1,"total_results":1}
09-13 11:30:58.318: D/JSONArray idOne(865): 13524
09-13 11:30:58.457: D/dalvikvm(865): GC_FOR_MALLOC freed 462 objects / 31288 bytes in 44ms
09-13 11:30:58.578: D/JSON Contents(865): {"page":1,"results":[{"adult":false,"id":26510,"name":"Eugene Levy","profile_path":"/5bs50OKUwLcjrznBnIua9wpLRJ4.jpg"}],"total_pages":1,"total_results":1}
09-13 11:30:58.578: D/creditOne contains(865): http://api.themoviedb.org/3/person/13524/credits?api_key=bb0b6d66c2899aefb4d0863b0d37dc4e
09-13 11:30:58.587: D/crediturl in title method contains(865): http://api.themoviedb.org/3/person/13524/credits?api_key=bb0b6d66c2899aefb4d0863b0d37dc4e
09-13 11:30:58.728: D/dalvikvm(865): GC_FOR_MALLOC freed 1078 objects / 243496 bytes in 44ms
09-13 11:30:58.977: D/JSON Contents(865): {"cast":[{"id":881,"title":"A Few Good Men","character":"Dr. Stone","original_title":"A Few Good Men","poster_path":"/4HNgFBkmfkJEbywBAKQQmEQpo95.jpg","release_date":"1992-12-11","adult":false},{"id":2493,"title":"The Princess Bride","character":"Count Tyrone Rugen","original_title":"The Princess Bride","poster_path":"/7DVvPfYpSNO6xim2Z7vtOu5aqp7.jpg","release_date":"1987-10-01","adult":false},{"id":10773,"title":"Mrs Henderson Presents","character":"Lord Cromer","original_title":"Mrs Henderson Presents","poster_path":"/ua1ZWxGE9tYJnJLQh06xgiCvUjo.jpg","release_date":"2005-09-09","adult":false},{"id":11031,"title":"This Is Spinal Tap","character":"Nigel Tufnel","original_title":"This Is Spinal Tap","poster_path":"/hTb29XWr5jm9itLcgkDaBfNvCpl.jpg","release_date":"1984-09-08","adult":false},{"id":13370,"title":"A Mighty Wind","character":"","original_title":"A Mighty Wind","poster_path":"/xkux7Pa89gi3YQGGKeAEAAA28rD.jpg","release_date":"2003-04-16","adult":false},{"id":13785,"title":"Best In Show","character":"Harlan Pepper","original_title":"Best In Show","poster_path":"/72eH7l8qN1y6EWC2R3BAMq62dEH.jpg","release_date":"2000-09-19","adult":false},{"id":16448,"title":"Waiting For Guffman","character":"Corky St. Clair","original_title":"Waiting For Guffman","poster_path":"/3WTPgKNE5gZVm3zNWvBhbbXYxXr.jpg","release_date":"1996-01-31","adult":false},{"id":18360,"title":"Night at the Museum 2: Battle of the Smithsonian","character":"Ivan the Terrible","original_title":"Night at the Museum 2: Battle of the Smithsonian","poster_path":"/qbU6AxmO69bBwu6Tw8HtcRoltAA.jpg","release_date":"2009-05-22","adult":false},{"id":23082,"title":"The Invention of Lying","character":"Nathan Goldfrappe","original_title":"The Invention of Lying","poster_path":"/x2CsqtcSqGcrkpMIQCUum5JDiHg.jpg","release_date":"2009-09-14","adult":false},{"id":48279,"title":"The Missing Link","character":"No Lobes","original_title":"Le chaînon manquant","poster_path":"/kHzAu0ZLFtiwV0PLV3iEGxtxRIc.jpg","release_date":"1980-05-21","adult":false},{"id":14729,"title":"The Long Riders","character":"Charlie Ford","original_title":"The Long Riders","poster_path":"/kPBOddG81QREMIR8Gir0i7rJ2q5.jpg","release_date":"1980-05-16","adult":false},{"id":83726,"title":"Beyond Therapy","character":"Bob","original_title":"Beyond Therapy","poster_path":"/sfmObsPgtuQOo99b9e59QOQsMe3.jpg","release_date":"1987-02-27","adult":false},{"id":50237,"title":"Shame of the Jungle","character":"Chief M'Bulu / Short / Nurse (voice: English version)","original_title":"Shame of the Jungle","poster_path":"/6eIui4lTo12bt7xPiwomcSqNmuu.jpg","release_date":"1979-09-14","adult":false},{"id":106273,"title":"Lemmings","character":"Various","original_title":"Lemmings","poster_path":"/g09OMzNEzCQLzc2XoLntwMPUsqk.jpg","release_date":null,"adult":false},{"id":73247,"title":"Heartbeeps","character":"Calvin","original_title":"Heartbeeps","poster_path":"/miWjN4ehccUnh0CKR3wG3udKVf0.jpg","release_date":"1981-12-18","adult":false}],"crew":[{"id":11031,"title":"This Is Spinal Tap","original_title":"This Is Spinal Tap","department":"Writing","job":"Author","poster_path":"/hTb29XWr5jm9itLcgkDaBfNvCpl.jpg","release_date":"1984-09-08","adult":false},{"id":11031,"title":"This Is Spinal Tap","original_title":"This Is Spinal Tap","department":"Sound","job":"Original Music Composer","poster_path":"/hTb29XWr5jm9itLcgkDaBfNvCpl.jpg","release_date":"1984-09-08","adult":false},{"id":13370,"title":"A Mighty Wind","original_title":"A Mighty Wind","department":"Directing","job":"Director","poster_path":"/xkux7Pa89gi3YQGGKeAEAAA28rD.jpg","release_date":"2003-04-16","adult":false},{"id":13370,"title":"A Mighty Wind","original_title":"A Mighty Wind","department":"Writing","job":"Writer","poster_path":"/xkux7Pa89gi3YQGGKeAEAAA28rD.jpg","release_date":"2003-04-16","adult":false},{"id":13785,"title":"Best In Show","original_title":"Best In Show","department":"Directing","job":"Director","poster_path":"/72eH7l8qN1y6EWC2R3BAMq62dEH.jpg","release_date":"2000-09-19","adult":false},{"id":13785,"title":"Best
09-13 11:30:59.017: D/castarray in title method contains(865): [{"release_date":"1992-12-11","original_title":"A Few Good Men","id":881,"title":"A Few Good Men","adult":false,"poster_path":"\/4HNgFBkmfkJEbywBAKQQmEQpo95.jpg","character":"Dr. Stone"},{"release_date":"1987-10-01","original_title":"The Princess Bride","id":2493,"title":"The Princess Bride","adult":false,"poster_path":"\/7DVvPfYpSNO6xim2Z7vtOu5aqp7.jpg","character":"Count Tyrone Rugen"},{"release_date":"2005-09-09","original_title":"Mrs Henderson Presents","id":10773,"title":"Mrs Henderson Presents","adult":false,"poster_path":"\/ua1ZWxGE9tYJnJLQh06xgiCvUjo.jpg","character":"Lord Cromer"},{"release_date":"1984-09-08","original_title":"This Is Spinal Tap","id":11031,"title":"This Is Spinal Tap","adult":false,"poster_path":"\/hTb29XWr5jm9itLcgkDaBfNvCpl.jpg","character":"Nigel Tufnel"},{"release_date":"2003-04-16","original_title":"A Mighty Wind","id":13370,"title":"A Mighty Wind","adult":false,"poster_path":"\/xkux7Pa89gi3YQGGKeAEAAA28rD.jpg","character":""},{"release_date":"2000-09-19","original_title":"Best In Show","id":13785,"title":"Best In Show","adult":false,"poster_path":"\/72eH7l8qN1y6EWC2R3BAMq62dEH.jpg","character":"Harlan Pepper"},{"release_date":"1996-01-31","original_title":"Waiting For Guffman","id":16448,"title":"Waiting For Guffman","adult":false,"poster_path":"\/3WTPgKNE5gZVm3zNWvBhbbXYxXr.jpg","character":"Corky St. Clair"},{"release_date":"2009-05-22","original_title":"Night at the Museum 2: Battle of the Smithsonian","id":18360,"title":"Night at the Museum 2: Battle of the Smithsonian","adult":false,"poster_path":"\/qbU6AxmO69bBwu6Tw8HtcRoltAA.jpg","character":"Ivan the Terrible"},{"release_date":"2009-09-14","original_title":"The Invention of Lying","id":23082,"title":"The Invention of Lying","adult":false,"poster_path":"\/x2CsqtcSqGcrkpMIQCUum5JDiHg.jpg","character":"Nathan Goldfrappe"},{"release_date":"1980-05-21","original_title":"Le chaînon manquant","id":48279,"title":"The Missing Link","adult":false,"poster_path":"\/kHzAu0ZLFtiwV0PLV3iEGxtxRIc.jpg","character":"No Lobes"},{"release_date":"1980-05-16","original_title":"The Long Riders","id":14729,"title":"The Long Riders","adult":false,"poster_path":"\/kPBOddG81QREMIR8Gir0i7rJ2q5.jpg","character":"Charlie Ford"},{"release_date":"1987-02-27","original_title":"Beyond Therapy","id":83726,"title":"Beyond Therapy","adult":false,"poster_path":"\/sfmObsPgtuQOo99b9e59QOQsMe3.jpg","character":"Bob"},{"release_date":"1979-09-14","original_title":"Shame of the Jungle","id":50237,"title":"Shame of the Jungle","adult":false,"poster_path":"\/6eIui4lTo12bt7xPiwomcSqNmuu.jpg","character":"Chief M'Bulu \/ Short \/ Nurse (voice: English version)"},{"release_date":null,"original_title":"Lemmings","id":106273,"title":"Lemmings","adult":false,"poster_path":"\/g09OMzNEzCQLzc2XoLntwMPUsqk.jpg","character":"Various"},{"release_date":"1981-12-18","original_title":"Heartbeeps","id":73247,"title":"Heartbeeps","adult":false,"poster_path":"\/miWjN4ehccUnh0CKR3wG3udKVf0.jpg","character":"Calvin"}]
09-13 11:30:59.017: D/title contains(865): A Few Good Men
09-13 11:30:59.027: D/AndroidRuntime(865): Shutting down VM
09-13 11:30:59.027: W/dalvikvm(865): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
09-13 11:30:59.037: E/AndroidRuntime(865): FATAL EXCEPTION: main
09-13 11:30:59.037: E/AndroidRuntime(865): java.lang.IllegalStateException: Could not execute method of the activity
09-13 11:30:59.037: E/AndroidRuntime(865):  at android.view.View$1.onClick(View.java:2072)
09-13 11:30:59.037: E/AndroidRuntime(865):  at android.view.View.performClick(View.java:2408)
09-13 11:30:59.037: E/AndroidRuntime(865):  at android.view.View$PerformClick.run(View.java:8816)
09-13 11:30:59.037: E/AndroidRuntime(865):  at android.os.Handler.handleCallback(Handler.java:587)
09-13 11:30:59.037: E/AndroidRuntime(865):  at android.os.Handler.dispatchMessage(Handler.java:92)
09-13 11:30:59.037: E/AndroidRuntime(865):  at android.os.Looper.loop(Looper.java:123)
09-13 11:30:59.037: E/AndroidRuntime(865):  at android.app.ActivityThread.main(ActivityThread.java:4627)
09-13 11:30:59.037: E/AndroidRuntime(865):  at java.lang.reflect.Method.invokeNative(Native Method)
09-13 11:30:59.037: E/AndroidRuntime(865):  at java.lang.reflect.Method.invoke(Method.java:521)
09-13 11:30:59.037: E/AndroidRuntime(865):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
09-13 11:30:59.037: E/AndroidRuntime(865):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
09-13 11:30:59.037: E/AndroidRuntime(865):  at dalvik.system.NativeStart.main(Native Method)
09-13 11:30:59.037: E/AndroidRuntime(865): Caused by: java.lang.reflect.InvocationTargetException
09-13 11:30:59.037: E/AndroidRuntime(865):  at com.tot.tipofthetongue.Main.displayResults(Main.java:84)
09-13 11:30:59.037: E/AndroidRuntime(865):  at java.lang.reflect.Method.invokeNative(Native Method)
09-13 11:30:59.037: E/AndroidRuntime(865):  at java.lang.reflect.Method.invoke(Method.java:521)
09-13 11:30:59.037: E/AndroidRuntime(865):  at android.view.View$1.onClick(View.java:2067)
09-13 11:30:59.037: E/AndroidRuntime(865):  ... 11 more
09-13 11:30:59.037: E/AndroidRuntime(865): Caused by: java.lang.NullPointerException
09-13 11:30:59.037: E/AndroidRuntime(865):  at com.tot.tipofthetongue.getFirstTitle.getFirstJSONTitle(getFirstTitle.java:48)
09-13 11:30:59.037: E/AndroidRuntime(865):  ... 15 more

My title contains the entry a Few Good Men but it doesn't seem to be looping through and adding the other titles in the array (there are certainly many more) as you can see in the logcat next to castarray in title method contents. I need to loop through that JSON and get all the titles into a new arraylist not sure why it isn't working. 
4

1 回答 1

6
 ArrayList<String> firstCast = null;

你的 firstCast 是null,你正在尝试

firstCast.add(title);

当您对空引用执行任何操作时,您将获得NullPointerException.

改变

 ArrayList<String> firstCast = null;

ArrayList<String> firstCast = new ArrayList<String>();
于 2012-09-13T18:41:14.883 回答