-2

I'm getting null pointer reference error on CrimeFragment .

crimeFragment's onCreateView method :

@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;
}

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

MainActivity class :

 @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<Crime> crimeList = frag.getCrime();

}

any suggestions is most welcome.

  • With due respect its not a duplicate and I have gone through other fragment null pointer exception articles here before.. I feel really bad for getting downvoted agaun and again just because my problem is similar to alot of people yet NOT same – Pragyan Choudhury Jan 20 '18 at 14:19
  • No problems are ever exactly the same, because this is programming. Unless you have copy-pasted someone's entire project, no matter what, you will get unique exceptions. NPEs have a single root: Something isn't initialized. Which it isn't as you initialize fields inside onClick instead of onCreate. It is a duplicate – Zoe Jan 20 '18 at 14:23
  • Didn't figure that out before...bdw thanks for taking ur time and mentioning it. I do have concept of annonymous inner classes in java but wasn't aware of this situation.. – Pragyan Choudhury Jan 20 '18 at 14:26

1 Answers1

0

move this editText casting code outside onClick

View view =  inflater.inflate(R.layout.fragment_crime, container, false);
try {
Button submit = (Button) view.findViewById(R.id.submitButton);
EditText id = (EditText) view.findViewById(R.id.crimeID);
EditText des = (EditText) view.findViewById(R.id.crimeDesc);

submit.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        Toast.makeText(view.getContext(), "Cant save yet!", Toast.LENGTH_SHORT).show();

        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);
    }
});
Munir
  • 2,548
  • 1
  • 11
  • 20