问题
I'm creating an Android application. The problem is that when I press the button and pass the data to the new array, it goes to black and giving me this error:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.util.ArrayList.add (java.lang.Object) 'on a null object reference
By debugging, it gives me something in the string:
mExampleList.add(Ticket)
My ExampleAdapter class:
public class ExampleAdapter extends RecyclerView.Adapter<ExampleAdapter.ExampleViewHolder>{
private ArrayList<ExampleItemRecyclerview>mExampleList;
public class ExampleViewHolder extends RecyclerView.ViewHolder{
public TextView mTextticket;
public TextView mTextredattore;
public TextView mTexttarga;
public TextView mTextdataA;
public TextView mTextmanutenzioneG;
public TextView mTextdataC;
public ExampleViewHolder(View itemView) {
super(itemView);
mTextticket = itemView.findViewById(R.id.txt_ticket);
mTextredattore = itemView.findViewById(R.id.txt_redattore);
mTexttarga = itemView.findViewById(R.id.txt_targa);
mTextdataA = itemView.findViewById(R.id.txt_dataA);
mTextmanutenzioneG = itemView.findViewById(R.id.txt_materiale);
mTextdataC = itemView.findViewById(R.id.txt_dataC);
}
}
public ExampleAdapter(ArrayList<ExampleItemRecyclerview>examplelist){
mExampleList = examplelist;
//todo creazione di una nuova ArrayList Item
//mExampleListFull = new ArrayList<>(examplelist);
}
@Override
public ExampleViewHolder onCreateViewHolder( ViewGroup parent , int view) {
//todo creazione variabile View
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.example_item_recyclerview,parent,false);
ExampleViewHolder evh = new ExampleViewHolder(v);
return evh;
}
@Override
public void onBindViewHolder(ExampleViewHolder holder, int position) {
ExampleItemRecyclerview currentItem = mExampleList.get(position);
holder.mTextticket.setText(Integer.toString(currentItem.getTicket()));
holder.mTextredattore.setText(currentItem.getRedattore());
holder.mTexttarga.setText(currentItem.getTarga());
holder.mTextdataA.setText(Integer.toString(currentItem.getDataApertura()));
holder.mTextmanutenzioneG.setText(currentItem.getMaterialeGuasto());
holder.mTextdataC.setText(Integer.toString(currentItem.getDataChiusura()));
}
@Override
public int getItemCount() {
return mExampleList.size();
}
}
My Visualizza class:
public class Visualizza extends AppCompatActivity {
ArrayList<ExampleItemRecyclerview> mExampleList;
RecyclerView mRecyclearView;
RecyclerView.Adapter mAdapter;
RecyclerView.LayoutManager mLayoutManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.recyclerview);
ArrayList<ExampleItemRecyclerview>examplelist = new ArrayList<>();
//examplelist = new ArrayList<>();
examplelist.add(new ExampleItemRecyclerview(123356,"ALESSANDRO","EP562WS",12052018,"Lampeggiante Guasto",23102018));
examplelist.add(new ExampleItemRecyclerview(34567,"FRANCESCO", "EP762NS",19052019,"Motore Guasto",21052019));
examplelist.add(new ExampleItemRecyclerview(34353,"ALESSANDRO","EP760WR",25052017,"Freni Guasti",10122018));
mRecyclearView = findViewById(R.id.miorecyclerView);
mRecyclearView.setHasFixedSize(true);
mLayoutManager = new LinearLayoutManager(this);
//mAdapter = new ExampleAdapter(mExampleList);
mAdapter = new ExampleAdapter(examplelist);
mRecyclearView.setLayoutManager(mLayoutManager);
mRecyclearView.setAdapter(mAdapter);
Bundle incomingMessages = getIntent().getExtras();
if(incomingMessages != null){
int ticket = Integer.parseInt(incomingMessages.getString("ticket"));
String redattore = incomingMessages.getString("redattore");
String targa = incomingMessages.getString("targa");
int dataA = Integer.parseInt(incomingMessages.getString("dataA"));
String materiale = incomingMessages.getString("materiale");
int dataC = Integer.parseInt(incomingMessages.getString("dataC"));
//TODO creazione nuova scheda Ticket
ExampleItemRecyclerview Ticket = new ExampleItemRecyclerview(ticket,redattore,targa,dataA,materiale,dataC);
mExampleList.add(Ticket);
mAdapter.notifyDataSetChanged();
}
}
}
回答1:
please initialize you arraylist<>
before using them
private ArrayList<ExampleItemRecyclerview> mExampleList = new ArrayList<>();
回答2:
You must initialize ArrayLists prior to adding items to them. Change the following line at the start of each of your classes to this:
private ArrayList<ExampleItemRecyclerview>mExampleList = new ArrayList<ExampleItemRecyclerview>();
来源:https://stackoverflow.com/questions/56212297/attempt-to-invoke-virtual-method-boolean-java-util-arraylist-add-on-a-null-obje