For some reason I get a null pointer exception and I think its because my object might be outside the scope of my alert dialog? I could be wrong though..

//private Lecture lecture;
private LectureManager lectureManager;


    addwork.setPositiveButton("Add", new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {

            EditText eweight = (EditText) ((AlertDialog)dialog).findViewById(99);
            EditText emark = (EditText) ((AlertDialog)dialog).findViewById(100);
            String coursename = ecoursename.getText().toString();


            try {

            if ((eweight.getText().toString().trim().equals("")) || 
                (emark.getText().toString().trim().equals(""))) {

                throw new InvalidInputException();

                } else {

                Double tmark = Double.parseDouble(emark.getText().toString());
                Double tweight = Double.parseDouble(eweight.getText().toString());

                Work work = new Work(tmark, tweight);
                Lecture lecture = new Lecture(coursename);
                lecture.addEvent(work);
                lectureManager.addClass(lecture);
                }

And my nullpointer exception is at the line "lectureManager.addClass(lecture);" and I'm not sure why. addClass is a function that is defined in class lecturemanager. The fields where I'm retrieving info for eweight, emark and coursename are instantiated outside of the alert dialog and are all final. Thanks in advance!

有帮助吗?

解决方案

I think you have declared and have not initialized lectureManager variable. Also make it final.

private final LectureManager lectureManager;

//somewhere in code instantiate it 
lectureManger = new LectureManager();

// then your code 

Hope this will help.

其他提示

lectureManager never intialized.

private LectureManager lectureManager = new LectureManager();

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top