Boolean.valueOf() produces NullPointerException sometimes

后端 未结 5 1471
名媛妹妹
名媛妹妹 2021-01-30 08:14

I have this code:

package tests;

import java.util.Hashtable;

public class Tests {

    public static void main(String[] args) {

        Hashtable

        
5条回答
  •  无人及你
    2021-01-30 08:33

    Method signature

    The method Boolean.valueOf(...) has two signatures:

    1. public static Boolean valueOf(boolean b)
    2. public static Boolean valueOf(String s)

    Your modifiedItems value is Boolean. You cannot cast Boolean to String so consequently the first signature will be chosen

    Boolean unboxing

    In your statement

    Boolean.valueOf(modifiedItems.get("item1"))
    

    which can be read as

    Boolean.valueOf(modifiedItems.get("item1").booleanValue())   
    

    However, modifiedItems.get("item1") returns null so you'll basically have

    null.booleanValue()
    

    which obviously leads to a NullPointerException

提交回复
热议问题