NullPointerException when calling a method from a different class

Han

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!

vipul mittal

change:

public NotesDao datasource;

to

public NotesDao datasource=new NotesDao();

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

NullPointerException when calling method from another class

From Dev

SpringBoot @Autowired NullPointerException when calling method from service class

From Dev

Calling method from a different class

From Dev

NullPointerException when calling a Fragment method from Activity?

From Dev

Mockito : java.lang.NullPointerException when calling method from mocked Class

From Java

Calling method of same name from different class

From Dev

Calling an instance method from a different class

From Dev

Calling method from another class in onPostExecute causing nullPointerException

From Dev

NullPointerException when calling a mocked method

From Dev

NullPointerException when calling mocked method

From Dev

NullPointerException when calling EJB method

From Dev

NullPointerException when calling Redis method

From Dev

NullPointerExceptionError when calling method from class

From Dev

NullPointerException when calling a method from a dynamic object array

From Dev

How would I change an object's value when calling a static method from a different class?

From Dev

TypeError: debug() missing 1 required positional argument: 'msg' when calling method from different class

From Dev

Calling a method from a different class that uses variables outside the method

From Dev

Javascript OOP - calling a method from a different method of the same class

From Java

calling String method from different class to main class

From Dev

Calling a method from a class

From Dev

JButton listener calling method in class gives NullPointerException

From Java

Calling a method from a different class containing Graphics g [JAVA]

From Dev

Calling methods from a different class on method_missing

From Dev

Create a label dynamiclay in form by calling a method from a different class

From Dev

Calling a New Method from a Different Class in the Same Package

From Dev

Calling Method To Add Parameters Into An Array From A Different Class

From Dev

Parcelable: Class not found when unmarshalling when calling from different App

From Dev

When Calling Other Activity Method Showing NullPointerException

From Dev

Python Error when class constructor calling from different file

Related Related

  1. 1

    NullPointerException when calling method from another class

  2. 2

    SpringBoot @Autowired NullPointerException when calling method from service class

  3. 3

    Calling method from a different class

  4. 4

    NullPointerException when calling a Fragment method from Activity?

  5. 5

    Mockito : java.lang.NullPointerException when calling method from mocked Class

  6. 6

    Calling method of same name from different class

  7. 7

    Calling an instance method from a different class

  8. 8

    Calling method from another class in onPostExecute causing nullPointerException

  9. 9

    NullPointerException when calling a mocked method

  10. 10

    NullPointerException when calling mocked method

  11. 11

    NullPointerException when calling EJB method

  12. 12

    NullPointerException when calling Redis method

  13. 13

    NullPointerExceptionError when calling method from class

  14. 14

    NullPointerException when calling a method from a dynamic object array

  15. 15

    How would I change an object's value when calling a static method from a different class?

  16. 16

    TypeError: debug() missing 1 required positional argument: 'msg' when calling method from different class

  17. 17

    Calling a method from a different class that uses variables outside the method

  18. 18

    Javascript OOP - calling a method from a different method of the same class

  19. 19

    calling String method from different class to main class

  20. 20

    Calling a method from a class

  21. 21

    JButton listener calling method in class gives NullPointerException

  22. 22

    Calling a method from a different class containing Graphics g [JAVA]

  23. 23

    Calling methods from a different class on method_missing

  24. 24

    Create a label dynamiclay in form by calling a method from a different class

  25. 25

    Calling a New Method from a Different Class in the Same Package

  26. 26

    Calling Method To Add Parameters Into An Array From A Different Class

  27. 27

    Parcelable: Class not found when unmarshalling when calling from different App

  28. 28

    When Calling Other Activity Method Showing NullPointerException

  29. 29

    Python Error when class constructor calling from different file

HotTag

Archive