线程“ JavaFX Application Thread”中的异常java.lang.NullPointerException:输入流不得为空

时间:2019-12-22 20:05:29

标签: java javafx nullpointerexception

所以我创建了一个效果很好的工具。该程序的目的基本上是

按钮cardButton =新按钮(“新手”);

被按下/激活,将显示一组随机生成的图像。我可以运行该程序并轻松按相应按钮20-30次,但迟早我会收到以下错误消息:

  

线程“ JavaFX Application Thread”中的异常   java.lang.NullPointerException:输入流不能为null   javafx.graphics/javafx.scene.image.Image.validateInputStream(Image.java:1117)     在javafx.graphics/javafx.scene.image.Image。(Image.java:701)     在sample.Main.lambda $ start $ 2(Main.java:162)

实际上,此错误消息的时间更长,但这是前3行。我已经阅读了其他一些具有相同问题的线程,但是仍然无法解决。我经常读到有关制作URL图像路径的信息,但我尝试过的所有操作均无济于事。

还有其他想法吗? Main.java:162是行

Image flopTwo = new Image(input11);

在下面的代码中。

package sample;

import javafx.application.Application;
import javafx.event.ActionEvent;

import javafx.geometry.HPos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
import java.io.InputStream;


public class Main extends Application{

    private Label potsizeLabel;
    private Label stacksizeLabel;
    private Label betsizeLabel;
    private Potsize potsize;
    private Potsize betsize;
    private Potsize stacksize;
    private int potSizeValue;
    private int betSizeValue;
    private int stackSizeValue;

