1. ホーム
  2. アンドロイド

インターフェースメソッド 'java.lang.Object[] java.util.Collection.toArray()' を null obj で呼び出そうとした場合。

2022-02-24 21:42:42
<パス

遭遇しました。ある理由により、NULLオブジェクト参照でインターフェースメソッド 'java.lang.Object[] java.util.Collection.toArray()' を呼び出そうとしました。

デタラメは言わない、ただ文句を言う
今日、上記のエラーに遭遇し、その原因を発見するのに大変な労力を要しました。


私のシナリオはこんな感じです。

// Step 1, there is no data inside the liveBeanList at this point
private List<LiveDetailBean.LiveBean> liveBeanList=new ArrayList<>();

//use liveBeanList to set the adapter for the recyclerView
recyclerView.setAdapter(liveBeanList);

//request network data, get a List netList, then
liveBeanList.addAll(netList);

// then the above error appears, after looking, it turns out that because the netList loaded from the network is empty, so the above error appears. (In addition, if you then adapter.notifyDataSetChanged(), there will be a similar error)



要約すると、ネットワークListのデータを読み込む際、ローカルListにデータを追加する際、まずネットワークデータが空かどうかを判断し、空でなければローカルListに追加する必要があります。