0

I am trying to display JSON data but it's giving null pointer exception. It can't get the context of custom single item that I'm inflating

public class HotelAdapter extends ArrayAdapter{
    private List<HotelModel> hotelModelList;
    private LayoutInflater layoutInflater;
    private int resource;

    public HotelAdapter(@NonNull Context context, int resource, @NonNull 
List<HotelModel> objects) {
        super(context, resource, objects);
        hotelModelList = objects;
        this.resource=resource;
        layoutInflater = 
(LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull 
ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView==null){
            holder =new ViewHolder();
            convertView = layoutInflater.inflate(R.layout.item_hotel,null);

            holder.addr = 
(TextView)convertView.findViewById(R.id.hotel_add_mini);
            holder.categ = 
(TextView)convertView.findViewById(R.id.hotel_rating);
            holder.name = 
(TextView)convertView.findViewById(R.id.hotel_name);
            holder.price = 
(TextView)convertView.findViewById(R.id.hotel_price);
            holder.rating = 
(RatingBar)convertView.findViewById(R.id.ratingBar2);

        }
        else {
            holder= (ViewHolder) convertView.getTag();
        }


        holder.addr.setText(hotelModelList.get(position).getAddr());
        holder.categ.setText(hotelModelList.get(position).getCateg());
        holder.name.setText(hotelModelList.get(position).getName());
        holder.price.setText(Integer.toString(hotelModelList.get(position).getPrice()));
        holder.rating.setRating(hotelModelList.get(position).getRating());


        return convertView;
    }
    class ViewHolder{
        private ImageView img;
        private TextView name;
        private TextView addr;
        private TextView categ;
        private TextView price;
        private RatingBar rating;
    }
}

It can't get the reference of TextViews from item_hotel.xml layout files. So It's giving null pointer exception. I have checked the List<>, the list is non null so it's just the reference problem. Moreover, in this

holder.addr = 
(TextView)convertView.findViewById(R.id.hotel_add_mini);

The (TextView) is showing redundant but it shouldn't have as findViewById returns a view and we have to cast it into textView.

1
  • 2
    Starting with API 26, findViewById uses inference for its return type, so you no longer have to cast. It's return type is <T extends View> T not View anymore. Commented Dec 19, 2017 at 9:00

4 Answers 4

2

Replace this:

private List<HotelModel> hotelModelList;
private LayoutInflater layoutInflater;
private int resource;

with:

private List<HotelModel> hotelModelList;
private LayoutInflater layoutInflater;
private int resource;
private Context context;

then update your constructor and instead of this:

public HotelAdapter(@NonNull Context context, int resource, @NonNull 
List<HotelModel> objects) 
{
    super(context, resource, objects);
    hotelModelList = objects;
    this.resource=resource;
    layoutInflater =
    (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
}

Use following:

public HotelAdapter(@NonNull Context context, int resource, @NonNull 
List<HotelModel> objects) 
{
    super(context, resource, objects);
    this.hotelModelList = objects;
    this.resource=resource;
    this.context = context;
}

After that instead of following line:

convertView = layoutInflater.inflate(R.layout.item_hotel,null);

Use following two lines:

LayoutInflater inflater = ((Activity) context).getLayoutInflater();
convertView = inflater.inflate(R.layout.item_hotel, parent, false);

Remove following line also:

layoutInflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);

This will solve your issue you need to provide context to your layout inflater I think that might be the problem in your case.

Sign up to request clarification or add additional context in comments.

6 Comments

Apply above mentioned changes it will work do let me know for further assistance. Best of luck buddy :)
Oh buddy you are not setting context in your adapter you need to upgrade your constructor and create a cariable context of class Context. I will update my answer check in 2 mins do that you will good to go with then.
there is another thing wrong where you are calling this adapter you also have to pass list of your hotels. I will update that too.
All set now try my mentioned changes. this will work now :). Happy coding :)
Let me know if it works also let me know if you need further help.
|
1

You forgot to set the tag in your if condition and your're using tag in else condition that is the problem.

In your if condition add,

convertView.setTag(holder);

1 Comment

Yeah that worked. But the list view is showing just one result
0

Try to make a constructor and set context also.

      public class HotelAdapter extends ArrayAdapter{
           Context mcontext;
           private List<HotelModel> mhotelModelList;

           public HotelAdapter(Context context, private List<HotelModel> hotelModelList;){                 
                                super(context, R.layout.item_hotel, hotelModelList);
                                this.mcontext = context;
                                this.mhotelModelList = hotelModelList;
                            }

          @Override
          public View getView(int position, View convertView, ViewGroup parent) {
                    LayoutInflater vi = (LayoutInflater) 
                   mcontext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            if (convertView == null) {
                 convertView = mInflater.inflate(R.layout.item_hotel, null);
                 holder = new ViewHolder();
                  holder.addr =(TextView)convertView.findViewById(R.id.hotel_add_mini);         
                 convertView.setTag(holder);
         }

        else{
        holder = (ViewHolder) convertView.getTag();

          holder.addr.setText(hotelModelList.get(position).getAddr());
         return convertView;
     }
   }

2 Comments

explain this so peer can get some concept which you were write
Yeah sure @ Waleed Asim
0

Basically the problem is in this line

layoutInflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);

try to use context which you are getting in constructor of your HotelAdapter

layoutInflater = (LayoutInflater)context.getSystemService(LAYOUT_INFLATER_SERVICE);

and set tag on convertView

if (convertView == null) {
            convertView = mInflater.inflate(R.layout.item_hotel, null);
            holder = new ViewHolder();
            holder.addr = (TextView) convertView.findViewById(R.id.hotel_add_mini);
            holder.categ = (TextView) convertView.findViewById(R.id.hotel_rating);
            holder.name = (TextView) convertView.findViewById(R.id.hotel_name);
            holder.price = (TextView) convertView.findViewById(R.id.hotel_price);
            holder.rating = (RatingBar) convertView.findViewById(R.id.ratingBar2);
            convertView.setTag(holder);
        } else{
            holder= (ViewHolder) convertView.getTag();
        }

2 Comments

post your item layout which you are inflating and logs
java.lang.NullPointerException: Attempt to read from field 'android.widget.TextView com.bt4u.shopcite.hotel_populate_list$HotelAdapter$ViewHolder.addr' on a null object reference

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.