Home >Java >javaTutorial >How to handle null pointer exception in Java

How to handle null pointer exception in Java

WBOY
WBOYforward
2023-05-10 17:43:061759browse

NullPointerException is the most common exception in Java code. I classify its most likely scenarios into the following five types:

  • The parameter value is a packaging type such as Integer. When using it, because A null pointer exception occurred during automatic unboxing;

  • A null pointer exception occurred during string comparison;

  • Containers such as ConcurrentHashMap do not support Key and Value is null, and a null pointer exception will occur if a null Key or Value is forcibly put;

  • A object contains B. After obtaining B through the fields of the A object, the field is not judged to be empty. A null pointer exception occurs when calling B's method in cascade; the List returned by the method or remote service is not empty but null, and a null pointer exception occurs when directly calling the List method without performing a null check.

private List<String> wrongMethod(FooService fooService, Integer i, String s, String t) {
    log.info("result {} {} {} {}", i + 1, s.equals("OK"), s.equals(t),
            new ConcurrentHashMap<String, String>().put(null, null));
    if (fooService.getBarService().bar().equals("OK"))
        log.info("OK");
    return null;
}

@GetMapping("wrong")
public int wrong(@RequestParam(value = "test", defaultValue = "1111") String test) {
    return wrongMethod(test.charAt(0) == &#39;1&#39; ? null : new FooService(),
            test.charAt(1) == &#39;1&#39; ? null : 1,
            test.charAt(2) == &#39;1&#39; ? null : "OK",
            test.charAt(3) == &#39;1&#39; ? null : "OK").size();
}

class FooService {
    @Getter
    private BarService barService;

}

class BarService {
    String bar() {
        return "OK";
    }
}

The repair ideas are as follows:

  • For the null judgment of Integer, you can use Optional.ofNullable to construct an Optional, and then use orElse(0 ) Replace null with the default value and perform 1 operation. For comparison between String and literal, you can put the literal in front, such as "OK".equals(s), so that even if s is null, a null pointer exception will not occur; and for two string variables that may be null For equals comparison, you can use Objects.equals, which will perform null processing.

  • For ConcurrentHashMap, since neither its Key nor Value supports null, the fix is ​​not to store null in it. HashMap's Key and Value can store null, while ConcurrentHashMap seems to be a thread-safe version of HashMap, but it does not support null-valued Key and Value. This is a place where misunderstandings can easily arise.

  • For cascade calls like fooService.getBarService().bar().equals("OK"), there are many places where null judgment is required, including fooService and getBarService() methods The return value of , and the string returned by the bar method. If you use if-else to check for null, you may need several lines of code, but if you use Optional, one line of code is enough.

  • For the List returned by rightMethod, since it cannot be confirmed whether it is null, before calling the size method to obtain the list size, you can also use Optional.ofNullable to wrap the return value, and then pass. orElse(Collections.emptyList()) achieves obtaining an empty List when the List is null, and finally calls the size method.

private List<String> rightMethod(FooService fooService, Integer i, String s, String t) {
    log.info("result {} {} {} {}", Optional.ofNullable(i).orElse(0) + 1, "OK".equals(s), Objects.equals(s, t), new HashMap<String, String>().put(null, null));
    Optional.ofNullable(fooService)
            .map(FooService::getBarService)
            .filter(barService -> "OK".equals(barService.bar()))
            .ifPresent(result -> log.info("OK"));
    return new ArrayList<>();
}

@GetMapping("right")
public int right(@RequestParam(value = "test", defaultValue = "1111") String test) {
    return Optional.ofNullable(rightMethod(test.charAt(0) == &#39;1&#39; ? null : new FooService(),
            test.charAt(1) == &#39;1&#39; ? null : 1,
            test.charAt(2) == &#39;1&#39; ? null : "OK",
            test.charAt(3) == &#39;1&#39; ? null : "OK"))
            .orElse(Collections.emptyList()).size();
}
  • We update the name, age and nickname respectively according to business needs: for the name, we believe that the client wants to reset the name to null when passing null. , allowing such an operation, just use the orElse method of Optional to convert null to an empty string with one click.

  • For age, we believe that if the client wants to update the age, it must pass a valid age. There is no reset operation for age. You can use the Optional orElseThrow method when the value is empty. throws an IllegalArgumentException.

  • For the nickname, because the name in the database cannot be null, you can safely set the nickname to guest plus the name taken out from the database.

@PostMapping("right")
public UserEntity right(@RequestBody UserDto user) {
    if (user == null || user.getId() == null)
        throw new IllegalArgumentException("用户Id不能为空");

    UserEntity userEntity = userEntityRepository.findById(user.getId())
            .orElseThrow(() -> new IllegalArgumentException("用户不存在"));

    if (user.getName() != null) {
        userEntity.setName(user.getName().orElse(""));
    }
    userEntity.setNickname("guest" + userEntity.getName());
    if (user.getAge() != null) {
        userEntity.setAge(user.getAge().orElseThrow(() -> new IllegalArgumentException("年龄不能为空")));
    }
    return userEntityRepository.save(userEntity);
}

The above is the detailed content of How to handle null pointer exception in Java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete