简体   繁体   中英

NullPointerException when calling a method from a different class

I'm trying to get a simple note-taking app to work, but I keep on getting a nullPointerException when calling a certain method.

The value from the textEdit in the dialog layout is retrieved just fine. I checked by logging the results. The problem occurs when I try to run the createNotes(content) method.

public class Dialog extends Activity {
NotesBean notesBean = null;
public NotesDao datasource;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Log.e("log", "Running NotiMgr");
    setContentView(R.layout.dialog);
    RelativeLayout layout = (RelativeLayout) findViewById(R.id.dialog_background);  
    layout.setOnClickListener( new View.OnClickListener() {
        @Override
        public void onClick(View v) {
        EditText mEdit = (EditText) v.findViewById(R.id.editText);
        String content = mEdit.getText().toString();
        Log.v("EditText", content);

        //THIS IS WHERE THE ERROR OCCURS
        datasource.createNotes(content);
        finish();
        }
    });
}

This class deals with inserts, deletes and db related stuff. I've tried debugging, but it never reaches this class.

public class NotesDao {
public NotesBean createNotes(String content) {
    try {
        Log.v("createNotes", content);
        ContentValues values = new ContentValues();
        values.put(DbHelper.COLUMN_CONTENT, content);
        long insertSeq = database.insert(DbHelper.TABLE_NOTES, null, values);
        Cursor cursor = database.query(
                DbHelper.TABLE_NOTES,                       //String table
                allColumns,                                 //String[] columns
                DbHelper.COLUMN_SEQ + " = " + insertSeq,        //String selection
                null,                                       //String[] selectionArgs
                null,                                       //String groupBy
                null,                                       //String having
                null);                                      //String orderBy
        cursor.moveToFirst();
        NotesBean newNotes = cursorToNotes(cursor);
        cursor.close();
        return newNotes;
    } catch (Exception e) {
          throw new RuntimeException(e);
    }
}

btw I have also confirmed that the db is opened through logcat... But it was in a different activity. Does that mean I should close it and open it again in this activity?

Any help would be greatly appreciated! Thanks!

change:

public NotesDao datasource;

to

public NotesDao datasource=new NotesDao();

You have never initialized datasource until you call createNotes() , so the NullPointerException does not come unexpected.

You might consider making NotesDao a Singleton , if you don't need multiple instances of it. Just google Singleton .

你从来没有初始化'datasource'对象,这就是它给出空指针异常的原因。

NullPointer Exceptions always come up with missing initializations or wrong view element initialization. In your case you are missing to initialise datasource object (as pointed by earlier answers).

A valuable advice will be always cross check your program for everything is initialised, you may also use Exception Handling for cases you are not sure, like for data coming from different source.

You are not instantiate your NoteDao object. please update your code like this:

   public NotesDao datasource=new NotesDao();

As stated, your other class is not initialized.

In your onCreate put the initialization.

datasource = new NotesDao();

You have to wait until your class is made in onCreate to initialize the variables in that class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM