title: Null pointer, Context in activity intent for bottom navigation
body: <p>This app has two activities and I want to switch between them with the BottomNavigationView. Declaring an intent inside the switch statement throws a null pointer exception. MainActivity.java was changed to SpecialsActivity.java. The second activity is PizzaActivity.  The bottom navigation is controlled with BottomNavigationBuilder.java. </p>
  
  <p>example stack trace: </p>
  
  <pre><code>W/System.err: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference
  </code></pre>
  
  <p>what I've tried:</p>
  
  <pre><code>Intent PizzaIntent = new Intent(this, PizzaActivity.class);
                                      //Intent PizzaIntent = new Intent(getCallingActivity(), PizzaActivity.class);
                                      //Intent PizzaIntent = new Intent( getBaseContext(),PizzaActivity.class);
                                      //Intent PizzaIntent = new Intent(BottomNavigationBuilder.this, PizzaActivity.class);
                                      //Intent PizzaIntent = new Intent(getApplicationContext(), PizzaActivity.class);
                                      startActivity(PizzaIntent);
  </code></pre>
  
  <p>BottomNavigationBuilder.java</p>
  
  <pre><code>public class BottomNavigationBuilder extends AppCompatActivity {
  
      private Context context;
      private BottomNavigationView bottomNavigation;
  
      public BottomNavigationBuilder(Context context, BottomNavigationView findViewById) {
          this.context = context;
          this.bottomNavigation = findViewById;
      }
  
      public BottomNavigationBuilder setBaseConfig() {
          setTextVisible();
          setSelectedListener();
          return this;
      }
  
      private void setTextVisible() {
          bottomNavigation.setLabelVisibilityMode(LabelVisibilityMode.LABEL_VISIBILITY_LABELED);
      }
  
      public void displayToast(String message) {
          Toast.makeText(this.context, message,
                  Toast.LENGTH_SHORT).show();
      }
  
      private void setSelectedListener() {
          BottomNavigationView.OnNavigationItemSelectedListener navigationItemSelectedListener =
                  new BottomNavigationView.OnNavigationItemSelectedListener() {
  
                      String url;
  
                      @Override
                      public boolean onNavigationItemSelected(@NonNull MenuItem item) {
  
                          switch (item.getItemId()) {
  
                              case R.id.action_specials:
                                  displayToast("Specials clicked");
                                  break;
  
                              case R.id.action_pizza:
                                  displayToast("Pizza clicked");
  
                                  try {
                                      Intent PizzaIntent = new Intent(this, PizzaActivity.class);
                                      //Intent PizzaIntent = new Intent(getCallingActivity(), PizzaActivity.class);
                                      //Intent PizzaIntent = new Intent( getBaseContext(),PizzaActivity.class);
                                      //Intent PizzaIntent = new Intent(BottomNavigationBuilder.this, PizzaActivity.class);
                                      //Intent PizzaIntent = new Intent(getApplicationContext(), PizzaActivity.class);
                                      startActivity(PizzaIntent);
                                  } catch (Exception e) {
                                      e.printStackTrace();
                                  }
  
  
                                  break;
  
                              case R.id.action_stromboli:
                                  displayToast("Stromboli clicked");
                                  break;
  
                              case R.id.action_salad:
                                  displayToast("Salad clicked");
                                  break;
  
                              case R.id.action_drinks:
                                  displayToast("Drinks clicked");
                                  break;
  
                              default:
                                  // none
                          }
                          return true;
                      }
                  };
          bottomNavigation.setOnNavigationItemSelectedListener(navigationItemSelectedListener);
      }
  
      public BottomNavigationView getBottomNavigation() {
          return bottomNavigation;
      }
  }
  </code></pre>
  
  <p>SpecialsActivity.java</p>
  
  <pre><code>public class SpecialsActivity extends AppCompatActivity {
  
      private BottomNavigationView bottomNavigation;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_specials);
          setItemId();
          setView();
      }
  
      private void setItemId() {
          bottomNavigation = findViewById(R.id.bottom_nav);
      }
  
      private void setView() {
          initNavigationView();
      }
  
      private void initNavigationView() {
          bottomNavigation = new BottomNavigationBuilder(this, bottomNavigation)
                  .setBaseConfig()
                  .getBottomNavigation();
      }
  
  }
  </code></pre>