字符串空指针异常

时间:2013-08-28 14:48:41

标签: android nullpointerexception

我的应用程序中有一些奇怪的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);

Log cat:

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]);

给我字符串而非null错误。

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

1 个答案:

答案 0 :(得分:0)

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

Boolean b= new GetUserPicture().execute();