sorry_I_wont
sorry_I_wont

Reputation: 656

NullPointerException on a button click in Android

I am really stuck on this issue I have in my app, which causes a crash after a button click.

I have a singleton class of "Comment" objects(CommentLab class, saves comments in an arrayList), a CommentListFragment that has a listView of my comments, and PostCommentFragment for the purpose of adding new comments to the arrayList in CommentLab.

I added a OptionMenu to my CommentListFragment, and when I click on New Comment item in optoinMenu, I want it to start the CommentListFragment class where I can edit and add a new comment.

Here are the parts of code that are working together to make it possible:

In CommentListFragment:

// method to add an option menu to the fragment
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    super.onCreateOptionsMenu(menu, inflater);
    inflater.inflate(R.menu.fragment_comment_list, menu);
}

// method to indicate what to do when an item is selected on the menu(which,what)
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()){
        case R.id.menu_item_new_comment:
            Comment comment = new Comment();
            CommentLab.get(getActivity()).addComment(comment);
            Intent i = new Intent(getActivity(), PostCommentActivity.class);
            i.putExtra(PostCommentFragment.EXTRA_COMMENT, comment.getId());
            startActivity(i);
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

PostCommentActivity(a hosting activity for PostCommentFragment):

public class PostCommentActivity extends SingleFragmentActivity{

@Override
protected Fragment createFragment() {
    UUID commentId = (UUID)getIntent().getSerializableExtra(PostCommentFragment.EXTRA_COMMENT);
    return PostCommentFragment.newInstance(commentId);
}

}

PostCommentFragment:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    UUID commentId = (UUID)getArguments().getSerializable(EXTRA_COMMENT);
    mComment = CommentLab.get(getActivity()).getComment(commentId);
}

    @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_post_comment, container, false);
    mCommentEditText = (EditText) v.findViewById(R.id.title_edit_text);
    mCommentEditText = (EditText) v.findViewById(R.id.comment_edit_text);
    mDateTextView = (TextView) v.findViewById(R.id.date_text_view);
    mDateTextView.setText(mComment.getDate().toString());
    mPostButton = (Button) v.findViewById(R.id.post_button);

    mPostButton.setOnClickListener( new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String title = mTitleEditText.getText().toString();
            String comment = mCommentEditText.getText().toString();
            mComment.setTitle(title);
            mComment.setComment(comment);
        }
    });

    return v;
}

When I click the Post button in PostCommentFragment, the app crashes...here is the logcat:

08-01 00:39:29.720    7442-7442/com.example.ashl7.E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.NullPointerException
            at com.example.ashl7.PostCommentFragment$1.onClick(PostCommentFragment.java:46)
            at android.view.View.performClick(View.java:3534)
            at android.view.View$PerformClick.run(View.java:14263)
            at android.os.Handler.handleCallback(Handler.java:605)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4441)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
            at dalvik.system.NativeStart.main(Native Method)

Anyone can figure this out please?

Upvotes: 1

Views: 178

Answers (3)

Shriram
Shriram

Reputation: 4421

mCommentEditText = (EditText) v.findViewById(R.id.title_edit_text); 
mCommentEditText = (EditText) v.findViewById(R.id.comment_edit_text);  

Initializing twice ..

please check it

Upvotes: 1

Roger
Roger

Reputation: 1225

Becasuse you are using mTitleEditText without reference it with object R.id.title_edit_text

Simply Change the below code

mCommentEditText = (EditText) v.findViewById(R.id.title_edit_text);
mCommentEditText = (EditText) v.findViewById(R.id.comment_edit_text);

to

mTitleEditText = (EditText) v.findViewById(R.id.title_edit_text);
mCommentEditText = (EditText) v.findViewById(R.id.comment_edit_text);

It will work for you .

Upvotes: 1

yole
yole

Reputation: 97338

You made a copy-paste error. You're assigning to mCommentEditText twice, and never assigning to mTitleEditText.

Upvotes: 7

Related Questions