NullPointerException error on Fragment Transaction


#1

Hi, i am following the book as closely as possible but when i got to the end of chapter 7 i tested my code i had written in the previous chapter and i got a NullPointerException error.

I looked over my code and when i commented out this line of code the programme ran fine, why is this

Here is the rest of CrimeActivity

[code]package com.shoutr.criminalintent;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;

public class CrimeActivity extends FragmentActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	
	setContentView(R.layout.activity_crime);
	
	FragmentManager fm = getSupportFragmentManager();

	Fragment fragment = fm.findFragmentById(R.id.fragmentContainer);

	if(fragment == null){

		fragment = new CrimeFragment();

		fm.beginTransaction().add(R.id.fragmentContainer, fragment).commit();
	}
}

}[/code]

And here is the code for CrimeFragment in case you need it

[code]package com.shoutr.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.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;

public class CrimeFragment extends Fragment {

private Crime mCrime;

private EditText mTitleField;

private Button mDateButton;

private CheckBox mSolvedCheckBox;


@Override
public void onCreate(Bundle savedInstanceState){

	super.onCreate(savedInstanceState);

	mCrime = new Crime();
	
}

@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.addTextChangedListener(new TextWatcher(){

		@Override
		public void afterTextChanged(Editable c) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void beforeTextChanged(CharSequence c, int start,
				int count, int after) {
			// TODO Auto-generated method stub
			
		}
		@Override
		public void onTextChanged(CharSequence c, int start, int before, int count) {
			//set the contents of the text box to the crime title
			mCrime.setTitle(c.toString());
			
		}

	});

	mDateButton = (Button)v.findViewById(R.id.crime_date);
	
	mDateButton.setText(mCrime.getDate().toString());
	
	mDateButton.setEnabled(false);
	
	
	mSolvedCheckBox = (CheckBox)v.findViewById(R.id.crime_solved);
	
	mSolvedCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener(){

		@Override
		public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
			// TODO Auto-generated method stub
			
			//set the new solved status
			mCrime.setSolved(isChecked);
		}	
	});
	return v;
}

}[/code]

And here is the error

Thanks


#2

Is this line returning null for some reason?

Can you add a log statement to check? I’m wondering if fragment is null when you commit the transaction.


#3

Hi, ok i ran the Log test and i got this result

Which i think is saying it is null

Here is the code i added to run the log test

This is the constant at the top

This is the log test

By the way i was looking at it today and when i clicked on the error in the logcat it took me to this line

I commented out that line and it still crashed so not sure why it is doing that.

Thanks for the help


#4

Ok, i was just playing around again and this line here is causing the problem

When i comment it out the app works fine, when i dont comment it it crashes

Here is the getDate() code

public Date getDate(){ return mDate; }

Thanks


#5

OK, start breaking it down. Will this run without crashing?


#6

Hi, ok i feel that were finally getting close to solving this

This code caused the programme to run correctly with no errors

I then added the below code without the .toString() and it also worked correctly, so i am guessing it has something to do with the .toString()?

Thanks for the help so far


#7

Ok, i managed to solve it, looks like i made a bit of a silly error, in the crime.java in the crime constructor i hadn’t added the code

Looks like i missed it accidentally, stupid error but i guess thats learning for ya.

Thanks for the help though