Check if null Boolean is true results in exception

后端 未结 7 1388
难免孤独
难免孤独 2020-11-28 05:20

I have the following code:

Boolean bool = null;

try 
{
    if (bool)
    {
        //DoSomething
    }                   
} 
catch (Exception e) 
{
    Syst         


        
7条回答
  •  没有蜡笔的小新
    2020-11-28 05:53

    as your variable bool is pointing to a null, you will always get a NullPointerException, you need to initialize the variable first somewhere with a not null value, and then modify it.

提交回复
热议问题