I have three linked lists called "Titles" (String linked list), "Descriptions" (String linked list), and "Pictures" (Bitmap linked list). I'm 100% sure that they are full of data. I've printed out their data just before the line MyAdapter myAdapter = new MyAdapter(this, Titles, Descriptions, Pictures);
and it has exactly the data I expect ("MyAdapter" is a class). However, in the MyAdapter class, all three linked lists are null, as I get the following error:
2022-08-07 19:13:03.009 14983-14983/com.example.usshop E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.usshop, PID: 14983
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.util.LinkedList.add(java.lang.Object)' on a null object reference
at com.example.usshop.MyAdapter.<init>(MyAdapter.java:30)
at com.example.usshop.Shop.CreateItems(Shop.java:70)
at com.example.usshop.Shop$1.onDataChange(Shop.java:56)
at com.google.firebase.database.core.ValueEventRegistration.fireEvent(ValueEventRegistration.java:75)
at com.google.firebase.database.core.view.DataEvent.fire(DataEvent.java:63)
at com.google.firebase.database.core.view.EventRaiser$1.run(EventRaiser.java:55)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:237)
at android.app.ActivityThread.main(ActivityThread.java:8167)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:496)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1100)
Below is the code of the two classes (Note: for the "Shop" class, the "CreateItems" method mainly matters because it sends the data to the other class. Included the rest in case it somehow affects the issue):
public class Shop extends AppCompatActivity {
RecyclerView rv;
//Below is unimportant; ignore.
/*
String s1[], s2[];
int images[] = {R.drawable.apple, R.drawable.banana, R.drawable.graoe,
R.drawable.orange, R.drawable.pineapple, R.drawable.peach};
*/
LinkedList<String> Titles = new LinkedList<>(), Descriptions = new LinkedList<>();
LinkedList<Integer> Prices = new LinkedList<>();
LinkedList<Bitmap> Pictures = new LinkedList<>();
DatabaseReference ItemsReference = FirebaseDatabase.getInstance().getReference("Items");
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_shop);
//Whenever the value of ItemsReference changes, it is stored
ItemsReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
// Storing all of the data
for (DataSnapshot DS : snapshot.getChildren()) {
BackendDataStorage TempData = DS.getValue(BackendDataStorage.class);
Titles.add(TempData.getTitle());
Descriptions.add(TempData.getDescription());
Prices.add(TempData.getPrice());
Pictures.add(Extras.StringToBitMap(TempData.getPicture()));
}
CreateItems();
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
Log.d("Error", "Failed to read value: " error.getMessage());
}
});
}
public void CreateItems () {
Log.d("index", Titles.get(0)); // Just making sure Titles isn't null
rv = findViewById(R.id.Recycler_View);
MyAdapter myAdapter = new MyAdapter(this, Titles, Descriptions, Pictures);
rv.setAdapter(myAdapter);
rv.setLayoutManager(new LinearLayoutManager(this));
}
}
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
private LinkedList<String> data1, data2;
private LinkedList<Bitmap> images;
private Context context;
public MyAdapter (Context ct, LinkedList<String> s1, LinkedList<String> s2, LinkedList<Bitmap> img) {
context = ct;
// Looping through LinkedList indexes & replacing with new variables
for (int i = 0; i < s1.size(); i ) {
data1.add(s1.get(i));
data2.add(s2.get(i));
images.add(img.get(i));
}
}
}
CodePudding user response:
However, in the
MyAdapter
class, all three linked lists are null
No, they are not. You have misread the error message. The error is not about any of the arguments you pass to the MyAdapter
constructor, but about a member of the MyAdapter
class.
Note for instance that there is no error on evaluating s1.size()
in the for
loop heading, so s1
is not null
.
But the error is not about s1
, s2
or img
. It is about calling the add
method on a null
reference:
java.lang.NullPointerException: Attempt to invoke virtual method
boolean java.util.LinkedList.add(java.lang.Object)
on a null object reference
The line where add
is called, is here:
data1.add(s1.get(i));
So, data1
is null
. And this is indeed the case. Your class has a private instance member called data1
which has not been initialised. You need to do something like:
data1 = new LinkedList<String>();