title: Attempt to invoke virtual method - NullPointerException
body: <p>I tried few of solved problems on this site about this instance of problems, but, seems like mine is specific, because it doesn't shows for instance of uninitialized object but on method that isn't even called.</p>
  
  <p>error log</p>
  
  <pre><code>java.lang.NullPointerException: Attempt to invoke virtual method 'void rs.ridjis.glosar.rjecnik.RjecnikCursorAdapter.changeCursor(android.database.Cursor)' on a null object reference
      at rs.ridjis.glosar.activities.MainActivity.changeCursor(MainActivity.java:75)
      at rs.ridjis.glosar.activities.MainActivity.onOptionsItemSelected(MainActivity.java:114)
      at android.app.Activity.onMenuItemSelected(Activity.java:2885)
      at android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:361)
      at android.support.v7.app.AppCompatActivity.onMenuItemSelected(AppCompatActivity.java:147)
      at android.support.v7.internal.view.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.java:100)
      at android.support.v7.internal.view.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.java:100)
      at android.support.v7.internal.app.ToolbarActionBar$2.onMenuItemClick(ToolbarActionBar.java:73)
      at android.support.v7.widget.Toolbar$1.onMenuItemClick(Toolbar.java:180)
      at android.support.v7.widget.ActionMenuView$MenuBuilderCallback.onMenuItemSelected(ActionMenuView.java:761)
      at android.support.v7.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:811)
      at android.support.v7.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:153)
      at android.support.v7.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:958)
      at android.support.v7.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:948)
      at android.support.v7.widget.ActionMenuView.invokeItem(ActionMenuView.java:619)
      at android.support.v7.internal.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java:139)
      at android.view.View.performClick(View.java:4789)
      at android.view.View$PerformClick.run(View.java:19881)
      at android.os.Handler.handleCallback(Handler.java:739)
      at android.os.Handler.dispatchMessage(Handler.java:95)
      at android.os.Looper.loop(Looper.java:135)
      at android.app.ActivityThread.main(ActivityThread.java:5294)
      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:904)
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)
  </code></pre>
  
  <p>MainActivity.java</p>
  
  <pre><code>public class MainActivity extends AppCompatActivity implements Communicator {
  public Toolbar toolbar;
  private RjecnikCursorAdapter adapter;
  private FragmentManager fragmentManager;
  private WordsListFragment wordsListFragment;
  
  private RjecnikDB dbRjecnik;
  private SQLiteDatabase db;
  private Cursor cursor;
  private final String QUERY_SELECTALL = "SELECT * FROM " + RjecnikDB.TABLE;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
  
      toolbar = (Toolbar) findViewById(R.id.toolbar);
      setSupportActionBar(toolbar);
  
      fragmentManager = getFragmentManager();
  
      wordsListFragment = new WordsListFragment();
  
      final FragmentTransaction ft = fragmentManager.beginTransaction();
      ft.add(R.id.fragment_container, wordsListFragment);
      ft.commit();
  }
  
  ///// Implementation interface methods /////
  public void changeCursor() {
      db = dbRjecnik.getWritableDatabase();
      cursor = db.rawQuery(QUERY_SELECTALL, null);
      adapter.changeCursor(cursor);
      db.close();
      Toast.makeText(this, "Lista ažurirana", Toast.LENGTH_SHORT).show();
  }
  
  public Cursor initCursor() {
      dbRjecnik = RjecnikDB.getInstance(getApplicationContext());
      db = dbRjecnik.getWritableDatabase();
      cursor = db.rawQuery(QUERY_SELECTALL, null);
      return cursor;
  }
  
  public void deleteOnLongClick(int id) {
      db = dbRjecnik.getWritableDatabase();
      db.delete(RjecnikDB.TABLE, RjecnikDB.COLUMN_ID + " = ?", new String[] { Integer.toString(id) } );
      db.close();
  }
  // END OF IMPLEMENTATION //
  
  @Override
  protected void onRestart() {
      super.onRestart();
      changeCursor();
  }
  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
      int id = item.getItemId();
  
      if (id == R.id.search_bar) {
          return true;
      } else if (id == R.id.refresh) {
          changeCursor();
      }
  
      return super.onOptionsItemSelected(item);
  }
  </code></pre>
  
  <p>As you can see from error log, problem is where shows <code>changeCursor()</code>, even if it's not called, <code>onOptionItemSelected</code>.
  What is best way to fix this? It's neither working when I call <code>cursor = initCursor()</code> before actionBar initialization.</p>