title: How to call a function from another class with its context (android, java)
body: <p>I'm trying to reload a viewPager whenever the user pushes a button inside an Item from the viewPager. The relevant classes for this case are <code>SetupActivity</code> and <code>Adapter</code>. In the activity class I've already made the two functions to reload the viewPager:</p>
  
  <pre><code>public void deleteSession(String title) {
          dbHelper = new DatabaseHelper(this);
          ((DatabaseHelper) dbHelper).deleteRow(title);
      }
  </code></pre>
  
  <p>and</p>
  
  <pre><code>public void refreshAdapter () {
  
          dbHelper = new DatabaseHelper(this); 
          Cursor dataCursor = ((DatabaseHelper) dbHelper).getData();
          models = new ArrayList&lt;&gt;(); 
  
          while (dataCursor.moveToNext()) { 
              models.add(new Model(
                      dataCursor.getString(1),
                      dataCursor.getInt(2),
                      dataCursor.getInt(3),
                      dataCursor.getInt(4)
              ));
          }
          adapter = new Adapter(models, this);
          viewPager.setAdapter(adapter);
          viewPager.setCurrentItem(0, true);
      }
  </code></pre>
  
  <p>So basically, <code>deleteSession()</code> deletes a specific item from a database. <code>refreshAdapter()</code> reads the updated data from the database and sets a new adapter to the viewPager.
  These functions are working fine, if I call them from an OnClick event in the <code>SetupActivity</code> class. However, if I try to call them from an OnClick event in the <code>Adapter</code> class, the app crashes with the following errors:</p>
  
  <pre><code>java.lang.NullPointerException: Attempt to invoke virtual method 'java.io.File android.content.Context.getDatabasePath(java.lang.String)' on a null object reference
          at android.content.ContextWrapper.getDatabasePath(ContextWrapper.java:327)
          at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:352)
          at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:322)
          at com.example.android.slink.DatabaseHelper.getData(DatabaseHelper.java:69)
          at com.example.android.slink.SetupActivity.refreshAdapter(SetupActivity.java:436)
          at com.example.android.slink.Adapter$3.onClick(Adapter.java:111)
          at android.view.View.performClick(View.java:6669)
          at android.view.View.performClickInternal(View.java:6638)
          at android.view.View.access$3100(View.java:789)
          at android.view.View$PerformClick.run(View.java:26145)
          at android.os.Handler.handleCallback(Handler.java:873)
          at android.os.Handler.dispatchMessage(Handler.java:99)
          at android.os.Looper.loop(Looper.java:193)
          at android.app.ActivityThread.main(ActivityThread.java:6863)
          at java.lang.reflect.Method.invoke(Native Method)
          at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:537)
          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
  </code></pre>
  
  <p>Here's my <code>SetupActiviy</code> class (reduced to the relevant parts):</p>
  
  <pre><code>package com.example.android.slink;
  
  public class SetupActivity extends AppCompatActivity {
  
      private SQLiteOpenHelper dbHelper;
      private SQLiteDatabase database;
  
      ViewPager viewPager;
      Adapter adapter;
      static ArrayList&lt;Model&gt; models;
  
      public void deleteSession(String title) {
          dbHelper = new DatabaseHelper(this);
          ((DatabaseHelper) dbHelper).deleteRow(title);
      }
  
      public void refreshAdapter () {
          dbHelper = new DatabaseHelper(this);
          Cursor dataCursor = ((DatabaseHelper) dbHelper).getData();
          models = new ArrayList&lt;&gt;();
          models.add(new Model("", 0,0,0));
          while (dataCursor.moveToNext()) {
              models.add(new Model(
                      dataCursor.getString(1),
                      dataCursor.getInt(2),
                      dataCursor.getInt(3),
                      dataCursor.getInt(4)
              ));
          }
          adapter = new Adapter(models, this);
          viewPager.setAdapter(adapter);
          viewPager.setCurrentItem(0, true);
      }
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_setup);
  
          editTextScreenTitle = findViewById(R.id.editTextScreenTitle);
          relativeLayoutCard = findViewById(R.id.relativeLayoutCard);
          viewPager = findViewById(R.id.viewPager);
  
          dbHelper = new DatabaseHelper(this);
          Cursor dataCursor = ((DatabaseHelper) dbHelper).getData();
  
          models = new ArrayList&lt;&gt;();
          models.add(new Model("", 0,0,0));
          while (dataCursor.moveToNext()) {
              models.add(new Model(
                      dataCursor.getString(1),
                      dataCursor.getInt(2),
                      dataCursor.getInt(3),
                      dataCursor.getInt(4)
              ));
          }
          adapter = new Adapter(models, this);
          viewPager.setAdapter(adapter);
      }
  }
  </code></pre>
  
  <p>Here's my <code>Adapter</code> class (also reduced to the relevant parts):</p>
  
  <pre><code>package com.example.android.slink;
  
  public class Adapter extends PagerAdapter {
  
      private List&lt;Model&gt; models;
      private LayoutInflater layoutInflater;
      private Context context;
  
      SetupActivity setupActivity = new SetupActivity();
  
      public Adapter(List&lt;Model&gt; models, Context context) {
          this.models = models;
          this.context = context;
      }
  
      @NonNull
      @Override
      public Object instantiateItem(@NonNull final ViewGroup container, final int position) {
          layoutInflater = LayoutInflater.from(context);
          final View view = layoutInflater.inflate(R.layout.item, container, false);
  
          TextView buttonDeleteCard = view.findViewById(R.id.textViewDeleteCard);
  
          buttonDeleteCard.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  setupActivity.deleteSession([...some title...]);
                  setupActivity.refreshAdapter();
              }
          });
  
          container.addView(view, 0);
          return view;
      }
  }
  </code></pre>
  
  <p>I think it has something to do whith the context.
  Do I have to pass the context from <code>SetupActiity</code> to the <code>Adapter</code> class and if so, how do I do that?
  Or is it not possible and I need to catch the OnClick event in the <code>Adapter</code> class from in the <code>SetupActivity</code> class?
  I'm new to java programming and would be very grateful if any of you could help me, solve this problem!</p>