if statement checks for null but still throws a NullPointerException

后端 未结 6 2092
無奈伤痛
無奈伤痛 2020-12-05 09:35

In this code.

public class Test {
     public static void testFun(String str) {
         if (str == null | str.length() == 0) {
             System.out.print         


        
6条回答
  •  心在旅途
    2020-12-05 10:09

    The problem here is that in your code the program is calling 'null.length()' which is not defined if the argument passed to the function is null. That's why the exception is thrown.

提交回复
热议问题