Question

As the title says, I want to know what can cause a setadapter() method to cause a nullexception. I am writtting an android application and I am trying to populate a listview, this is my custom listadapter:

private class MyListAdapter extends ArrayAdapter<Grade> {

        ArrayList<Student> studentList;

        public MyListAdapter(ArrayList<Student> studentlist) {
            super(ShowExamActivity.this, R.layout.exam_grade_list_view, theList);

            studentList = studentlist;

            Log.e("Set Adapter1","Passed");

        }


        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View itemView = convertView;

            Log.e("before evrything", "!!!!!!!");
            if(itemView == null){
                itemView = getLayoutInflater().inflate(R.layout.exam_grade_list_view, parent, false);
            }

            Log.e("before Loop","Inside");

            Grade grade = theList.get(position);
            Student student = new Student();

            for(int i =0; i<studentList.size(); i++){
                for(int j =0; j<studentList.get(i).getGradeList().size(); j++){
                if(grade.getGrade() == studentList.get(i).getGradeList().get(j).getGrade() && studentList.get(i).getGradeList().get(j).getName().equals(grade)){
                    student = studentList.get(i);
                    studentList.remove(i);
                    break;
                }
                }   
            }
            Log.e("passed Loop","Inside");

            //Fill Name
            TextView name = (TextView) itemView.findViewById(R.id.name);
            name.setText(student.getFirstName());
            Log.e("Set Adapter3","Passed");


            //Fill Street
            TextView Grade = (TextView) itemView.findViewById(R.id.grade);
            Grade.setText(grade.getGrade());
            Log.e("Set Adapter4","Passed");


            return itemView;


        }

and this is the OnCreate() where the nullexception is appearing according to the LogCat:

public class ShowExamActivity extends Activity {


    TextView average;
    TextView stddev;
    TextView max;
    TextView min;
    Exam exam = new Exam();
    DSLL<Grade> gradeList = new DSLL<Grade>();
    ArrayList<Grade> theList = new ArrayList<Grade>();
    DSLL<Student> studentList = new DSLL<Student>();
    ArrayList<Student> studentTheList = new ArrayList<Student>();
    int position;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show_exam);
        // Show the Up button in the action bar.
        setupActionBar();

        average = (TextView) findViewById(R.id.nametextview);
        stddev = (TextView) findViewById(R.id.lastnametextview);
        max = (TextView) findViewById(R.id.max);
        min = (TextView) findViewById(R.id.min);


        try{
        Intent i = getIntent();
        exam = (Exam) i.getSerializableExtra("clickedExam");
        }
        catch (Exception e){
            Log.e("problema","con serializacion");
        }

        average.setText(Double.toString(exam.getAvg()));
        stddev.setText(Double.toString(exam.getStdDev()));
        max.setText(Double.toString(exam.getMax()));
        min.setText(Double.toString(exam.getMin()));
        gradeList = exam.getGradelist();
        studentList = exam.getStudentlist();

        if(gradeList.size() != 0){

            ListView lv =(ListView)findViewById(R.id.gradeList);
            Log.e("Listview!","created!!!!");
            for(int i=0;i<gradeList.size();i++){
                theList.add(gradeList.get(i));
            }
            for(int i=0;i<studentList.size();i++){
                studentTheList.add(studentList.get(i));
            }
            Log.e("for Loop Finished",Integer.toString(studentTheList.size()));
            Log.e("for Loop Finished",Integer.toString(theList.size()));

            MyListAdapter adapter = new MyListAdapter(studentTheList);
            Log.e("ArrayAdapter","Created");
            lv.setAdapter(adapter);
            Log.e("Set Adapter","Passed");

        }

    }

The part where the nullexception is appearing is in the lv.setAdapter(adapter)

here is my logcat:

04-18 21:17:41.510: E/AndroidRuntime(2318): FATAL EXCEPTION: main
04-18 21:17:41.510: E/AndroidRuntime(2318): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.student_lists/com.example.student_lists.ShowExamActivity}: java.lang.NullPointerException
04-18 21:17:41.510: E/AndroidRuntime(2318):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1960)
04-18 21:17:41.510: E/AndroidRuntime(2318):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1985)
04-18 21:17:41.510: E/AndroidRuntime(2318):     at android.app.ActivityThread.access$600(ActivityThread.java:127)
04-18 21:17:41.510: E/AndroidRuntime(2318):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1151)
04-18 21:17:41.510: E/AndroidRuntime(2318):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-18 21:17:41.510: E/AndroidRuntime(2318):     at android.os.Looper.loop(Looper.java:137)
04-18 21:17:41.510: E/AndroidRuntime(2318):     at android.app.ActivityThread.main(ActivityThread.java:4447)
04-18 21:17:41.510: E/AndroidRuntime(2318):     at java.lang.reflect.Method.invokeNative(Native Method)
04-18 21:17:41.510: E/AndroidRuntime(2318):     at java.lang.reflect.Method.invoke(Method.java:511)
04-18 21:17:41.510: E/AndroidRuntime(2318):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
04-18 21:17:41.510: E/AndroidRuntime(2318):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
04-18 21:17:41.510: E/AndroidRuntime(2318):     at dalvik.system.NativeStart.main(Native Method)
04-18 21:17:41.510: E/AndroidRuntime(2318): Caused by: java.lang.NullPointerException
04-18 21:17:41.510: E/AndroidRuntime(2318):     at com.example.student_lists.ShowExamActivity.onCreate(ShowExamActivity.java:80)
04-18 21:17:41.510: E/AndroidRuntime(2318):     at android.app.Activity.performCreate(Activity.java:4466)
04-18 21:17:41.510: E/AndroidRuntime(2318):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1092)
04-18 21:17:41.510: E/AndroidRuntime(2318):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1924)
04-18 21:17:41.510: E/AndroidRuntime(2318):     ... 11 more
Was it helpful?

Solution

Please make sure that R.id.gradeList is in your layout R.layout.activity_show_exam located in res/layout/activity_show_exam.xml

if it is and it still returns null please comment on my answer and tell me which API Level are you using.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top