简体   繁体   English

从ArrayList检索数据时发生NullPointerException

[英]NullPointerException while retrieving data from ArrayList

Android 2.3.3 Android 2.3.3

I have an ArrayList, with 3 values. 我有一个带有3个值的ArrayList。 While i am trying to retrieve values from the arraylist, i get a NullPointerException. 当我尝试从arraylist检索值时,出现NullPointerException。 I am trying to get the value from arraylist and set it to a textview. 我试图从arraylist获取值并将其设置为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 ::: LogCat输出:::

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. 只是一个典型的Eclipse问题。 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? 重新编写代码,作为for-each循环,难道不是更容易阅读,更省力吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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