Home Ask Login Register

Developers Planet

Your answer is one click away!

Amandeep Singh February 2016

NullPointer while using dynamic view in android

I have created a dynamic view which repeats for a specific times. The view has 2 textViews and 2 radio buttons with default values. I want to change the Text values from program but getting nullpointer exception, Here is my code:

    LayoutInflater inflater = (LayoutInflater)context.getSystemService
    LinearLayout scrollViewLinearlayout = (LinearLayout)findViewById(R.id.parent); // The layout inside scroll view
    //int count=50;
    for(int i = 0; i < Studentlist.length; i++){
        String data = Studentlist[i];
        String RollNum = data.split("--")[0];
        String Name = data.split("--")[1];

        LinearLayout layout2 = new LinearLayout(context);
        layout2.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
        View item = inflater.inflate(R.layout.item_layout, null, false);

        TextView trollnum = (TextView)findViewById(R.id.RollNum);
        TextView tname = (TextView)findViewById(R.id.Name);

        Log.d("Id is: ", trollnum.getText().toString());

I am getting nullpointer at 'trollnum.setText(RollNum);'

How do i change the text in the View?


George Mulligan February 2016

You are trying to find the view in the activity's layout where it doesn't exist instead of finding it in the layout you just inflated.


TextView trollnum = (TextView)findViewById(R.id.RollNum);
TextView tname = (TextView)findViewById(R.id.Name);


TextView trollnum = (TextView) item.findViewById(R.id.RollNum);
TextView tname = (TextView) item.findViewById(R.id.Name);

Post Status

Asked in February 2016
Viewed 3,780 times
Voted 11
Answered 1 times


Leave an answer

Quote of the day: live life