我是android的新手,并且卡在空指针异常中。
我什至尝试了防御性编码,但仍然显示错误。

这是我的代码:

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;
    }

}


和log-cat显示:

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


请帮忙!

最佳答案

onCreate在例程的第3行调用loadContent

loadContent例程中,您可以调用:

days.add(day_1);


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

private ArrayList<Days> days;


...从未实例化。在尝试使用它之前,请将其放置在某处:

days = new ArrayList<Days>();


编辑:

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

关于java - java runtimeExceptio:java.lang.nullPointerException,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17494155/

10-11 03:28