尝试在空对象引用上调用虚拟方法'boolean java.util.ArrayList.add

时间:2019-05-19 21:53:16

标签: java android arraylist nullpointerexception

我正在创建一个Android应用程序。问题是,当我按下按钮并将数据传递到新数组时,它变成黑色,并给了我这个错误:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.util.ArrayList.add (java.lang.Object) 'on a null object reference

通过调试,它可以给我一些字符串:

mExampleList.add(Ticket)

我的ExampleAdapter类:

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();
    }

}

我的Visualizza类:

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();

        } 
    }
}

2 个答案:

答案 0 :(得分:1)

必须先初始化ArrayList,然后再向其中添加项目。在每个类的开头将以下行更改为此:


private ArrayList<ExampleItemRecyclerview>mExampleList = new ArrayList<ExampleItemRecyclerview>();

答案 1 :(得分:1)

请在使用它们之前初始化arraylist<>

    private ArrayList<ExampleItemRecyclerview> mExampleList = new ArrayList<>();