    private void initPot() {
        MyMath stacks = new MyMath();
        potSizeValue = stacks.createPot();
        betSizeValue = stacks.createBet();
        stackSizeValue = stacks.createStack();

        potsize = new Potsize(potSizeValue);
        betsize = new Potsize(betSizeValue);
        stacksize = new Potsize(stackSizeValue);

        potsizeLabel.setText(potsize.toString()+"$");
        stacksizeLabel.setText(stacksize.toString()+"$");
        betsizeLabel.setText(betsize.toString()+"$");
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        primaryStage.setTitle("Pot Odds Trainer");

        Class<?> genericClass = this.getClass();
        InputStream input2 = genericClass.getResourceAsStream("/sample/img/chipsREDsmall.png");

        InputStream input5 = genericClass.getResourceAsStream("/sample/img/chipsBLUEstack.png");
        InputStream input6 = genericClass.getResourceAsStream("/sample/img/chipsGREENstack.png");
        InputStream input13 = genericClass.getResourceAsStream("/sample/img/cardback.png");
        InputStream input14 = genericClass.getResourceAsStream("/sample/img/cardback.png");
        InputStream input15 = genericClass.getResourceAsStream("/sample/img/cardback.png");
        InputStream input16 = genericClass.getResourceAsStream("/sample/img/cardback.png");
        InputStream input17 = genericClass.getResourceAsStream("/sample/img/cardback.png");
        InputStream input18 = genericClass.getResourceAsStream("/sample/img/cardback02.png");
        InputStream input19 = genericClass.getResourceAsStream("/sample/img/cardback02.png");


        Image image2 = new Image(input2);
        ImageView redChips = new ImageView(image2);
        Image image5 = new Image(input5);
        ImageView blueChipsStack = new ImageView(image5);
        Image image6 = new Image(input6);
        ImageView greenChipStack = new ImageView(image6);
        Image image15 = new Image(input15);
        ImageView cardback01 = new ImageView(image15);
        Image image16 = new Image(input16);
        ImageView cardback02 = new ImageView(image16);
        Image image17 = new Image(input17);
        ImageView cardback03 = new ImageView(image17);
        Image turnRiver= new Image(input13);
        ImageView turnFlop = new ImageView(turnRiver);
        Image flopRiver= new Image(input14);
        ImageView riverFlop = new ImageView(flopRiver);
        Image handBack01= new Image(input18);
        ImageView cardBack04 = new ImageView(handBack01);
        Image handBack02= new Image(input19);
        ImageView cardBack05 = new ImageView(handBack02);

        GridPane gridPane = new GridPane();
        gridPane.setHgap(10);
        gridPane.setVgap(10);
        gridPane.setGridLinesVisible(false);

        Label potsizeText = new Label("Current Potsize: ");
        Label betsizeText = new Label("Amount to call: ");
        Label stacksizeText = new Label("Current Stacksize: ");

        Label potOddsLabel = new Label("Pot Odds:");
        Label textNotify = new Label();

        TextField insertText = new TextField();
        insertText.setMaxWidth(50);

        // Submit Button.
        Button submitButton = new Button("Submit");
        submitButton.setOnAction(e ->
                {
                    textNotify.setText(MyMath.checkOdds(insertText.getText(), (double) betSizeValue / (potSizeValue + betSizeValue)));
                }
        );

        Button cardButton = new Button("New Hand");
        Button flopButton = new Button("New Flop");

        // Restart Button.
        Button restartButton = new Button("New Pot");
        restartButton.setOnAction((ActionEvent event) -> {
            textNotify.setText("");
            initPot();
        });

        potsizeLabel = new Label();
        stacksizeLabel = new Label();
        betsizeLabel = new Label();


        gridPane.add(potsizeText, 4, 7, 2, 1);
        gridPane.add(betsizeText, 4, 8, 2, 1);
        gridPane.add(stacksizeText, 4, 9, 2, 1);
        gridPane.add(potsizeLabel, 5, 7, 2, 1);
        gridPane.add(betsizeLabel, 5, 8, 2, 1);
        gridPane.add(stacksizeLabel, 5, 9, 2, 1);

        // Chipstacks for the stack sizes.
        gridPane.add(blueChipsStack, 5, 7, 1, 1);
        gridPane.add(redChips, 5, 8, 1, 1);
        gridPane.add(greenChipStack, 5, 9, 1, 1);


        gridPane.add(potOddsLabel, 4, 12, 4, 1);
        gridPane.add(insertText, 5, 12, 2, 1);
        gridPane.add(textNotify, 4, 15, 3, 1);

        // Buttons.
        gridPane.add(restartButton, 4, 13, 1, 1);
        gridPane.add(submitButton, 5, 12, 1, 1);
        gridPane.add(cardButton, 10, 5, 4, 1);
        gridPane.add(flopButton, 4, 5, 4, 1);

        // Board for the five board cards.
        gridPane.add(cardback01, 4, 4, 1, 1);
        gridPane.add(cardback02, 5, 4, 1, 1);
        gridPane.add(cardback03, 6, 4, 1, 1);
        gridPane.add(turnFlop, 7, 4, 1, 1);
        gridPane.add(riverFlop, 8, 4, 1, 1);

        // Board for the two hand cards.
        gridPane.add(cardBack04, 10, 4, 2, 1);
        gridPane.add(cardBack05, 12, 4, 2, 1);

        // Flop Button.
        flopButton.setOnAction(e ->
                {
                    Card card = new Card();

                    InputStream input10 = genericClass.getResourceAsStream("/sample/img/" +card.getFirstBoardRanks() + card.getFirstBoardSuits() + ".png");
                    Image flopOne = new Image(input10);
                    ImageView firstFlop = new ImageView(flopOne);
                    gridPane.add(firstFlop, 4, 4, 1, 1);


                    InputStream input11 = genericClass.getResourceAsStream("/sample/img/" +card.getFirstBoardRanks() + card.getFirstBoardSuits() + ".png");
                    Image flopTwo = new Image(input11);
                    ImageView secondFlop = new ImageView(flopTwo);
                    gridPane.add(secondFlop, 5, 4, 2, 1);

                    InputStream input12 = genericClass.getResourceAsStream("/sample/img/" +card.getFirstBoardRanks() + card.getFirstBoardSuits() + ".png");
                    Image flopThree = new Image(input12);
                    ImageView thirdFlop = new ImageView(flopThree);
                    gridPane.add(thirdFlop, 6, 4, 1, 1);

                }
        );

        // Hand Button.
        cardButton.setOnAction(e ->
                {
                    Card card = new Card();
                    String rank = card.getFirstBoardRanks();
                    String suit = card.getFirstBoardSuits();
                //  System.out.println("Neue Karte: " + rank + " " + suit);

                    InputStream input8 = genericClass.getResourceAsStream("img/" +card.getFirstBoardRanks() + card.getFirstBoardSuits() + ".png");
                    Image handCardOne = new Image(input8);
                    ImageView firstHand = new ImageView(handCardOne);
                    gridPane.add(firstHand, 10, 4, 2, 1);

                    InputStream input9 = genericClass.getResourceAsStream("img/" +card.getFirstBoardRanks() + card.getFirstBoardSuits() + ".png");
                    Image handCardTwo = new Image(input9);
                    ImageView secondHand = new ImageView(handCardTwo);
                    gridPane.add(secondHand, 12, 4, 2, 1);
                }
        );

        GridPane.setHalignment(blueChipsStack, HPos.RIGHT);
        GridPane.setHalignment(redChips, HPos.RIGHT);
        GridPane.setHalignment(greenChipStack, HPos.RIGHT);
        GridPane.setHalignment(submitButton, HPos.RIGHT);

        Scene scene = new Scene(gridPane, 1250, 675);
        scene.getStylesheets().add(Main.class.getResource("css/styles.css").toExternalForm());

        primaryStage.setScene(scene);
        primaryStage.show();

    }
}

0 个答案:

没有答案