0

我是 android 新手,遇到了 null Pointer 的异常。我什至尝试过防御性编码,但它仍然显示错误。

这是我的代码:

public class MainActivity extends Activity {

    private ArrayList<SubjectType> subjects;
    private ArrayList<Days> days;
    private ExpandableListAdapter adapter;
    private ExpandableListView exv;
    private String DayNames[] = {"Monday","Tuesday","Wednesday","Thursday","Friday"};
    private String TypeName[]=  {"Lecture","Practical","Tutorial"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        loadContent();

        exv = (ExpandableListView) findViewById(R.id.expandableListView1);
        adapter = new ExpandableListAdapter(MainActivity.this, subjects);
        exv.setAdapter(adapter);
        collapseAll();


    }

    private void collapseAll() {
        for(int i=0;i<3;i++)
        {
            exv.collapseGroup(i);
        }
    }

    private void loadContent() {

            int daySize,typeSize;
            Days day_1 = null;
            boolean False = false;
            SubjectType sub1=null;
            daySize = 5;
            typeSize = 3;

            for(int i=0;i<daySize;i++)
            {
                if(DayNames[i]!=null)
                {
                    Days day_12 = day_1;
                    day_12.setDayAttributes(DayNames[i], False);
                    days.add(day_1);
                }
            }

            for(int j=0;j<typeSize;j++)
            {
                SubjectType sub12 = sub1;
                sub12.setChecked(false);
                if(days!=null&&TypeName[j]!=null&&sub12!=null&&sub12!=null)
                {
                sub12.setDays(days);
                sub12.setSubjectTypeName(TypeName[j]);
                subjects.add(sub12);
                }
            }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

和日志猫显示:

07-05 17:14:26.942: E/AndroidRuntime(991): FATAL EXCEPTION: main
07-05 17:14:26.942: E/AndroidRuntime(991): java.lang.RuntimeException: Unable to start activity ComponentInfo{trying.expandables/trying.expandables.MainActivity}: java.lang.NullPointerException
07-05 17:14:26.942: E/AndroidRuntime(991):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
07-05 17:14:26.942: E/AndroidRuntime(991):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
07-05 17:14:26.942: E/AndroidRuntime(991):  at android.app.ActivityThread.access$600(ActivityThread.java:141)
07-05 17:14:26.942: E/AndroidRuntime(991):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
07-05 17:14:26.942: E/AndroidRuntime(991):  at android.os.Handler.dispatchMessage(Handler.java:99)
07-05 17:14:26.942: E/AndroidRuntime(991):  at android.os.Looper.loop(Looper.java:137)
07-05 17:14:26.942: E/AndroidRuntime(991):  at android.app.ActivityThread.main(ActivityThread.java:5041)
07-05 17:14:26.942: E/AndroidRuntime(991):  at java.lang.reflect.Method.invokeNative(Native Method)
07-05 17:14:26.942: E/AndroidRuntime(991):  at java.lang.reflect.Method.invoke(Method.java:511)
07-05 17:14:26.942: E/AndroidRuntime(991):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-05 17:14:26.942: E/AndroidRuntime(991):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-05 17:14:26.942: E/AndroidRuntime(991):  at dalvik.system.NativeStart.main(Native Method)
07-05 17:14:26.942: E/AndroidRuntime(991): Caused by: java.lang.NullPointerException
07-05 17:14:26.942: E/AndroidRuntime(991):  at trying.expandables.MainActivity.loadContent(MainActivity.java:54)
07-05 17:14:26.942: E/AndroidRuntime(991):  at trying.expandables.MainActivity.onCreate(MainActivity.java:23)
07-05 17:14:26.942: E/AndroidRuntime(991):  at android.app.Activity.performCreate(Activity.java:5104)
07-05 17:14:26.942: E/AndroidRuntime(991):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
07-05 17:14:26.942: E/AndroidRuntime(991):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
07-05 17:14:26.942: E/AndroidRuntime(991):  ... 11 more

请帮忙!

4

4 回答 4

2

stacktrace 中最有趣的一行是

07-05 17:14:26.942: E/AndroidRuntime(991):  at trying.expandables.MainActivity.loadContent(MainActivity.java:54)

(很多时候,第一行提到非系统代码是最好的起点)

根据我的统计,第 54 行是

subjects.add(sub12);

在您的代码中,我没有看到正在初始化的主题,这解释了 NPE。

编辑:虽然我看错了路线,但原因是一样的,正如 TheCapn 和 Joachim Isaksson 已经指出的那样。我现在看到你这样做了

        SubjectType sub1=null;
        ...
            SubjectType sub12 = sub1;
            sub12.setChecked(false); // <--- same BOOM, other line.

我们是否开始在这里看到一种模式?

此外,避免密集的东西,如

if(days!=null&&TypeName[j]!=null&&sub12!=null&&sub12!=null)

因为

if( (days!=null) && (TypeName[j]!=null) && (sub12!=null) && (sub12!=null) )

眼睛会轻松很多...

于 2013-07-05T17:35:46.357 回答
2

onCreate在例程的第 3 行调用loadContent

loadContent您调用的例程中:

days.add(day_1);

在第一个 for 循环中。声明:

private ArrayList<Days> days;

...从不实例化。在尝试使用它之前把它放在某个地方:

days = new ArrayList<Days>();

编辑:

正如其他人所说。做同样的事情subjects

于 2013-07-05T17:41:10.180 回答
2

你的逻辑有些不对劲loadContent

Days day_1 = null;

...后来,没有其他任务,day_1然后...

Days day_12 = day_1;                          // Sets day12 to null.
day_12.setDayAttributes(DayNames[i], False);  // boom
于 2013-07-05T17:41:34.687 回答
-1

变量 subject 没有任何值!

于 2013-07-05T17:38:41.050 回答