【问题标题】:null pointer exception when accessing array with null reference [duplicate]使用空引用访问数组时出现空指针异常[重复]
【发布时间】:2020-10-17 06:50:36
【问题描述】:

我的方法应该从整个猫数组中比较一只猫的名字。

这是因为我的数组在创建的对象之间会有空值。

例如 [0(cat 0), 1(cat 1), 2(cat 2), 3(empty null), 4(cat 4), 5(cat 5)]

我认为我得到空指针异常的原因是因为我尝试返回空值并将其与参数(字符串 catName)进行比较。

如何在搜索整个数组的同时避免这种情况?

/**
 * @param catName to search for cat name
 */
public void searchCatByName(String catName) {

    if (cats[0] != null) {

        if(catName != null) {
            int index = 0;
            while (index < cats.length) {

                
                if(cats[index].getName() != catName) {

                    if(index == cats.length - 1) {

                        System.out.println(catName + " was not found in the cattery.");
                        break;
                    }
                    index++;
                } 
                else {

                    System.out.println(cats[index].getName() + " is in cage " + index);
                    break;
                }

            }
        }
    }
}

【问题讨论】:

    标签: java arrays nullpointerexception


    【解决方案1】:

    使用简单的逻辑。首先检查数组元素是否为空,如果不为空,则比较猫名。这是一个关于如何进行空值检查和比较字符串的示例。

    public class Cat {
        public static void main(String[] args) {
    
            String catName = "Cat3";
            String[] cats = new String[] {"Cat1","Cat2","Cat3", "", "Cat4"};
            searchCatByName(catName, cats);
            
        }
        
        public static void searchCatByName(String catName, String[]cats) {
        
            int catCounter = 0;
            for(int i=0;i<cats.length;i++){
                if (cats[i] != null){
                    if(catName.equals(cats[i])){
                        System.out.println(catName + " is in cage no " + (i+1));
                        catCounter++;
                    }
                }
            }
            if(catCounter == 0){
                System.out.println(catName + " was not found in the cattery.");
            }
    
        }
    }
    

    【讨论】:

    • 我现在明白了。我试图访问一个空对象并返回名称。为了避免 NPE,我需要先检查对象是否为空,然后再检查名称。谢谢+1
    • @GrantLee 谢谢.. 添加了 Cat not found 的代码。
    【解决方案2】:

    在将名称与 String equals 方法进行比较之前,您必须检查数组元素是否为空,而不是:

    if(cats[index].getName() != catName) { /* logic */ }
    

    正确的条件是:

    Cat cat = cats[index];
    if(cat != null && !cat.getsName().equals(catName)) { /* logic */ }
    

    【讨论】:

    • 不能 cats[index] 为空,这会导致 ...getName() 上的 NPE?
    • @MikeSamuel 绝对正确,我更正了代码。
    【解决方案3】:

    检查值是否等于 null。如果它不等于 null 然后执行您的条件。我在下面的代码中做了一个例子

            String arr[] = new String[]{"tom","blake",null,"jerry"};
            String fider = "jerry";
            for (int i = 0; i < arr.length; i++)
            {
                if (arr[i] != null)
                    if (arr[i].equals(fider))
                        System.out.println(i); // Prints 3
            }
    

    【讨论】:

    • 我添加了 if (cats[index].getName() != null
    猜你喜欢
    • 2016-09-26
    • 1970-01-01
    • 1970-01-01
    • 2015-03-22
    • 1970-01-01
    • 2011-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多