2
votes

I am working on part two of the big nerd ranch guide's Android Programming. For some reason when I try to run the app it crashes on start and log cat says a null pointer exception was thrown on line:

UUID crimeID = (UUID)getArguments().getSerializable(EXTRA_CRIME_ID);

Why is EXTRA_CRIME_ID returning a null value?

Here is my Crime List Fragment where the exception was thrown:

package com.bignerdranch.android.criminalintent;

import java.util.UUID;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.Editable;
import android.text.TextWatcher;
import android.text.format.DateFormat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;

public class CrimeFragment extends Fragment {
    public static final String EXTRA_CRIME_ID =
           "com.bignerdranch.android.criminalintent.crime_id";

    private Crime mCrime;
    private EditText mTitleField;
    private Button mDateButton;
    private CheckBox mSolvedCheckBox;

    @Override 
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        **UUID crimeID = (UUID)getArguments().getSerializable(EXTRA_CRIME_ID);**  
        mCrime = CrimeLab.get(getActivity()).getCrime(crimeID);   

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState){
        View v =inflater.inflate(R.layout.fragment_crime, parent, false);
        mTitleField = (EditText)v.findViewById(R.id.crime_title);
        mTitleField.setText(mCrime.getTitle());
        mTitleField.addTextChangedListener(new TextWatcher() {
            public void onTextChanged(CharSequence c, int start, int before, int count){
            mCrime.setTitle(c.toString());
        }

        public void beforeTextChanged(CharSequence c, int start, int count, int after){
            //left  blank
        }

        public void afterTextChanged(Editable c){
            //this one to
        }
    });

    mDateButton = (Button)v.findViewById(R.id.crime_date);
    mDateButton.setText(DateFormat.format("hh:mm:ss", mCrime.getDate()));
    mDateButton.setEnabled(false);

    mSolvedCheckBox = (CheckBox)v.findViewById(R.id.crime_solved);
    mSolvedCheckBox.setChecked(mCrime.isSolved());
    mSolvedCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            //set the crimes solved property
            mCrime.setSolved(isChecked);
        }
    });

        return v;
    }

}
3
Your getArguments() method is likely returning null, which you are trying to dereference on the same line. - Kon
Perhaps this one could help you: stackoverflow.com/questions/16561817/… - Leandro Carracedo

3 Answers

0
votes

I had not yet defined any arguments so my getArguments() was returning null. I should have used

UUID crimeId = (UUID)getActivity().getIntent().getSerializableExtra(EXTRA_CRIME_ID);

instead.

----Update 12/16/2014----

As pointed out by njzk2 I linked the fragment to the activity which prevents it from being used in another activity. The BigNerdRanch book links it to the activity in earlier chapters for simplicity reasons then has the reader change it latter and add the method:

public static CrimeFragment newInstance(UUID crimeId){

    Bundle args = new Bundle();
    args.putSerializable(EXTRA_CRIME_ID, crimeId);

    CrimeFragment fragment = new CrimeFragment();
    fragment.setArguments(args);

    //pass UUID from extra
    return fragment;
}

and change UUID crimeId = (UUID)getActivity().getIntent().getSerializableExtra(EXTRA_CRIME_ID); to

 UUID crimeId = (UUID)getArguments().getSerializable(EXTRA_CRIME_ID);
0
votes

I had this specific problem when going through the book you mentioned. Just delete code from your CrimeActivity. When you delete excess code from the activity, the one should look like this.

public class CrimeListActivity extends SingleFragmentActivity {
    private ArrayList<Crime> mCrimes;

    @Override
    protected Fragment createFragment() {
        return new CrimeListFragment();
    }}

I guess you did solve this by yourself in last five years, but still someone could run in similar problem. Also I would recommend the book, it shows some solid examples and it is pretty straightforward.

-2
votes

find CrimeActivity.java file then delete onCreate() function.