Java 调用目标异常,不知道该怎么办

Java 调用目标异常,不知道该怎么办,java,eclipse,javafx,Java,Eclipse,Javafx,我试过在没有使用局部变量的类的情况下解决同样的问题 一个图形用户界面应该打开,我可以提交我想要的卡片数量,并从一副牌中随机选择。以及gui上的其他按钮 public class MainGUI extends Application { public void start(Stage primaryStage) { class deck { public ArrayList <Integer> Deck;

我试过在没有使用局部变量的类的情况下解决同样的问题

一个图形用户界面应该打开,我可以提交我想要的卡片数量,并从一副牌中随机选择。以及gui上的其他按钮

public class MainGUI extends Application {

    public void start(Stage primaryStage) {

        class deck {
            public ArrayList <Integer> Deck;



            public ArrayList<Integer> shuffleDeck(ArrayList<Integer> Deck) { 
                int x = 52;
                for (int i = 1; i <= x; ++i ) {
                Deck.add(i);
                }
                Collections.shuffle(Deck);
                return Deck;

        }

            public  ArrayList<Integer> randomCardsSelector(int x, ArrayList<Integer> Deck){
                ArrayList<Integer> selectedCards = new ArrayList<Integer>();
                Random rand = new Random();
                for(int i = 1; i <= x; ++i) {
                    int newElement = rand.nextInt(Deck.size());
                    selectedCards.add(Deck.get(newElement));
                    Deck.remove(newElement);
                    }
                return selectedCards;
            }

        }



        //Horizontal box containing Label, TextField, Button, Cards drawn
        HBox topContainer = new HBox();
        topContainer.setSpacing(10);
        topContainer.setPadding(new Insets(25,50,25,50));

        //This is the label
        Label insertNbr = new Label();
        insertNbr.setText("Number of cards:");
        insertNbr.setPadding(new Insets(5,5,5,5));

        //This the TextField
        TextField cardNbr = new TextField();
        cardNbr.setPadding(new Insets(5,5,5,5));
        cardNbr.setPrefWidth(30);

        //This is the submit button
        Button submitBtn = new Button();
        submitBtn.setPadding(new Insets(5,5,5,5));
        submitBtn.setText("Submit");

        HBox newBox = new HBox();
        //This is the fetch button
        Button fetchBtn = new Button();
        fetchBtn.setPadding(new Insets(5,5,5,5));
        fetchBtn.setText("Fetch");
        fetchBtn.setDisable(true);

        //This is the button to draw a card from the array
        Button drawCardBtn = new Button("Get Card");
        drawCardBtn.setPadding(new Insets(5,5,5,5));

        Button higherBtn = new Button();
        higherBtn.setPadding(new Insets(5,5,5,5));
        higherBtn.setText("Higher");

        Button lowerBtn = new Button();
        lowerBtn.setPadding(new Insets(5,5,5,5));
        lowerBtn.setText("Lower");



        newBox.getChildren().addAll(fetchBtn,drawCardBtn,higherBtn,lowerBtn);

        ImageView drawnCard = new ImageView();

        //Spacer between left nodes and right nodes
        final Pane spacer = new Pane();
        HBox.setHgrow(spacer, Priority.ALWAYS);
        spacer.setMinSize(10, 1);

        //FlowPane to add drawn cards
        FlowPane drawnCards = new FlowPane();
        drawnCards.setPadding(new Insets(5,5,5,5));
        drawnCards.setMaxSize(360, 100);
        drawnCards.setStyle("-fx-background-color: #008080");

        topContainer.getChildren().addAll(insertNbr,cardNbr,submitBtn,spacer,drawnCards);


        /*This is the main pane that will
         * hold all of the other panes 
         * and components
         */
        BorderPane mainPane = new BorderPane();
        mainPane.setTop(topContainer);
        mainPane.setLeft(newBox);
        mainPane.setCenter(drawnCard);

        Scene scene = new Scene(mainPane, 1000, 700);
        primaryStage.setTitle("Card Game");
        primaryStage.setResizable(false);
        primaryStage.setScene(scene);
        primaryStage.show();

        deck firstDeck = new deck();
        deck secondDeck = new deck();

        firstDeck.Deck = firstDeck.shuffleDeck(firstDeck.Deck);



        submitBtn.setOnAction(e->{
            int number = Integer.parseInt(cardNbr.getText());
            secondDeck.Deck = secondDeck.randomCardsSelector(number, firstDeck.Deck);

            fetchBtn.setDisable(false);
            submitBtn.setDisable(true);

            for(int i = 0; i<number; i++) {
                int x = secondDeck.Deck.get(i);
                String url = new String("bin/"+ x +".png");
                File file1 = new File(url);
                Image image1 = new Image(file1.toURI().toString());
                ImageView imageView = new ImageView(image1);
                imageView.setFitHeight(90);
                imageView.setFitWidth(90);
                drawnCards.getChildren().add(imageView);
            }
        });



        fetchBtn.setOnAction(e->{
            fetchBtn.setDisable(true);

            ArrayList<Integer> selectedNewCard= new ArrayList<Integer>();
            selectedNewCard = firstDeck.randomCardsSelector(1, firstDeck.Deck);

            int s = selectedNewCard.get(0);
            String url = new String("bin/" + s + ".png");
            File file2 = new File(url);
            Image img = new Image(file2.toURI().toString());
            drawnCard.setImage(img);
            drawnCard.setFitHeight(150);
            drawnCard.setFitWidth(150);
        });

        drawCardBtn.setOnAction(e->{
            drawCardBtn.setDisable(false);

            ArrayList<Integer> oneCard = new ArrayList<Integer>();
            oneCard = secondDeck.randomCardsSelector(1,secondDeck.Deck);
            int number = oneCard.get(0);
            String url = new String("bin/" + number + ".png");
            File file3 = new File(url);
            Image image = new Image(file3.toURI().toString());
            drawnCard.setImage(image);
            drawnCards.getChildren().get(0).setStyle("-fx-opacity: 0.5");
        });



     //drawnCards.getChildren().get(3).setStyle("-fx-opacity: 0.5");
    }


    public static void main(String[] args) {


        launch(args);

    }

}

Here is the stack trace
Exception in Application start method
java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Na`enter code here`tive Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
    at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at sun.launcher.LauncherHelper$FXHelper.main(Unknown Source)
Caused by: java.lang.RuntimeException: Exception in Application start method
    at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$155(LauncherImpl.java:182)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NullPointerException
    at MainGUI$1deck.shuffleDeck(MainGUI.java:36)
    at MainGUI.start(MainGUI.java:135)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
    ... 1 more
Exception running application MainGUI

您的代码至少有两个问题:

  • 在方法中定义类
  • NullPointerException
  • 您在
    start()
    方法中定义了类
    deck

    public class MainGUI extends Application {
    
        public void start(Stage primaryStage) {
    
            class deck {
            }
        }
    }
    
    那是行不通的。将
    类组
    移动到
    MainGUI
    类之外:

    public class MainGUI extends Application {
    
        public void start(Stage primaryStage) {
        }
    }
    
    class deck {
    }
    
    NullPointerException

    从堆栈跟踪中:

    Caused by: java.lang.NullPointerException
        at MainGUI$1deck.shuffleDeck(MainGUI.java:36)
    
    这涉及以下方法:

    public ArrayList<Integer> shuffleDeck(ArrayList<Integer> Deck) {
        int x = 52;
        for (int i = 1; i <= x; ++i) {
            Deck.add(i);
        }
        Collections.shuffle(Deck);
        return Deck;
    
    }
    
    此处
    firstDeck.Deck
    将为
    null
    。解决此问题的一种方法是在创建新的
    Deck
    实例时立即初始化
    Deck
    类成员:

    class deck {
        public ArrayList<Integer> Deck = new ArrayList<>();
    }
    
    
    类甲板{
    public ArrayList Deck=new ArrayList();
    }
    
    您能发布准确的异常/堆栈跟踪吗?只需使用堆栈跟踪编辑它。从堆栈跟踪中,在随机播放组的第36行有一个NPE。当您实例化一个新的deck()对象时,您永远不会将deck(ArrayList)分配给任何对象。尝试将其更改为:public ArrayList Deck=new ArrayList();谢谢Jens的帮助。做了,但还是一样error@tony_h我根据你的堆栈跟踪更新了我的帖子。成功了。非常感谢你的帮助!
    class deck {
        public ArrayList<Integer> Deck = new ArrayList<>();
    }