public void reader(String epc) { String nickName = epc.replaceAll("@+", ""); String code = nickName.substring(0, nickName.length() - 7); ImportViewModel model = list.stream().filter(e -> (e.getStyle() + e.getType()).equalsIgnoreCase(code)).findFirst().orElse(null); model.setNum(model.getNum() + 1); ImportViewModelAdapter adapter = new ImportViewModelAdapter(list); rvList.setAdapter(adapter); } public class InventoryRFID implements RfidEventsListener { @Override public void eventReadNotify(RfidReadEvents rfidReadEvents) { TagData[] tags = reader.Actions.getReadTags(1000); if (tags == null) { return; } for (final TagData tag : tags) { startBeepingTimer(); if (epcList.add(tag.getTagID())) { runOnUiThread(new Runnable() { @Override public void run() { String epc = tag.getTagID(); if (epc != null && /*epc.length()==24 && */epc.startsWith("40")) { String hexEpc = hexToAscii(epc); Log.e("tag", tag.getTagID()); reader(hexEpc); } } }); } } }java.lang.NullPointerException: Attempt to invoke virtual method 'int com.ives.upcrfid_zebra.entiry.ImportViewModel.getNum()' on a null object reference怎么解决
时间: 2024-01-01 09:03:56 浏览: 91
liaotianshi.rar_Java 8_java nickname_liaotianshi
这个错误是因为您在调用 `getNum()` 方法时,`model` 对象为 `null`,导致空指针异常。可能是因为在 `list` 中没有找到符合条件的 `ImportViewModel` 对象。
建议您在调用 `getNum()` 方法前,先对 `model` 进行非空判断,例如:
```
if (model != null) {
model.setNum(model.getNum() + 1);
}
```
这样可以避免空指针异常的出现。
阅读全文