Вопрос

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