gpt4 book ai didi

java - 在扩展方法中获取 NullPointerException - Java

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

我的 UiAutomator 有 4 个类(class)测试项目:

1:

public class A {
protected E e;
public T t = null;
protected String m ,v;
protected long L;

public A(){
t = new T();
}

public A(E e, String m, String v, String tf)
{
this.e = e;
this.m = m;
this.v = v;
t = t.loadXML(tf);
}

2:

public class B extends A {

public B(E e, String m, String v, String tf)
{
this.e = e;
this.m = m;
this.v = v;
t = t.loadXML(tf);
}

3:

public class C {
private B b = null;
private D d = null;
private E e = null;

public C(B b, E e, D d)
{
this.b = b;
this.e = e;
this.d = d;
}

4:

public class D extends B{

public D (E e, String m, String v, String tf)
{
super(e, m, v, tf);
this.e = e;
this.m = m;
this.v = v;
t = t.loadXML(tf);
}

我收到了 NullPointerException当程序尝试使用D类的方法时(C类有使用A、B、D的方法)。
A、B、C中没有问题。
D中的方法调用B中的方法我认为我在延期方面遇到了一些问题?另外,如何在不扩展类的情况下在 D 中使用 B 的方法?谢谢!

最佳答案

一些事情: 尽管 t 是泛型,但您假设 T 有一个方法 loadXML();然而,对于 T 的内容没有任何限制。

此外,这里是您的 NullPointerException 所在的位置:

D 构造函数调用 B 的构造函数,但 B 的构造函数调用默认构造函数对于A,它初始化T。由于 t 未初始化,因此您会收到 NullPointerException

关于java - 在扩展方法中获取 NullPointerException - Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20394296/

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