PHP User
PHP User

Reputation: 2422

Android ArrayAdapter null object reference error while scrolling up again

[update] Here's the link to the very simple project to view code Download Project

I'm creating a custom adapter with radio buttons. when the list view gets displayed I get no error but once I scroll up again to display the first hidden radio group I get

Attempt to invoke virtual method 'void android.widget.RadioButton.setText(java.lang.CharSequence)' on a null object reference

when I set the text for the first radio button in the new radio group

Here's the list_item

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<TextView
    android:id="@+id/q_text"
    android:text="question"
    style="@style/q_title"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

<RadioGroup
    android:id="@+id/q_answers"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <RadioButton
        android:text="text"
        android:layout_width="120dp"
        android:layout_height="wrap_content"
        android:id="@+id/q_option1"/>

    <RadioButton
        android:text="text"
        android:layout_width="120dp"
        android:layout_height="wrap_content"
        android:id="@+id/q_option2"/>

    <RadioButton
        android:text="text"
        android:layout_width="120dp"
        android:layout_height="wrap_content"
        android:id="@+id/q_option3"/>

</RadioGroup>

<Button
    style="@style/view_answer_btn"
    android:id="@+id/view_answer_btn"
    android:text="@string/view_answer"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

And the Adapter

public class QuestionsAdapter extends ArrayAdapter<Questions> {

    public QuestionsAdapter(Activity context, ArrayList<Questions> questions) {
        super(context, 0, questions);
    }

    @NonNull
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View listItemView = convertView;
        if (listItemView == null) {
            listItemView = LayoutInflater.from(getContext()).inflate(R.layout.list_item, parent, false);
        }

        Questions currentQuestion = getItem(position);

        TextView questionTextView = (TextView) listItemView.findViewById(R.id.q_text);
        questionTextView.setText( currentQuestion.getmQuestionTitle());

        RadioGroup radioGroup = (RadioGroup) listItemView.findViewById(R.id.q_answers);

        RadioButton firstAnswer = (RadioButton) listItemView.findViewById(R.id.q_option1);
        firstAnswer.setText( currentQuestion.getQuestionAnswer1() );
        firstAnswer.setId(Integer.parseInt(currentQuestion.getQuestionAnswer1()));

        RadioButton secondAnswer = (RadioButton) listItemView.findViewById(R.id.q_option2);
        secondAnswer.setText( currentQuestion.getQuestionAnswer2() );
        secondAnswer.setId(Integer.parseInt(currentQuestion.getQuestionAnswer2()));

        RadioButton thirdAnswer = (RadioButton) listItemView.findViewById(R.id.q_option3);
        thirdAnswer.setText( currentQuestion.getQuestionAnswer3() );
        thirdAnswer.setId(Integer.parseInt(currentQuestion.getQuestionAnswer3()));

        return listItemView;
    }
}

The error is fired at this line

firstAnswer.setText( currentQuestion.getQuestionAnswer1() );

Why I get the error here which means it can't find the firstAnswer radio button though the list is displayed correctly when running the activity before scrolling

enter image description here

Upvotes: 0

Views: 215

Answers (2)

Nalan Ulusoy
Nalan Ulusoy

Reputation: 46

When you scroll listview your radio button will be null with your code. Your project will work when you will fix your project just as below code.

    @NonNull
@Override
public View getView(int position, View convertView, ViewGroup parent) {

    View listItemView = convertView;
    if (listItemView == null) {
        listItemView = 
  LayoutInflater.from(getContext()).inflate(R.layout.list_item, parent, false);

        final ViewHolder viewHolder = new ViewHolder();
        try {
            Questions currentQuestion = getItem(position);

             viewHolder.questionTextView = (TextView) listItemView.findViewById(R.id.q_text);
             viewHolder.questionTextView.setText(currentQuestion.getmQuestionTitle());

             viewHolder.radioGroup = (RadioGroup) listItemView.findViewById(R.id.q_answers);

            viewHolder.firstAnswer = (RadioButton) listItemView.findViewById(R.id.q_option1);
            viewHolder.firstAnswer.setText(currentQuestion.getQuestionAnswer1());
            viewHolder.firstAnswer.setId(Integer.parseInt(currentQuestion.getQuestionAnswer1()));

            viewHolder.secondAnswer = (RadioButton) listItemView.findViewById(R.id.q_option2);
            viewHolder.secondAnswer.setText(currentQuestion.getQuestionAnswer2());
            viewHolder.secondAnswer.setId(Integer.parseInt(currentQuestion.getQuestionAnswer2()));
            viewHolder.thirdAnswer = (RadioButton) listItemView.findViewById(R.id.q_option3);
            viewHolder.thirdAnswer.setText(currentQuestion.getQuestionAnswer3());
            viewHolder.thirdAnswer.setId(Integer.parseInt(currentQuestion.getQuestionAnswer3()));

Button viewAnswer = (Button) listItemView.findViewById(R.id.view_answer_btn);
    viewAnswer.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

        }
    });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    else{
        viewHolder = (ViewHolder) listItemView.getTag();
    }

    return listItemView;
}
private class ViewHolder {
    TextView questionTextView;
    RadioGroup radioGroup;
    RadioButton firstAnswer, secondAnswer,thirdAnswer;
}

Upvotes: 1

Gavin Wright
Gavin Wright

Reputation: 3212

Try grabbing a reference to your RadioButtons like this instead. Notice the difference in the findViewById() calls:

public class QuestionsAdapter extends ArrayAdapter<Questions> {

public QuestionsAdapter(Activity context, ArrayList<Questions> questions) {
    super(context, 0, questions);
}

@NonNull
@Override
public View getView(int position, View convertView, ViewGroup parent) {

    View listItemView = convertView;
    if (listItemView == null) {
        listItemView = LayoutInflater.from(getContext()).inflate(R.layout.list_item, parent, false);
    }

    Questions currentQuestion = getItem(position);

    TextView questionTextView = (TextView) listItemView.findViewById(R.id.q_text);
    questionTextView.setText( currentQuestion.getmQuestionTitle());

    RadioGroup radioGroup = (RadioGroup) listItemView.findViewById(R.id.q_answers);

    RadioButton firstAnswer = (RadioButton) radioGroup.findViewById(R.id.q_option1);
    firstAnswer.setText( currentQuestion.getQuestionAnswer1() );
    firstAnswer.setId(Integer.parseInt(currentQuestion.getQuestionAnswer1()));

    RadioButton secondAnswer = (RadioButton) radioGroup.findViewById(R.id.q_option2);
    secondAnswer.setText( currentQuestion.getQuestionAnswer2() );
    secondAnswer.setId(Integer.parseInt(currentQuestion.getQuestionAnswer2()));

    RadioButton thirdAnswer = (RadioButton) radioGroup.findViewById(R.id.q_option3);
    thirdAnswer.setText( currentQuestion.getQuestionAnswer3() );
    thirdAnswer.setId(Integer.parseInt(currentQuestion.getQuestionAnswer3()));

    return listItemView;
}

}

Upvotes: 0

Related Questions