Keep getting java.lang.NullPointerException error


#1

This my CrimeFragment,java:

package com.bignerdranch.android.criminalintent;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.Editable;
import android.text.TextWatcher;
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.EditText;

/**

  • Created by tidalwave on 9/11/14.
    */
    public class CrimeFragment extends Fragment {
    private Crime mCrime;
    private EditText mTitleField; //initializes variable for listener
    //Initialize widget instance for mDateButton variable
    private Button mDateButton;
    //Initialize widget instance for mSolvedCheckBox variable
    private CheckBox mSolvedCheckBox;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_crime, parent, false);
    // Wiring up the EditText widget for the fragment
    mTitleField = (EditText)v.findViewById(R.id.crime_title);
    mTitleField.addTextChangedListener(new TextWatcher() {

         //show or store items once you start typing
         public void onTextChanged(CharSequence s, int start, int before, int count) {
             mCrime.setTitle(s.toString());
    
         }
    
         public void beforeTextChanged(CharSequence s, int start, int count, int after) {
             //this space left blank on purpose
         }
    
    
         public void afterTextChanged(Editable s) {
             //This space left blank on purpose
         }
     });
     //Setup text for mDateButton
    
    
     mDateButton = (Button)v.findViewById(R.id.crime_date);//get a reference for the new button
     mDateButton.setText(mCrime.getDate().toString());//set its text as the date of the crime
     mDateButton.setEnabled(false);//Disable the usage of date button by setting it to false
     // Disabling the button ensures that it will not respond in any way to the user pressing it
     //it also greys out the button so you know not to click on it
    
     //Setup listener of the crime solved checkbox (mSolvedCheckbox)
     mSolvedCheckBox = (CheckBox)v.findViewById(R.id.crime_solved);//get a reference for the new checkbox
     //assign mSolvedCheckBox a value once someone clicks on the checkbox
     //OnCheckedChangedListener is a part of the CompoundButton Class
     //Checkbox is a sub-class of CompoundButton
     mSolvedCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
         //@Override are not required for methods defined in interfaces
         public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
             //Set the crime's solved property
             mCrime.setSolved(isChecked);
         }
     });
     return v;
    

    }

}

This is my Crime.java:

package com.bignerdranch.android.criminalintent;

import java.util.Date;
import java.util.UUID;

/**

  • Created by tidalwave on 9/11/14.
    */
    public class Crime {

    //UUID is an immutable representation of a 128-bit universally unique identifier (UUID)
    private UUID mId;

    //declare String variable mTitle
    private String mTitle;

    //declare mDate as the variable to hold the Date
    private Date mDate;

    //Declare boolean variable mSolved to determine if a crime is solved or not
    private boolean mSolved;

    public Crime() {
    //Generate unique identifier
    mId = UUID.randomUUID();
    //setup mDate with a Data value
    mDate = new Date();

    }

    public UUID getId() {
    return mId;
    }

    public String getTitle() {
    return mTitle;
    }

    public void setTitle(String title) {
    mTitle = title;
    }
    //Getter for mDate
    public Date getDate() {
    return mDate;
    }
    //Setter for mDate
    public void setDate(Date date) {
    mDate = date;
    }
    //Getter for mSolved
    public boolean isSolved() {
    return mSolved;
    }
    //setter for mSolved
    public void setSolved(boolean solved) {
    mSolved = solved;
    }
    }

I’ve went over the code multiple times I can’t figure out what’s wrong. If you go into the log it always references this line as the problem:

mDateButton.setText(mCrime.getDate().toString());


#2

Can you tell which object reference is null? mCrime, mDateButton, or the Date object returned from Crime.getDate()? Maybe there’s a problem with your fragment_crime.xml layout file. May want to post that here as well.


#3

Here’s my fragment_crime.xml file:

<?xml version="1.0" encoding="utf-8"?>

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/crime_title_label"
    style="?android:listSeparatorTextViewStyle"
    />

<EditText
    android:id="@+id/crime_title"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="@string/crime_title_hint"
    />

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/crime_details_label"
    style="?android:listSeparatorTextViewStyle"/>

<Button
    android:id="@+id/crime_date"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="16dp"
    android:layout_marginRight="16dp"/>

<CheckBox
    android:id="@+id/crime_solved"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="16dp"
    android:layout_marginRight="16dp"
    android:text="@string/crime_solved_label"
    />

#4

Don’t see any obvious problems with your source code or layout file. How about setting a breakpoint in the debugger in onCreatView() right before mDateButton is assigned and then check if it’s null after assignment. If not, then repeat the process and this time step into the Crime getDate() method and inspect the mDate field. Is it null?