无法访问由同级类实例化的对象引用 [英] Cannot access an Object Reference instantiated by a sibling class

查看:100
本文介绍了无法访问由同级类实例化的对象引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个抽象类,它声明了许多对象引用,特别是JLabel。我有两个类都扩展了该类。其中一个子实例实例化JLabel并调用其几个方法。该类同级尝试在JLabel上调用setText方法,并且我得到一个NullPointerException(实例化该同级的同级先于其他同级被调用。)



子对象在内存中引用同一对象,因此将修改同一对象,但显然我错了。我如何解决这个问题?



以下是我的意思的示例:

  public class BlahDriver 
{
public static void main(String [] args)
{
BlahChildOne blah = new BlahChildOne();
BlahChildTwo blah2 = new BlahChildTwo();
}
}
公共抽象类Blah
{
受保护的JLabel标签;
}
公共类BlahChildOne扩展了Blah
{
public BlahChildOne()
{
label = new JLabel();
}
}
public class BlahChildTwo扩展了Blah
{
public BlahChildTwo()
{
label.setText( Fred);
}
}

在尝试setText时,我在BlahChildTwo上收到NullPointerException。

解决方案

因此,blah定义了一个名为 label 的实例字段,但没有

 公共抽象类Blah 
{
受保护的JLabel标签;
}

BlahChildOne 初始化标签来自 Blah

  public class BlahChildOne扩展了Blah 
{
public BlahChildOne()
{
label = new JLabel();
}
}

BlahChildTwo 不会初始化 Blah 标签,因此它仍然 null ...

 公共类BlahChildTwo扩展了Blah 
{
public BlahChildTwo()
{
label.setText( Fred);
}
}

都不是 BlahChildOne BlahChildTwo 在它们之间共享信息(它们通过从 Blah 继承而具有共同性,但实际信息是



将其视为同卵双胞胎,他们可能会共享共同点,但仅仅是因为一个人生病,并不意味着另一个人会是自己的,自包含实例。



如果要在类之间共享信息,则应考虑提供要共享的类的引用,例如...

 公共类BlahChildTwo扩展了Blah 
{
public BlahChildTwo(Blah blah)
{
label = blah.label;
label.setText( Fred);
}
}

但我真的很想看看一个用例

为什么静态被认为不好



让我们假设...

 公共抽象类Blah 
{
受保护的静态JLabel标签;
}

现在在您的代码中,您可以执行类似...

  BlahChildOne bco = new BlahChildOne(); 
add(bco.label); //它是屏幕上的标签...
BlahChildTwo bct = new BlahChildTwo();

好的,到目前为止,很好。现在想象一下,在代码的其他地方,您会做...

  BlahChildOne bco = new BlahChildOne(); 
BlahChildTwo bct = new BlahChildTwo();

...但是为什么屏幕没有更新?因为 Blah#label 不再指向以前添加到屏幕上的 JLabel ,所以您已经失去了它



调试器很难找到,即使您知道要查找的内容,因为没有责任,任何扩展<$ c $的类c> Blah 可以创建 JLabel 的新实例,并将其分配给 label 。 / p>

I have an abstract class that declares a number of object references, specifically a JLabel. I have two classes that both extend that class. One of the children instantiates the JLabel and calls a couple of its methods. That classes sibling attempts to call the setText method on the JLabel and I get a NullPointerException(The sibling that instantiates it is called before the other.)

I had thought that all children refer to the same object in memory and would thus modify the same object but apparently I'm wrong. How can I get around this?

Here is an example of what I mean:

public class BlahDriver 
{
    public static void main(String[] args) 
    {
        BlahChildOne blah = new BlahChildOne();
        BlahChildTwo blah2 = new BlahChildTwo();
    }
}
public abstract class Blah 
{
    protected JLabel label;
}
public class BlahChildOne extends Blah
{
    public BlahChildOne()
    {
        label = new JLabel();
    }
}
public class BlahChildTwo extends Blah
{
    public BlahChildTwo()
    {
        label.setText("Fred");
    }
}

I get a NullPointerException at BlahChildTwo when attempting to setText.

解决方案

So, blah defines an instance field called label but does not initialise it

public abstract class Blah 
{
    protected JLabel label;
}

BlahChildOne initialises the label from Blah

public class BlahChildOne extends Blah
{
    public BlahChildOne()
    {
        label = new JLabel();
    }
}

BlahChildTwo does not initialises the label from Blah, so it is still null...

public class BlahChildTwo extends Blah
{
    public BlahChildTwo()
    {
        label.setText("Fred");
    }
}

Neither BlahChildOne or BlahChildTwo share information between them (they have commonality through inheritance from Blah, but the actually information is each owns).

Think of it like identical twins, they might share commonality, but just because one get's sick, doesn't mean the other will, they are their own self contained instance.

IF you want to share information between classes, you should consider providing a reference to the class with the information you want to share, for example...

public class BlahChildTwo extends Blah
{
    public BlahChildTwo(Blah blah)
    {
        label = blah.label;
        label.setText("Fred");
    }
}

But I'd really like to see a use case for something so drastic...

Why static is consider bad

Lets assume...

public abstract class Blah 
{
    protected static JLabel label;
}

Now in your code, you do something like...

BlahChildOne bco = new BlahChildOne();
add(bco.label); // It the label to the screen...
BlahChildTwo bct = new BlahChildTwo();

Okay, so far, so good. Now imagine, somewhere else in your code, you do...

BlahChildOne bco = new BlahChildOne();
BlahChildTwo bct = new BlahChildTwo();

...But why hasn't the screen updated? Because Blah#label is no longer pointing to the JLabel which you previously added to the screen, you've lost that reference.

This is pain in the debugger to find, even when you know what you are looking for, because there is no accountability, any class that extends Blah can create a new instance of JLabel and assign it to label.

这篇关于无法访问由同级类实例化的对象引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