【问题标题】:java.lang.NullPointerException upon access to an Imageview[] array - Javafx访问 Imageview[] 数组时出现 java.lang.NullPointerException - Javafx
【发布时间】:2014-08-12 05:25:30
【问题描述】:

对不起,如果这对你们大家来说似乎微不足道,但我仍在学习,我已经阅读了文章并尝试了几种技术......我可以单独创建每个卡片图像,但我需要学习如何做到这一点.我在这段代码上抛出了 java.lang.NullPointerException:

setCardBack.setImage(new Image("image/cardDown.png"));

现在的问题是我确实通过输入来修复它:

setCardBack = new ImageView(new Image("image/cardDown.png"));

问题是,当它尝试在 loadCard 方法中访问该图像时,我抛出了另一个 NullPointerException,这是引发异常的部分:

cardBack[0].setVisible(true);

这是完整的课程(顺便说一句,下面的代码没有更改以反映我声称我所做的上述更改,以便大家可以看到原始未编辑的代码):

public class PlayerTableCardPane extends Pane {
// Data Fields
ImageView[] cardBack;
private int cardXOffset;
private int cardHeight;
private int cardCount;

// StackPane for cards
StackPane cardStack;

/** Constructor */
public PlayerTableCardPane() {
    this.cardStack = new StackPane();
    this.cardBack = new ImageView[5];
    this.cardHeight = 50;
    this.cardXOffset = 5;
    this.cardCount = 0;

    ImageView tempCard = null;
    for (ImageView setCardBack: cardBack) {
        setCardBack.setImage(new Image("image/cardDown.png"));
        setCardBack.setVisible(false);
        setCardBack.setFitHeight(cardHeight);
        setCardBack.setPreserveRatio(true);

        if (tempCard != null) {
            setCardBack.setX(tempCard.getX() + cardXOffset);
        }

        tempCard = setCardBack;
    }

    getChildren().add(this.cardStack);
}

/** Constructor for setting custom height */
public PlayerTableCardPane(int cardHeight, int cardXOffset) {
    this.cardStack = new StackPane();
    this.cardBack = new ImageView[5];
    this.cardXOffset = cardXOffset;
    this.cardHeight = cardHeight;
    this.cardCount = 0;

    ImageView tempCard = null;
    for (ImageView setCardBack: cardBack) {
        setCardBack = new ImageView(new Image("image/cardDown.png"));
        setCardBack.setVisible(false);
        setCardBack.setFitHeight(cardHeight);
        setCardBack.setPreserveRatio(true);

        if (tempCard != null) {
            setCardBack.setX(tempCard.getX() + cardXOffset);
        }

        tempCard = setCardBack;
    }

    getChildren().add(this.cardStack);
}

/** Get cardXOffset */
public int getCardXOffset() {
    return this.cardXOffset;
}

/** Set cardXOffset */
public void setCardXOffset(int cardXOffset) {
    this.cardXOffset = cardXOffset;
}

/** Get cardCount */
public int getCardCount() {
    return this.cardCount;
}

/** Set cardCount */
public void setCardCount(int cardCount) {
    this.cardCount = cardCount;
}

/** Load cards into StackPane */
public void loadCard() {
    if (cardCount == 0) {
        cardBack[0].setVisible(true);
    } else if (cardCount == 1) {
        cardBack[1].setVisible(true);
    } else if (cardCount == 2) {
        cardBack[2].setVisible(true);
    } else if (cardCount == 3) {
        cardBack[3].setVisible(true);
    } else if (cardCount == 4) {
        cardBack[4].setVisible(true);
    }
    this.cardCount++;
}

/** Hide last card() */
public void hideLastCard() {
    if (cardCount == 4) {
        cardBack[4].setVisible(false);
    } else if (cardCount == 3) {
        cardBack[3].setVisible(false);
    } else if (cardCount == 2) {
        cardBack[2].setVisible(false);
    } else if (cardCount == 1) {
        cardBack[1].setVisible(false);
    } else if (cardCount == 0) {
        cardBack[0].setVisible(false);
    }
    this.cardCount--;
}

/** Replace card with FaceUp Image */
public void setCardImg(ImageView card, int cardPos) {
    cardBack[cardPos].setImage(card.getImage());
}

}

这是最后一个例外:

