gpt4 book ai didi

java - 将 null 值作为参数传递给方法时如何修复 NullPointerException

转载 作者:太空宇宙 更新时间:2023-11-04 09:24:52 24 4
gpt4 key购买 nike

我的目标是使用自动生成测试来查找这些错误的工具来修复多个 Java 类中的错误。其中一个错误是 NullPointerException,它是由于两个属性被初始化为 null 并传递到对象中而引起的。同样,这是由该工具自动生成的,因此我的目标是修复类文件。

这是该工具生成的代码:

public void test1() throws Throwable {
if (debug)
System.out.format("%n%s%n", "ErrorTest0.test1");
Point point0 = null;
Point point1 = null;
Line line2 = new Line(point0, point1); // Line 16
}

这是导致问题的构造函数:

        public Line(Point p1, Point p2)
{
this.point1 = new Point(p1.x,p1.y); // Line 17
this.point2 = new Point(p2.x,p2.y);
}

以及 Point 的构造函数:

        public Point(double x, double y)
{
this.x = x;
this.y = y;
}

这是我从该工具收到的错误消息:

java.lang.NullPointerException
at Line.<init>(Line.java:17)
at ErrorTest0.test1(ErrorTest0.java:16)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:27)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:27)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
at org.junit.runner.JUnitCore.runMain(JUnitCore.java:77)
at org.junit.runner.JUnitCore.main(JUnitCore.java:36)

我猜我应该在构造函数中包含一些不允许 this 为空的内容?

最佳答案

您可以使用三种方法来更新线路方法。希望您的测试工具能够识别这些内容。

要求构造函数中的参数不为空。这是更现代的方法。

public Line(@NotNull Point p1, @NotNull Point p2) {

this.point1 = Objects.requireNonNull(p1);
this.point2 = Objects.requireNonNull(p2);
}

当参数设置为null时抛出异常。您的测试用例应该允许您预期该用例会出现异常。

public Line(Point p1, Point p2) throws IllegalArgumentException {

if (p1 == null || p2 == null) {
throw new IllegalArgumentException("point cannot be null");
}

this.point1 = p1;
this.point2 = p2;
}

传入 null 时有一个合理的默认行为。什么是合理的完全取决于您的用例。

public Line(Point p1, Point p2) {

if (p1 != null) {
this.point1 = p1;
} else {
this.point1 = new Point(0.0, 0.0);
}

if (p2 != null) {
this.point2 = p2;
} else {
this.point2 = new Point(0.0, 0.0);
}
}

关于java - 将 null 值作为参数传递给方法时如何修复 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57834959/

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