title: NullPointerException - attempt to invoke virtual method `notifyDataSetChanged()`
body: <p>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 <code>MainActivity.adapter.notifyDataSetChanged</code> in the <code>Main2Activity</code> (Edit Text Activity). The app crashes when after installing during pressing the back button in the EditText actitvity i.e. <code>Main2Activity.java</code>.</p>
  
  <p><strong>MainActivity:</strong></p>
  
  <pre><code>public class MainActivity extends AppCompatActivity {
  
  ListView notesListView;
  static ArrayList&lt;String&gt; notesArrayList;
  static ArrayAdapter&lt;String&gt; adapter;
  
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
  
      notesListView = (ListView) findViewById(R.id.notesListView);
      notesArrayList = new ArrayList&lt;String&gt;();
  
          adapter = new ArrayAdapter&lt;String&gt;(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&lt;?&gt; 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&lt;?&gt; 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;
  
  }
  }
  </code></pre>
  
  <p><strong>Main2Activity (EditText Activity):</strong></p>
  
  <pre><code>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();
  }
  }
  </code></pre>
  
  <p>Log:</p>
  
  <blockquote>
    <p>07-02 18:45:40.402 17500-17500/com.akhileshchobey.mynotes
    E/MessageQueue-JNI: java.lang.<strong>NullPointerException</strong>: 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)</p>
  </blockquote>