java.lang.NullPointerException
at Casino_Poker.PlayerTableCardPane.loadCard(PlayerTableCardPane.java:94)
at Casino_Poker.Main.dealCards(Main.java:444)
at Casino_Poker.Main.lambda$signIn$1(Main.java:249)
at Casino_Poker.Main$$Lambda$4/86482238.handle(Unknown Source)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
at javafx.event.Event.fireEvent(Event.java:204)
at javafx.scene.Node.fireEvent(Node.java:8175)
at javafx.scene.control.Button.fire(Button.java:185)
at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:96)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89)
at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.event.Event.fireEvent(Event.java:204)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3746)
at javafx.scene.Scene$MouseHandler.access$1800(Scene.java:3471)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1695)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2486)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:314)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:243)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:345)
at com.sun.glass.ui.View.handleMouseEvent(View.java:526)
at com.sun.glass.ui.View.notifyMouse(View.java:898)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.access$300(WinApplication.java:39)
at com.sun.glass.ui.win.WinApplication$4$1.run(WinApplication.java:112)
at java.lang.Thread.run(Thread.java:745)

进程以退出代码 0 结束

【问题讨论】:

  • 到目前为止,我在这个程序中总共有1633行代码,这是我开始编程以来的第一个真正的项目。我开始好转了,但我显然还需要一些工作

标签: java arrays image nullpointerexception javafx-8


【解决方案1】:

你得到的空指针异常是因为虽然你已经声明了

 this.cardBack = new ImageView[5];

,数组为空,索引中没有任何实际的ImageView 对象。您需要实例化数组中的 ImageView 对象才能真正对它们执行任何操作。您通过实例化一个 ImageView 来“修复”您的第一个错误,您在该 ImageView 上执行了循环主体中的其余方法。

您仍然收到空指针异常的原因是您增强了 for 循环,这使得 setCardBack 成为局部变量。允许修改现有对象,但不能在数组中创建新对象。点击here了解更多信息。

您可以使用常规 for 循环来实例化您的 ImageViews。

cardBack = new ImageView[5];
for (int i=0;i<5;i++)
    cardBack[i] = new ImageView(new Image("image/cardDown.png"));

【讨论】:

  • 谢谢我真的很感激,我不太了解 ForEach 循环。既然你解释了,我就明白了很多。
【解决方案2】:

以下代码应该可以修复您的错误,并且 cmets 应该可以解释错误的原因。

默认构造函数:

/** Constructor */
public PlayerTableCardPane() {
    // Calling an overloaded constructor is a lot less
    // work than writing out two constructors with minor
    // differences.
    this(50, 5);
}

重载的构造函数:

/** Constructor for setting custom height and X offset */
public PlayerTableCardPane(int height, int xOffset) {
    this.cardStack = new StackPane();
    this.cardBack = new ImageView[5];
    this.cardHeight = height;
    this.cardXOffset = xOffset;
    this.cardCount = 0;
    
    // There is no need to define the ImageView object
    // outside of the for loop unless you need to use
    // the ImageView object after the for loop.
    // ImageView tempCard = null;
    
    // I don't like using for-each unless I have to.
    for (int index = 0; index < 5; index++) {
        
        Image newImage = new Image("image/cardDown.png");
        
        // The NullPointerException came from trying to
        // access the property of an uninstantiated
        // ImageViewer in the cardBack array.
        ImageView setCardBack = new ImageView();
        setCardBack.setImage(newImage);
        
        setCardBack.setVisible(false);
        setCardBack.setFitHeight(cardHeight);
        setCardBack.setPreserveRatio(true);
        
        // this next line does what your conditional does without
        // the need for any conditional, also it avoids testing for
        // null, and removes the need for a tempCard variable.
        setCardBack.setX(index * cardXOffset);
        
        /*
        if (tempCard != null) {
            setCardBack.setX(tempCard.getX() + cardXOffset);
        }
        */
        
        // There is no longer a need for the next line.
        // tempCard = setCardBack;
        
        // Add the dynamically created and instantiated ImageView
        // object to this.cardBack.
        this.cardBack[index] = setCardBack;
    }
    
    // You have to add the array of ImageViews to the StackPanel,
    // before adding the StackPanel to the Panel that this class
    // etends.
    this.cardStack.getChildren().add(this.cardBack);
    this.getChildren().add(this.cardStack);
    
}

如果您需要进一步的帮助,或者如果此代码导致更多错误,请告诉我,我很乐意提供帮助。

【讨论】:

  • 是的,这绝对可以让事情变得清晰,我喜欢你在消除 tempCard 变量的同时清理代码的方式。
【解决方案3】:

我忘记了重载功能,这会让我的代码看起来更漂亮

【讨论】:

  • 并不是想把这个作为答案发布,还是习惯了这个安卓应用:/
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-30
  • 2020-03-09
  • 2016-06-19
  • 1970-01-01
  • 1970-01-01
  • 2014-03-12
  • 2012-11-11
相关资源
最近更新 更多