0

So I'm making a card game for my project at school. It uses a GUI where the cards are shown to the user.

I'm using Java FXML in intellij

I'm simply trying to get everything working before I make it look pretty at the moment but the cards are buttons with an image attatched to them.

I looked up how to get an image onto a button and the main guide for JavaFX just isnt working for me. I've also tried searching and I found one - Loading image resource - but I've followed the instructions exactly and I'm still getting the error:

Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1769)
... 48 more
Caused by: java.lang.NullPointerException: Input stream must not be null
at javafx.scene.image.Image.validateInputStream(Image.java:1128)
at javafx.scene.image.Image.<init>(Image.java:706)
at sample.Controller.cardClickHandler(Controller.java:26)
... 58 more

Here is a picture of my IDE with the directories: https://i.stack.imgur.com/Bl5YY.jpg

All I want is that when the button is clicked the image changes to the Ace of Clubs ( I will make it that when the deal button is clicked they all change to random ones but that comes after I got this working )

Heres the code atm:

public void cardClickHandler(ActionEvent actionEvent)
{
    Button button = (Button)actionEvent.getSource();
    Integer row = GridPane.getRowIndex(button);
    Integer column = GridPane.getColumnIndex(button);
    if (row==1)
    {
        System.out.println("Opponent's Hand, Card "+Integer.toString(column+1));
    }
    if (row==3)
    {
        System.out.println("Your Hand, Card "+Integer.toString(column+1));
    }
    Image image = new Image(getClass().getResourceAsStream("/resources/images/Ace-of-Clubs.png"));
    ImageView imageView = new ImageView(image);
    imageView.setFitHeight(80);
    imageView.setFitWidth(50);
    button.setGraphic(imageView);
}

What does the url for the image have to be? Or have I set it up wrong?

Any help would be much appreciated:)

Thankyou

Community
  • 1
  • 1

1 Answers1

1

I have managed to solve it but i still don't know why it gives that exception though. But the solution is:

Image image = new Image(new FileInputStream("image.jpg"));

This will do :)

xogah
  • 34
  • 4