view raw
Kurlicue Kurlicue - 11 months ago 57
Android Question

Unexpected NullpointExpection inside a setter

I'm trying to do a simple operation: click a button and show a custom DialogFragment, but I'm getting a NullPointExpection and I can't figure out why.

public class mAlertDialog extends DialogFragment {

TextView title;

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.dialog_invalid_time, container, false);
title = (TextView)view.findViewById(;
return view;

public void setTheTitle(String title) {

Showing mAlertDialog:

mAlertDialog dialog = new mAlertDialog();
dialog.setTheTitle(getActivity().getString(R.string.invalidTimeTitle));, "InvalidTime");

Error message:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at m.inschool8.Objects.mAlertDialog.setTheTitle(
at m.inschool8.bSubjects.Fragment_Subjects$55.onClick(
at android.view.View.performClick(
at android.view.View$
at android.os.Handler.handleCallback(
at android.os.Handler.dispatchMessage(
at android.os.Looper.loop(
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(


Your title is null until the DialogFragment is shown causing onCreateView to kick-in. Hence change the order as below:

mAlertDialog dialog = new mAlertDialog();, "InvalidTime");