function getStyle(element, attr) { if(window.getComputedStyle) { return window.getComputedStyle(element, null)[attr]; } else { return element.currentStyle[attr]; } } //可以使用 getStyle,补充代码 var scrollTop = window.pageYoffset || document.documentElement.scrollTop || document.body.scrollTop || 0; console.log(scrollTop); // 输出页面y的偏移量(即向下滑动多少距离) window.sessionStorage.setItem("lastname", "jxnu"); // 设置lastname为jxnu var storage = window.localStorage; storage["a"] = 1; storage.b = 1; storage.setItem("c", 3); console.log(storage);
时间: 2023-06-19 10:06:44 浏览: 43
这段代码主要分为三个部分:
1. `getStyle`函数:用于获取元素指定属性的样式值,跨浏览器兼容,可以用于补充其他代码。
2. `scrollTop`变量:用于获取页面向下滑动的距离,包括了不同浏览器的兼容处理。
3. localStorage和sessionStorage:分别用于在浏览器端存储数据的API,可以用来存储一些临时的或者长期的数据。
其中,`localStorage`和`sessionStorage`的区别在于前者存储的数据没有过期时间,后者存储的数据在关闭浏览器窗口或标签页后会被清除。在代码中,我们可以看到使用了`sessionStorage.setItem`方法来设置一个键值对,以及使用了`localStorage`对象来存储多个键值对。最后,`console.log`方法用于将结果输出到控制台。
相关问题
ImportViewModel model = list.stream().filter(e -> (e.getType() + e.getStyle()).equalsIgnoreCase(code)).findFirst().orElse(null); Attempt to invoke virtual method 'int com.ives.upcrfid_zebra.entiry.ImportViewModel.getNum()' on a null object reference
这个错误提示的意思是你在一个空对象上调用了 `getNum()` 方法,导致程序崩溃。
根据你提供的代码片段,问题出现在 `findFirst().orElse(null)` 这一行。这行代码的作用是在 `list` 集合中查找第一个满足条件的元素,如果找不到则返回 `null`。
因此,很有可能是 `list` 集合中没有满足条件的元素,导致 `orElse(null)` 返回了空对象,进而调用对象的 `getNum()` 方法时发生了空指针异常。
你可以在调用 `getNum()` 方法前先判断该对象是否为空,例如:
```java
ImportViewModel model = list.stream().filter(e -> (e.getType() + e.getStyle()).equalsIgnoreCase(code)).findFirst().orElse(null);
if (model != null) {
int num = model.getNum();
// do something with num
} else {
// handle the case where model is null
}
```
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怎么解决
这个错误是因为您在调用 `getNum()` 方法时,`model` 对象为 `null`,导致空指针异常。可能是因为在 `list` 中没有找到符合条件的 `ImportViewModel` 对象。
建议您在调用 `getNum()` 方法前,先对 `model` 进行非空判断,例如:
```
if (model != null) {
model.setNum(model.getNum() + 1);
}
```
这样可以避免空指针异常的出现。