Null object reference Android after OnCreateView : <p>I'm getting null pointer reference error on CrimeFragment .</p>

<p>crimeFragment's onCreateView method : </p>

<pre><code>@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view =  inflater.inflate(R.layout.fragment_crime, container, false);
    try {
    Button submit = (Button) view.findViewById(R.id.submitButton);
    submit.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Toast.makeText(view.getContext(), "Cant save yet!", Toast.LENGTH_SHORT).show();
            EditText id = (EditText) view.findViewById(R.id.crimeID);
            EditText des = (EditText) view.findViewById(R.id.crimeDesc);
            Crime crime = new Crime();

            String desS = des.getText().toString();
            String idS = (id.getText().toString());
            if(desS == null || idS == null){
                Toast.makeText(getContext(),"Blank fields !",Toast.LENGTH_SHORT).show();
                return;
            }
            crime.setId(Integer.parseInt(desS));
            crime.setDesc(desS);
            addCrime(crime);
        }
    });
}
    catch(Exception e){
        e.printStackTrace();
    }
    return view;
}
</code></pre>

<p>I have read the fragment lifetime documentation &amp; so have grabbed view components only after inflating them in OnCreateView. So why is it showing null object?</p>

<p>MainActivity class  : </p>

<pre><code> @Override
 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    FragmentManager fm = getSupportFragmentManager();
    CrimeFragment frag = (CrimeFragment) fm.findFragmentById(R.id.main_container);
    if(frag == null){
        frag  = new CrimeFragment();
        fm.beginTransaction()
                .add(R.id.main_container,frag)
                .commit();
    }
    List&lt;Crime&gt; crimeList = frag.getCrime();

}
</code></pre>

<p>any suggestions is most welcome.</p>
