user68791 user68791 - 2 months ago 7
Java Question

Keep getting java.lang.NullPointerException but don't know why or how to fix it

Within these two class I keep getting a null pointer exception error. I have narrowed down to one line but I don't why or how to fix it.

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


Here is the class that this line is being called in:

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;

}




}


Here is the class that the line is calling:

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;

}




}


And here the log for the error:

1498-1498/com.bignerdranch.android.criminalintent E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.bignerdranch.android.criminalintent, PID: 1498
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.bignerdranch.android.criminalintent/com.bignerdranch.android.criminalintent.CrimeActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.bignerdranch.android.criminalintent.CrimeFragment.onCreateView(CrimeFragment.java:55)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1504)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:942)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1121)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1484)
at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:571)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1171)
at android.app.Activity.performStart(Activity.java:5241)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2168)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
            at android.app.ActivityThread.access$800(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5017)
            at java.lang.reflect.Method.invokeNative(Native Method)

Answer

The value of mCrime is never set so when you try to call a method for a variable which is never instantiated it will throw a null pointer exception.

mCrime.getDate().toString()

Solution is to instantiate mCrime or not to use it.

Comments