EDIT : I know what a null pointer exception is. My issue is why is it happening because I have everything initialized.
I'm having an issue with my android app where I enter some text in a fragment, save the data into an object and pass said object back in a callback.
@BindView(R.id.new_post_title_til)
TextInputLayout mTitleTil;
@BindView(R.id.new_post_body_til)
TextInputLayout mBodyTil;
@BindView(R.id.new_post_title)
TextInputEditText mTitle;
@BindView(R.id.new_post_body)
TextInputEditText mBody;
@BindView(R.id.button)
Button send;
private Post mPost;
@OnClick(R.id.button)
public void submit() {
UserPost post = new UserPost.Builder()
.title(mTitle.getText().toString())
.body(mBody.getText().toString())
.build();
mPost.onUserPost(post);
getFragmentManager().popBackStack();
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_new_post, container, false);
ButterKnife.bind(this, v);
return v;
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
try {
mPost = (Post) context;
} catch (ClassCastException e) {
e.printStackTrace();
}
}
public interface Post {
void onUserPost(@NonNull UserPost post);
}
Whenever I hit the submit button I get the correct toast my app crashes with the error message. The data is being saved but for some reason, the app is still crashing whenever the submit button is pressed.