0

我的应用程序中有一些奇怪的 NullPointerException。

我运行这段代码:

protected void onPostExecute(Void result)
{               

    Log.i("name1", userProfilePictures[positionForPicture][0]);

    if(movedToAnotherActivity)
        return;

    if(loadNewPosts && loaded10More == false)
    {           
        firstLoopPicture = true;
        positionForPicture = 0;
        return; 
    }

    if((positionForPicture  == userProfilePictures.length) == false)
    {
        new GetUserPicture().execute();

        int loadLength = loadedPostsSaverForPictures.length;

        String name = userProfilePictures[positionForPicture][0];

        Log.i("name", name);
    }
}

…………

我在这一行得到空指针异常:

String name = userProfilePictures[positionForPicture][0];
    Log.i("name", name);

日志猫:

08-28 17:39:28.140: E/AndroidRuntime(26801): FATAL EXCEPTION: main
08-28 17:39:28.140: E/AndroidRuntime(26801): java.lang.NullPointerException: println needs a message
08-28 17:39:28.140: E/AndroidRuntime(26801):    at android.util.Log.println_native(Native Method)
08-28 17:39:28.140: E/AndroidRuntime(26801):    at android.util.Log.i(Log.java:159)
08-28 17:39:28.140: E/AndroidRuntime(26801):    at com.example.workoutlog.WorkoutsWall$GetUserPicture.onPostExecute(WorkoutsWall.java:3420)
08-28 17:39:28.140: E/AndroidRuntime(26801):    at com.example.workoutlog.WorkoutsWall$GetUserPicture.onPostExecute(WorkoutsWall.java:1)
08-28 17:39:28.140: E/AndroidRuntime(26801):    at android.os.AsyncTask.finish(AsyncTask.java:631)
08-28 17:39:28.140: E/AndroidRuntime(26801):    at android.os.AsyncTask.access$600(AsyncTask.java:177)
08-28 17:39:28.140: E/AndroidRuntime(26801):    at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
08-28 17:39:28.140: E/AndroidRuntime(26801):    at android.os.Handler.dispatchMessage(Handler.java:99)
08-28 17:39:28.140: E/AndroidRuntime(26801):    at android.os.Looper.loop(Looper.java:137)
08-28 17:39:28.140: E/AndroidRuntime(26801):    at android.app.ActivityThread.main(ActivityThread.java:5226)
08-28 17:39:28.140: E/AndroidRuntime(26801):    at java.lang.reflect.Method.invokeNative(Native Method)
08-28 17:39:28.140: E/AndroidRuntime(26801):    at java.lang.reflect.Method.invoke(Method.java:511)
08-28 17:39:28.140: E/AndroidRuntime(26801):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
08-28 17:39:28.140: E/AndroidRuntime(26801):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
08-28 17:39:28.140: E/AndroidRuntime(26801):    at dalvik.system.NativeStart.main(Native Method)

奇怪的是,在这行之前我有这个代码:

Log.i("name1", userProfilePictures[positionForPicture][0]);

这给了我字符串而不是空错误。

为什么我得到空指针异常?它是相同的字符串...

4

1 回答 1

0

如果此异步任务正在加载您的数组userProfilePictures,您应该等待答案。在 postexecute 中返回一个布尔值并执行:

Boolean b= new GetUserPicture().execute();
于 2013-08-28T15:15:14.423 回答