gpt4 book ai didi

java - 跟踪 NullPointerExceptions 的最佳方法是什么?

转载 作者:行者123 更新时间:2023-11-29 09:54:11 24 4
gpt4 key购买 nike

我现在遇到了一些这样的异常,我一直在努力解决它们,所以任何关于如何修复它们的指南或建议都会很好,而不是必须依赖其他人来帮助解决它们。目前我有一个关于如何修复它的建议,我将不胜感激,但从长远来看,关于如何追踪问题原因的一般建议会更好。

class Egg extends Bee{
protected void anotherDay() {

eat();
if(age>=3)
{
HashMap<String, Hive> thisHive = Garden.GARDEN.getHiveMap();
Larvae larvae = new Larvae(this.health, this.age);
thisHive.get("a").bees.set(thisHive.get("a").beeIndex, larvae); //-------LINE 27
//thisHive.get("a").replaceBee(larvae) Line 27 was origionally this throwing the same exception

}
age++;
System.out.println("Egg" + " age " + this.age + " health " + this.health);

}
}

import java.util.ArrayList;

class Hive {
protected int honey;
protected int royalJelly;
protected int pollen;
public int beeIndex; // used to know what the index of bee you are in is
public boolean holdAdd;
ArrayList<Bee> bees = new ArrayList<Bee>();
protected Hive(int honeyStart, int royalJellyStart, int pollenStart)
{
bees = new ArrayList<Bee>();
this.setHoney(honeyStart);
this.setRoyalJelly(royalJellyStart);
this.setPollen(pollenStart);
System.out.println("hive made");
System.out.println(honey + " honey");
System.out.println(royalJelly + " royalJelly");
System.out.println(pollen + " pollen");
holdAdd = false;
}
//code removed ...

public void replaceBee(Bee addBee) {
bees.set(beeIndex, addBee);
}

// code removed

protected void anotherDay() {
int i = 0;
for(int k = 0; k < bees.size(); k++)
{
i++;
Bee bee = bees.get(k);
bee.anotherDay(); // ----------------LINE 144
beeIndex = i;
}
// code removed
}
}


public class Garden {

static HashMap<String, Hive> HiveMap = new HashMap<String, Hive>();
public static final Garden GARDEN = new Garden();
public static void main(String[] args) {
GARDEN.anotherDay(); //------------------LINE 21
}
}

//CODE REMOVED

public HashMap<String, Hive> getHiveMap()
{
return Garden.HiveMap;
}
// CODE REMOVED


protected void anotherDay() {
//CODE REMOVED

//should find all Hives and call anotherday() on them each

for(Hive currentHive : HiveMap.values()){
currentHive.anotherDay(); //------------LINE 56
}

}
//CODE REMOVED
}

最佳答案

NullPointerException 是代码中您尝试访问/修改尚未初始化的对象的情况。

因此理想情况下,您不应该修复 NPE,而是需要确保您没有在 Null 对象上操作/调用。

  • 很少有你会得到 NPE 的场景
  • 在未初始化的对象上调用方法
  • 方法中传入的参数为空使用
  • 在一个空对象上同步
  • 哈希表的键为空
  • 在单个语句中调用链式方法

我们如何安全处理

<强>1。更好的编码实践

例1:改进编码风格

String s=getValue();
// this is error prone
if(s.equals("SOMEVALUE"){
}
// Rather you can check for
if("SOMEVALLUE".equals(s)){

}

eg2:不要将 Null 作为对象的返回类型返回,比如说如果你想返回 List,而不是返回 null,你可以尝试 Collections.emptyList()

  public List getEmpList(){
// some operation
if(exp) {
return List
}
else{
return Collections.emptyList(); //dont return null
}

}

2.提供足够的测试覆盖率。

NPE 是 RunTimeException,您应该能够从测试类中捕获大部分 RTE。

<强>3。不鼓励传递空参数

但是有些地方你必须支持NPE

Joshua bloch in effective java says that “Arguably, all erroneous method invocations boil down to an illegal argument or illegal state, but other exceptions are standardly used for certain kinds of illegal arguments and states. If a caller passes null in some parameter for which null values are prohibited, convention dictates that NullPointerException be thrown rather than IllegalArgumentException.”

关于java - 跟踪 NullPointerExceptions 的最佳方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20499273/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com