本文介绍了尝试在空对象引用上调用虚拟方法 'boolean java.util.ArrayList.add的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我正在创建一个 Android 应用程序.问题是当我按下按钮并将数据传递给新数组时,它会变黑并给我这个错误: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)我的 ExampleAdapter 类: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(); }}我的 Visualizza 课程: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(); } }}推荐答案请在使用前初始化arraylist private ArrayList<ExampleItemRecyclerview> mExampleList = new ArrayList<>(); 这篇关于尝试在空对象引用上调用虚拟方法 'boolean java.util.ArrayList.add的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
09-02 18:46