title: Android fragment listener null object reference
body: <p>Hello i am trying to add an listener to my messages fragment but its giving me a null pointer exception for some reason.</p>
  <p>my fragment</p>
  <pre><code>
  public class MessageFragment extends Fragment {
  
      OnItemSelectedListener Listener;
  
      @Nullable
      @Override
      public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
  
          View view = inflater.inflate(R.layout.fragment_message, container, false);
  
  
          List&lt;String&gt; dummyList = new ArrayList&lt;&gt;();
  
          for (int i = 0; i &lt; DummyDataProvider.getDummyData().size(); i++) {
              dummyList.add(DummyDataProvider.getDummyData().get(i).getDummyData() +&quot; &quot;+ i);
          }
  
          ListView listView = (ListView) view.findViewById(R.id.messageFragmentListView);
  
          ArrayAdapter&lt;String&gt; listAdapter = new ArrayAdapter&lt;String&gt;(getActivity(), android.R.layout.simple_list_item_1,dummyList);
  
          listView.setAdapter(listAdapter);
          listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
              @Override
              public void onItemClick(AdapterView&lt;?&gt; parent, View view, int position, long id) {
                  Listener.onItemSelectedMessageFragment(position);
              }
          });
  
          return view;
          //this method inflates my layout to the java class
      }
  
  
      public interface OnItemSelectedListener{
          public void onItemSelectedMessageFragment(int position);
      }
  
  }
  
  
  </code></pre>
  <p>I have implemented the interface in the main activity like so</p>
  <pre><code>public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener, MessageFragment.OnItemSelectedListener
  </code></pre>
  <p>this is my overide</p>
  <pre><code> @Override
      public void onItemSelectedMessageFragment(int position) {
          Toast.makeText(this, &quot;this is the position&quot; + position, Toast.LENGTH_SHORT).show();
      }
  
  </code></pre>
  <p>the fragment prints data fine but when i click the listener it throws an exception</p>
  <pre><code>E/AndroidRuntime: FATAL EXCEPTION: main
      Process: firebaseapp.com.mygbfapp, PID: 10995
      java.lang.NullPointerException: Attempt to invoke interface method 'void firebaseapp.com.mygbfapp.fragments.MessageFragment$OnItemSelectedListener.onItemSelectedMessageFragment(int)' on a null object reference
          at firebaseapp.com.mygbfapp.fragments.MessageFragment$1.onItemClick(MessageFragment.java:47)
          at android.widget.AdapterView.performItemClick(AdapterView.java:374)
          at android.widget.AbsListView.performItemClick(AbsListView.java:1736)
          at android.widget.AbsListView$PerformClick.run(AbsListView.java:4207)
          at android.widget.AbsListView$7.run(AbsListView.java:6692)
          at android.os.Handler.handleCallback(Handler.java:883)
          at android.os.Handler.dispatchMessage(Handler.java:100)
          at android.os.Looper.loop(Looper.java:237)
          at android.app.ActivityThread.main(ActivityThread.java:8167)
          at java.lang.reflect.Method.invoke(Native Method)
          at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:496)
          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1100)
  I/Process: Sending signal. PID: 10995 SIG: 9
  
  </code></pre>
  <p>and i am not really sure why can someone explain this to me.</p>