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

我已经尝试过不使用类使用局部变量相同的问题。


GUI应该打开,我可以提交我想要的牌的数量,并将从一副牌中随机选择。和 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);


        });


我已经尝试过不使用类使用局部变量相同的问题。


慕妹3242003
浏览 81回答 1
1回答

冉冉说

您的代码至少有两个问题:在方法中定义类空点器异常您在方法中定义了类:deckstart()public class MainGUI extends Application {&nbsp; &nbsp; public void start(Stage primaryStage) {&nbsp; &nbsp; &nbsp; &nbsp; class deck {&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}这是行不通的。移动类的外部:class deckMainGUIpublic class MainGUI extends Application {&nbsp; &nbsp; public void start(Stage primaryStage) {&nbsp; &nbsp; }}class deck {}空点器异常从堆栈跟踪中:Caused by: java.lang.NullPointerException&nbsp; &nbsp; at MainGUI$1deck.shuffleDeck(MainGUI.java:36)这涉及以下方法:public ArrayList<Integer> shuffleDeck(ArrayList<Integer> Deck) {&nbsp; &nbsp; int x = 52;&nbsp; &nbsp; for (int i = 1; i <= x; ++i) {&nbsp; &nbsp; &nbsp; &nbsp; Deck.add(i);&nbsp; &nbsp; }&nbsp; &nbsp; Collections.shuffle(Deck);&nbsp; &nbsp; return Deck;}唯一可以在这里(因此导致异常)的实数变量是变量。所以让舒尔它不是。nullDecknull在您的代码中,它是 null,因为您创建了一个新实例并期望设置类成员,但它实际上是 。deckDecknull看:deck firstDeck = new deck();firstDeck.Deck = firstDeck.shuffleDeck(firstDeck.Deck);这里将是.解决此问题的一种方法是在创建新实例时立即初始化类成员:firstDeck.DecknullDeckdeckclass deck {&nbsp; &nbsp; public ArrayList<Integer> Deck = new ArrayList<>();}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java