简体   繁体   中英

Attempt to invoke virtual method 'android.view.View android.widget.ImageView.findViewById(int)' on a null object reference

I am trying to retrieve data from firebase but my app is keep crashing. i have tried many solution but nothing is working. In my app i am trying to retrieve name,image,descriptions,price form my Firebase database. I have written this flowing code to display data in Card view.

my code :- Main Activity

public class MainActivity extends AppCompatActivity {
private DatabaseReference ProductsRef;
private RecyclerView recyclerView;
RecyclerView.LayoutManager layoutManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ProductsRef = FirebaseDatabase.getInstance().getReference().child("Products");
    recyclerView = findViewById(R.id.recycler_menu);
    recyclerView.setHasFixedSize(true);
    layoutManager= new LinearLayoutManager(this);
    recyclerView.setLayoutManager(layoutManager);
    FirebaseRecyclerOptions<Products> options =
            new FirebaseRecyclerOptions.Builder<Products>()
                    .setQuery(ProductsRef, Products.class)
                    .build();
    FirebaseRecyclerAdapter<Products, ProductViewHolder> adapter =
            new FirebaseRecyclerAdapter<Products, ProductViewHolder>(options) {
                @Override
                protected void onBindViewHolder(@NonNull ProductViewHolder holder, int position, @NonNull Products model) {
                    holder.textProductName.setText(model.getName());
                    holder.textProductDescription.setText(model.getDescription());
                    holder.textProductPrice.setText("Price = ₹"+model.getPrice());
                    Picasso.get().load(model.getImage()).into(holder.imageView);
                }

                @NonNull
                @Override
                public ProductViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.product_items_layout, parent,false);
                    ProductViewHolder holder = new ProductViewHolder(view);
                    return holder;
                }
            };
    recyclerView.setAdapter(adapter);
    adapter.startListening();
}

}

Products Activity

public class Products {
private String  description, image,name,price ;
public Products() {
}

public String getDescription() {
    return description;
}

public void setDescription(String description) {
    this.description = description;
}

public String getImage() {
    return image;
}

public void setImage(String image) {
    this.image = image;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getPrice() {
    return price;
}

public void setPrice(String price) {
    this.price = price;
}

}

ProductViewHolder Activity

              [enter image description here][1]public class ProductViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public TextView textProductName, textProductDescription, textProductPrice;
public ImageView imageView;
public ProductViewHolder(@NonNull View itemView) {
    super(itemView);
    imageView= imageView.findViewById(R.id.product_image);
    textProductName = textProductName.findViewById(R.id.product_name);
    textProductDescription = textProductDescription.findViewById(R.id.product_description);
    textProductPrice = textProductPrice.findViewById(R.id.product_price);
}

@Override
public void onClick(View v) {

}

}

It seems that your assignments at ProductViewHolder are wrong, try this:

public ProductViewHolder(@NonNull View itemView) {
super(itemView);
imageView= itemView.findViewById(R.id.product_image);
textProductName = itemView.findViewById(R.id.product_name);
textProductDescription = itemView.findViewById(R.id.product_description);
textProductPrice = itemView.findViewById(R.id.product_price);}

Hope I helped!

Attempt to invoke virtual method 'android.view.View android.widget.ImageView.findViewById(int)' on a null object reference

findViewById() is nullable , so it might return a null value.. make sure that the ImageView resource id product_name exists in your R.layout.product_items_layout layout and not in any other layouts in your project.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

Related Question Android Listview: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference FacebookSdk Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference Attempt to invoke virtual method 'android.view.View com.google.android.exoplayer2.ui.PlayerView.findViewById(int)' on a null object reference I'm getting this error: Attempt to invoke virtual method 'android.view.View android.widget.Button.findViewById(int)' on a null object reference java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View .MainActivity.findViewById(int)' on a null object reference How to fix attempt to invoke virtual method 'android.view.View androidx.fragment.app.FragmentActivity.findViewById(int)' on a null object reference? java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.app.Activity.findViewById(int)' on a null object reference Android:Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM