0

Before you mark this duplicate, I did view solution to other similar question but it isn't working. I am getting this error only the first time I run my app. I am not sure why is this happening. I am calling MainActivity.adapter.notifyDataSetChanged in the Main2Activity (Edit Text Activity). The app crashes when after installing during pressing the back button in the EditText actitvity i.e. Main2Activity.java.

MainActivity:

public class MainActivity extends AppCompatActivity {

ListView notesListView;
static ArrayList<String> notesArrayList;
static ArrayAdapter<String> adapter;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    notesListView = (ListView) findViewById(R.id.notesListView);
    notesArrayList = new ArrayList<String>();

        adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, notesArrayList);
        notesListView.setAdapter(adapter);
        Main2Activity.myDb = new DatabaseOperations(this);

    if (Main2Activity.myDb != null) {
        notesArrayList.clear();
        Cursor result = Main2Activity.myDb.getData();
        if (result.getCount() == 0) {
            Log.i("Error", "error");
            return;
        }
        while (result.moveToNext()) {
            notesArrayList.add(result.getString(result.getColumnIndex("text")));
        }
    }

    //notesArrayList.add("Akhilesh Chobey");      

    notesListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            Intent intent = new Intent(getApplicationContext(), Main2Activity.class);
            intent.putExtra("notePosition", i);
            startActivity(intent);
        }
    });

    notesListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
        @Override
        public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {


            return false;
        }
    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    int id = item.getItemId();
    if (id == R.id.add_button) {
        notesArrayList.add("");
    }
    Intent intent = new Intent(getApplicationContext(), Main2Activity.class);
    intent.putExtra("notePosition", notesArrayList.size() - 1);
    startActivity(intent);


    return true;

}
}

Main2Activity (EditText Activity):

public class Main2Activity extends AppCompatActivity implements TextWatcher {

static DatabaseOperations myDb;
EditText editNote;
int position;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);
    editNote = (EditText) findViewById(R.id.noteEditText);

    myDb = new DatabaseOperations(Main2Activity.this);

    Intent intent = getIntent();
    position = intent.getIntExtra("notePosition", -1);
    if(position != -1){

        editNote.setText(MainActivity.notesArrayList.get(position));

    }
    editNote.addTextChangedListener(this);
}

@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

}

@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
    if (myDb != null) {   

    }

}

@Override
public void afterTextChanged(Editable editable) {

}

@Override
public void onBackPressed(){
    myDb.insertData(editNote.getText().toString());
    MainActivity.notesArrayList.set(position, editNote.getText().toString());
    MainActivity.adapter.notifyDataSetChanged();
    this.finish();
}
}

Log:

07-02 18:45:40.402 17500-17500/com.akhileshchobey.mynotes E/MessageQueue-JNI: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ArrayAdapter.notifyDataSetChanged()' on a null object reference at com.akhileshchobey.mynotes.Main2Activity.onBackPressed(Main2Activity.java:70) at android.app.Activity.onKeyUp(Activity.java:2495) at android.view.KeyEvent.dispatch(KeyEvent.java:2695) at android.app.Activity.dispatchKeyEvent(Activity.java:2746) at android.support.v7.app.AppCompatActivity.dispatchKeyEvent(AppCompatActivity.java:533) at android.support.v7.view.WindowCallbackWrapper.dispatchKeyEvent(WindowCallbackWrapper.java:53) at android.support.v7.app.AppCompatDelegateImplBase$AppCompatWindowCallbackBase.dispatchKeyEvent(AppCompatDelegateImplBase.java:254) at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:2221) at android.view.ViewRootImpl$ViewPostImeInputStage.processKeyEvent(ViewRootImpl.java:3918) at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3880) at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3449) at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3502) at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3468) at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3578) at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3476) at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3635) at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3449) at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3502) at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3468) at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3476) at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3449) at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3502) at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3468) at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3611) at android.view.ViewRootImpl$ImeInputStage.onFinishedInputEvent(ViewRootImpl.java:3772) at android.view.inputmethod.InputMethodManager$PendingEvent.run(InputMethodManager.java:2244) at android.view.inputmethod.InputMethodManager.invokeFinishedInputEventCallback(InputMethodManager.java:1885) at android.view.inputmethod.InputMethodManager.finishedInputEvent(InputMethodManager.java:1876) at android.view.inputmethod.InputMethodManager$ImeInputEventSender.onInputEventFinished(InputMethodManager.java:2221) at android.view.InputEventSender.dispatchInputEventFinished(InputEventSender.java:141) at android.os.MessageQueue.nativePollOnce(Native Method) at android.os.MessageQueue.next(MessageQueue.java:143) at android.os.Looper.loop(Looper.java:122) at android.app.ActivityThread.main(ActivityThread.java:5264) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:900) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:695)

1 Answers1

1

ArrayAdapter.notifyDataSetChanged() is been called BUT ArrayAdapter instance is pointing to a null reference (is not initialized or wrongly initialized...)

in your case it looks like the 2nd option is the cause...

you have this in the code:

adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, notesArrayList);

but you need to check the parameters in the constructor the instance: this the resourcesin layout: android.R.layout.simple_list_item_1 or the list: notesArrayList

at least one of those is breaking the correct initialization of the adapter, which means, after that line adapter still is null

the consecuence is: calling adapter.notifyDataSetChanged() will throw a NPE

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97