Java 跟踪NullPointerException的最佳方法是什么?
我现在遇到了一些例外情况,我一直在努力解决它们,因此任何关于如何修复它们的指导或建议都将是非常好的,而不必依赖其他人的帮助。 目前,我有一个关于如何解决它的建议,我们将不胜感激,但从长远来看,关于如何追踪问题原因的一般建议会更好Java 跟踪NullPointerException的最佳方法是什么?,java,nullpointerexception,runtimeexception,Java,Nullpointerexception,Runtimeexception,我现在遇到了一些例外情况,我一直在努力解决它们,因此任何关于如何修复它们的指导或建议都将是非常好的,而不必依赖其他人的帮助。 目前,我有一个关于如何解决它的建议,我们将不胜感激,但从长远来看,关于如何追踪问题原因的一般建议会更好 class Egg extends Bee{ protected void anotherDay() { eat(); if(age>=3) { HashMap<String,
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
}
class-Bee{
另一天()受保护无效{
吃();
如果(年龄>=3)
{
HashMap thisHive=Garden.Garden.getHiveMap();
幼虫=新幼虫(此健康,此年龄);
thisHive.get(“a”).bees.set(thisHive.get(“a”).beeIndex,幼虫);/----第27行
//thisHive.get(“a”).replaceBee(幼虫)第27行最初抛出了相同的异常
}
年龄++;
System.out.println(“鸡蛋”+“年龄”+this.age+“健康”+this.health);
}
}
导入java.util.ArrayList;
类蜂巢{
蜂蜜;
保护果冻;
保护花粉;
public int beeIndex;//用于知道您所在的蜜蜂的索引是什么
公共布尔添加;
ArrayList=新的ArrayList();
受保护的配置单元(int honeyStart、int honeyStart、int pollenStart)
{
bees=新的ArrayList();
这是一个.setHoney(honeyStart);
这个.setRoyalJelly(royalJellyStart);
这是花粉粒(pollenStart);
System.out.println(“蜂巢制造”);
System.out.println(蜂蜜+“蜂蜜”);
System.out.println(royalJelly+“royalJelly”);
System.out.println(花粉+花粉);
holdAdd=false;
}
//代码已删除。。。
公共空间替换蜜蜂(蜜蜂添加蜜蜂){
bees.set(beeIndex,addBee);
}
//代码删除
另一天()受保护无效{
int i=0;
对于(int k=0;k
NullPointerException是代码中试图访问/修改尚未初始化的对象的一种情况
因此,理想情况下,您不应该修复NPE,而是需要确保您没有对空对象进行操作/调用
- 很少情况下你会得到NPE
- 在未初始化的对象上调用方法
- 方法中传递的参数为null,使用
- 已在空对象上同步
- 哈希表的键为空
- 单个语句中的链式方法调用
String s=getValue();
// this is error prone
if(s.equals("SOMEVALUE"){
}
// Rather you can check for
if("SOMEVALLUE".equals(s)){
}
不要从对象返回Null
作为返回类型,比如说如果你想返回列表,你可以尝试Collections.emptyList()
2.提供足够的测试覆盖率。
NPE是RunTimeException,您应该能够从测试类捕获大部分RTE
3。不鼓励传递空参数
但有些地方您必须支持NPE
约书亚·布洛赫(Joshua bloch)在《有效java》一书中说,“可以说,这一切都是错误的
方法调用归结为非法参数或非法状态,
但其他例外情况通常用于某些类型的非法交易
参数和状态。如果调用方在
哪些空值是禁止的,约定规定
将引发NullPointerException,而不是IllegalArgumentException。“
NullPointerException是代码中尝试访问/修改尚未初始化的对象的情况 因此,理想情况下,您不应该修复NPE,而是需要确保您没有对空对象进行操作/调用
- 很少情况下你会得到NPE
- 在未初始化的对象上调用方法
- 方法中传递的参数为null,使用
- 已在空对象上同步
- 哈希表的键为空
- 单个语句中的链式方法调用
String s=getValue();
// this is error prone
if(s.equals("SOMEVALUE"){
}
// Rather you can check for
if("SOMEVALLUE".equals(s)){
}
不要从对象返回Null
作为返回类型,比如说如果你想返回列表,你可以尝试Collections.emptyList()
2.提供足够的测试覆盖率。
NPE是RunTimeException,您应该能够从测试类捕获大部分RTE
3。不鼓励传递空参数
但有些地方您必须支持NPE
约书亚·布洛赫(Joshua bloch)在《有效java》一书中说,“可以说,这一切都是错误的
方法调用归结为非法参数或非法状态,
但其他例外情况通常用于某些类型的非法交易
参数和状态。如果调用方在
哪些空值是禁止的,约定规定
将引发NullPointerException,而不是IllegalArgumentException。“
我的建议是保持线路短,不要打太多嵌套的内联电话。如果你有错误
thisHive.get("a").bees.set(thisHive.get("a").beeIndex, larvae) .
thisHive.get("a").bees.set(thisHive.get("a").beeIndex, larvae);
private int getMyInt() {
Integer myInt = null;
return myInt;
}
private void doSomething() {
int i = getMyInt();
}