项目中常见NPE空指针异常

Posted Bug解决者

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了项目中常见NPE空指针异常相关的知识,希望对你有一定的参考价值。

项目中常见NPE空指针异常

BigDecimalString 
BigDecimal bigDecimal = null;
System.out.println("bigDecimal.toString() = " + bigDecimal.toString());
StringBigDecimal
String string = null;
BigDecimal bigDecimal = new BigDecimal(string);
System.out.println("bigDecimal = " + bigDecimal);
List<String> list = null;
String s = list.get(0);
System.out.println("s = " + s); 
UserInfo userInfo = null;
String userName = userInfo.getUserName();
System.out.println("userName = " + userName);
String aa = null;
boolean bb = aa.equals("bb");
System.out.println("bb = " + bb);
List<String> strings = null;
List<String> strings1 = Collections.emptyList();
System.out.println("strings = " + strings); // null
System.out.println("strings1 = " + strings1); // []

boolean empty = CollUtil.isEmpty(strings);
boolean empty1 = CollUtil.isEmpty(strings1);
System.out.println("empty = " + empty);  // true
System.out.println("empty1 = " + empty1); // true
String str = null;
Map map = new ConcurrentHashMap<>();
map.put(str,"abc");
map.put("aaa",null);
System.out.println("map = " + map);
ZoneInfo zoneInfo = null;
zoneInfo.setName("aaa");
System.out.println("zoneInfo = " + zoneInfo);

以上是关于项目中常见NPE空指针异常的主要内容,如果未能解决你的问题,请参考以下文章

项目中常见NPE空指针异常

Java认识NPE问题 - 空指针异常

JDK8新特性:使用Optional:解决NPE问题的更干净的写法

Java 8 Optional:优雅地避免 NPE

空指针异常与Optional类

NPE nullpoNullPointerException问题