1

I'm sorry if this seems trivial to you all, but I am still learning and I have looked through articles and tried several techniques...I could create each card image separately, but I need to learn how to do this. I am throwing a java.lang.NullPointerException on this bit of code:

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

Now the problem is that I did fix it by entering:

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

the problem is that when doing so I threw another NullPointerException when it tried to access that image in the loadCard method here is the part that threw the exception:

cardBack[0].setVisible(true);

Here is the full class (Btw the below code wasn't changed to reflect the above change I claimed I made so that you all can see the original unedited coded):

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());
}

}

and here is the last 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)

Process finished with exit code 0

John Conner
  • 233
  • 1
  • 4
  • 18
  • So far I have a total of 1633 lines of code in this program, it's my first real project since I started programming. I'm starting to get better but I obviously still need a bit of work – John Conner Aug 12 '14 at 05:37

3 Answers3

1

The Null Pointer Exception you get is because although you've declared

 this.cardBack = new ImageView[5];

, the array is empty, without any actual ImageView objects in the indexes. You need to instantiate ImageView objects in the array to actually be able to do anything with them. You "fixed" your first error by instantiating an ImageView on which you executed the rest of the methods in the loop's body.

The reason why you're still getting a Null Pointer Exception is because of your enhanced for loop, which makes setCardBack a local variable. Modifications to existing objects are allowed, but you can't create new objects in your array. Click here for more info.

You can use a regular for loop instead to instantiate your ImageViews.

cardBack = new ImageView[5];
for (int i=0;i<5;i++)
    cardBack[i] = new ImageView(new Image("image/cardDown.png"));
Community
  • 1
  • 1
Forager
  • 307
  • 2
  • 10
  • Thanks I really appreciate that, I didn't quite understand the ForEach loop. Now that you explained it, I understand it a lot better. – John Conner Aug 12 '14 at 06:37
1

The following code should fix your error and the comments should hopefully explain the reasons for the error.

Default Constructor :

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

Overloaded Constructor :

/** 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);
    
}

Let me know if you need further help or if this code leads to more errors, I'm happy to help.

Community
  • 1
  • 1
Jonny Henly
  • 4,023
  • 4
  • 26
  • 43
  • Yeah, this definitely puts things into perspective and i like how you cleaned up the code while eliminating the tempCard variable. – John Conner Aug 12 '14 at 14:28
0

And I forgot about the overload feature, that's going to make my code look a lot nicer

John Conner
  • 233
  • 1
  • 4
  • 18