如果这对你们来说似乎微不足道,我很抱歉,但我仍在学习,我浏览了文章并尝试了几种技术...我可以单独创建每个卡片图像,但我需要学习如何做到这一点。我在这段代码上抛出 java.lang.NullPointerException:
setCardBack.setImage(new Image("image/cardDown.png"));
现在的问题是我确实通过输入修复了它:
setCardBack = new ImageView(new Image("image/cardDown.png"));
问题是,这样做时,当它尝试在 loadCard 方法中访问该图像时,我抛出了另一个 NullPointerException,这里是抛出异常的部分:
cardBack[0].setVisible(true);
这是完整的类(class)(顺便说一句,下面的代码没有更改以反射(reflect)我声称我所做的上述更改,以便你们都可以看到原始的未经编辑的编码):
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());
}
}
这是最后一个异常(exception):
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
最佳答案
你得到的空指针异常是因为尽管你已经声明了
this.cardBack = new ImageView[5];
,数组为空,索引中没有任何实际的 ImageView
对象。您需要实例化数组中的 ImageView
对象才能真正能够对它们执行任何操作。您通过实例化一个 ImageView
来“修复”了第一个错误,并在该 ImageView 上执行了循环主体中的其余方法。
您仍然收到空指针异常的原因是因为您增强了 for 循环,这使得 setCardBack
成为局部变量。允许修改现有对象,但不能在数组中创建新对象。点击here了解更多信息。
您可以使用常规的 for 循环来实例化您的 ImageView
。
cardBack = new ImageView[5];
for (int i=0;i<5;i++)
cardBack[i] = new ImageView(new Image("image/cardDown.png"));
关于访问 Imageview[] 数组时出现 java.lang.NullPointerException - Javafx,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25256725/