属性在事件处理中变为 null

发布于 2024-08-16 15:26:26 字数 455 浏览 2 评论 0原文

在简化版本中,我所拥有的是这样的:

public class MyLabel extends JLabel implements MouseListener{
private SomeControl control;

public MyLabel(SomeControl control){
    this.addMouseListener(this);
    this.control = control;
}

@Override
public void mouseClicked(MouseEvent arg0) {
    Object x = this.control.getSomeProperty();
}

即使我在构造 MyLabel 实例时调试和验证控件及其 someProperty 不为空,但当事件被触发并且处理程序介入时,它会显示 someProperty 就好像它是null,这里可能有什么问题?

in a simplified version, what i have is this:

public class MyLabel extends JLabel implements MouseListener{
private SomeControl control;

public MyLabel(SomeControl control){
    this.addMouseListener(this);
    this.control = control;
}

@Override
public void mouseClicked(MouseEvent arg0) {
    Object x = this.control.getSomeProperty();
}

Even though i debug and verify when constructing the MyLabel instance that the control and its someProperty is not null, when the event is fired and handler steps in, it shows the someProperty as if it was null, what could be the problem here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦明 2024-08-23 15:26:26

MyLabel 构造函数中,control 的值为 null(通过 default) 当 this 添加为 MouseListener 时。如果此时调用侦听器,它将看到空值。随后,如调试器报告的那样,control 值将更新为非空参数值。我怀疑异常可能是转义的 this,正如所讨论的 这里

In the MyLabel constructor, the value of control is null (by default) when this is added as the MouseListener. If the listener were invoked at this point, it would see the null value. Subsequently, the control value is updated with the non-null parameter value, as the debugger reports. I suspect the anomaly may be an escaped this, as discussed here.

飘然心甜 2024-08-23 15:26:26

如果你能做到这一点,请将代码更改为:(

编辑:早上太早了..忘记了final这个词:-)

private final SomeControl control;

你必须在构造函数中执行“control = new ......”,但是如果您只想将其分配在一个地方,那么这就是让编译器帮助您的方法。

另外,您确定 NullPointerException 不在方法调用中吗?

它显示了 someProperty,就好像它是
null,这里可能有什么问题?

基于此,我假设我误解了您的意思...您是说 someProperty() 方法调用返回 null 吗?如果是这种情况,请执行以下操作:

  • 在 SomeControl 类中将 someProoerty 变量标记为 Final(如果可能)。这意味着您不能使用 setProperty(....) 方法。
  • 如果您无法使变量成为final,请将以下代码添加到setProperty(...) 方法中:

...

public void setPropert(... value)
{
    if(value == null)
    {
        throw new IllegalArgumentException("value cannot be null");
    }
}

然后您将看到代码的哪一部分将属性设置为null。

if you can do it change the code to:

(EDIT: too early in the morning.. .forgot the word final :-)

private final SomeControl control;

You will have to do the "control = new ......" in the constructor, but if you only want it to be assigned in one place then that is the way to get the compiler to help you.

Also, are you sure that the NullPointerException is not in the method call?

it shows the someProperty as if it was
null, what could be the problem here?

Based on that I am assuming I misunderstood what you were saying... are you saying that the someProperty() method call returns null? If that is the case, then do the following:

  • in the SomeControl class mark the someProoerty variable as final (if possible). That means that you cannot have a setProperty(....) method.
  • if you cannot make the variable final add the following code to the setProperty(...) method:

...

public void setPropert(... value)
{
    if(value == null)
    {
        throw new IllegalArgumentException("value cannot be null");
    }
}

then you will see what part of the code is setting the property to null.

零度° 2024-08-23 15:26:26

我很确定该属性在代码中的其他位置设置为 null。
您可以尝试在 someProperty 字段上设置一个观察点并调试以查看它何时变为 null。

或者您可以向我们提供一些可编译的代码,以便我们自己尝试。

I'm pretty sure the property is set to null somewhere else in your code.
You can try setting a watchpoint on the someProperty field and debug to see when it becomes null.

Or you could provide us with some compilable code, so we could try it ourselves.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文