简体   繁体   中英

NullPointerException while retrieving data from ArrayList

Android 2.3.3

I have an ArrayList, with 3 values. While i am trying to retrieve values from the arraylist, i get a NullPointerException. I am trying to get the value from arraylist and set it to a textview.

Here is the code :::

System.out.println(alAngleResult.toString());

        txtViewResult.setText("");

        int i = 0;
        while(i < alAngleResult.size())
        {
            txtViewResult.append(alAngleResult.get(i).toString());
                    // Exception at above line
            txtViewResult.append("\n");

            i++;
        }

LogCat output :::

01-25 01:32:26.599: I/System.out(18437): [10.0 Degrees, 0.17453292519943295 Radians, 10.0 Gradians] => Output
01-25 01:32:26.599: W/dalvikvm(18437): threadid=1: thread exiting with uncaught exception (group=0x40184560)
01-25 01:32:26.599: E/AndroidRuntime(18437): FATAL EXCEPTION: main
01-25 01:32:26.599: E/AndroidRuntime(18437): java.lang.NullPointerException
01-25 01:32:26.599: E/AndroidRuntime(18437):    at com.xx.xx.xx.performConvertionForAngle(UnitConverter.java:416)
01-25 01:32:26.599: E/AndroidRuntime(18437):    at com.xx.xx.xx.performConvertion(UnitConverter.java:368)
01-25 01:32:26.599: E/AndroidRuntime(18437):    at com.xx.xx.xx.onClick(UnitConverter.java:356)
01-25 01:32:26.599: E/AndroidRuntime(18437):    at android.view.View.performClick(View.java:2485)
01-25 01:32:26.599: E/AndroidRuntime(18437):    at android.view.View$PerformClick.run(View.java:9080)
01-25 01:32:26.599: E/AndroidRuntime(18437):    at android.os.Handler.handleCallback(Handler.java:587)
01-25 01:32:26.599: E/AndroidRuntime(18437):    at android.os.Handler.dispatchMessage(Handler.java:92)
01-25 01:32:26.599: E/AndroidRuntime(18437):    at android.os.Looper.loop(Looper.java:130)
01-25 01:32:26.599: E/AndroidRuntime(18437):    at android.app.ActivityThread.main(ActivityThread.java:3687)
01-25 01:32:26.599: E/AndroidRuntime(18437):    at java.lang.reflect.Method.invokeNative(Native Method)
01-25 01:32:26.599: E/AndroidRuntime(18437):    at java.lang.reflect.Method.invoke(Method.java:507)
01-25 01:32:26.599: E/AndroidRuntime(18437):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:878)
01-25 01:32:26.599: E/AndroidRuntime(18437):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:636)
01-25 01:32:26.599: E/AndroidRuntime(18437):    at dalvik.system.NativeStart.main(Native Method)

EDIT ::: Just had to clean the project. It works fine now.

您的数组很可能包含null值,因此最终调用null.toString()

Just a typical Eclipse Problem. Just had to clean the project, and everything worked fine.

Glad it's working for you now.

Re the code snip, wouldn't it be easier to read and less effort as a for-each loop?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM