如何修复我的java fx问题?

时间:2018-04-28 17:00:18

标签: java javafx stack trace

我的任务有问题。我应该使用JavaFX创建NFL草案,但从未使用过JavaFX我不知道如何解决任何问题。如果有人可以帮助我,那将是惊人的。我已经发布了我的代码以及所有堆栈跟踪错误。我知道其中一个错误是在代码的position.equals部分,我不知道如何解决这个问题。我对编码以及本网站都很陌生。

Exception in Application start method
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 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(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
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$154(LauncherImpl.java:182)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.Error: Unresolved compilation problem: 
position cannot be resolved

at application.Main.playerInfo(Main.java:80)
at application.Main.allPlayersVBoxList(Main.java:111)
at application.Main.main(Main.java:124)
at application.Main.start(Main.java:162)
at         com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$161(Launc.herImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$174(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$172(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$173(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)

运行应用程序应用程序的异常。主要

package application;
import java.util.ArrayList;
import java.util.Optional;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonType;
import javafx.scene.control.Label;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;


public class Main extends Application {
    private static Stage primaryStage = new Stage();
    private ArrayList<NFLPlayers> DRAFT_LIST = new ArrayList<>();


    public HBox navigationBar() {
        HBox navigationBtnsHBox = new HBox();

        navigationBtnsHBox.setAlignment(Pos.CENTER);

        Button btn1 = new Button("Main Menu");
        Button btn2 = new Button("NFL PLayers");

        btn1.setOnAction(e -> {
                main();
        });

        btn2.setOnAction(e -> myDraft());

        navigationBtnsHBox.getChildren().addAll(btn1, btn2);
        return navigationBtnsHBox;
    }



    public HBox playerInfo (NFLPlayers player) {
        HBox listOnePlayerHBox = new HBox();

        Button removeBtn = new Button ("Remove Player");

        Button addBtn = new Button("Add Player");

        addBtn.setOnAction(e ->{
            DRAFT_LIST.add(player);
            listOnePlayerHBox.getChildren().remove(addBtn);
            listOnePlayerHBox.getChildren().add(removeBtn);

            Alert confirmAddingPlayer = new Alert(AlertType.INFORMATION);
            confirmAddingPlayer.setHeaderText("You have added a new player!");
            confirmAddingPlayer.setContentText(((NFLPlayers) player).getName() + "has been added to your NFL Draft.");
            confirmAddingPlayer.show();
        });
        removeBtn.setOnAction(ae -> {
            Alert confirmRemovePlayer = new Alert(AlertType.CONFIRMATION);
            confirmRemovePlayer.setTitle("NFL Draft");
            confirmRemovePlayer.setHeaderText("Are you sure you want to remove this player?");
            confirmRemovePlayer.setContentText("Are you sure you want to remove this player?");
            Optional<ButtonType> choice = confirmRemovePlayer.showAndWait();
            if(choice.get() == ButtonType.OK) {
                DRAFT_LIST.remove(player);
                listOnePlayerHBox.getChildren().remove(removeBtn);
                listOnePlayerHBox.getChildren().add(addBtn);

            } else {
                confirmRemovePlayer.close();


            };
        });
        if (position.equals("offensive")) {
            OffensivePlayers op = (OffensivePlayers)DRAFT_LIST.get(0);
            System.out.print(op.getFumbles() + op.getYards() + op.getAverage() + op.getTouchdowns());
        }


        else {
            DefensivePlayers dp = (DefensivePlayers) DRAFT_LIST.get(0);
            System.out.println(dp.getTouchdowns() + dp.getInterceptions() + dp.getYards() + dp.getTackles() + dp.getAssists());
        }


        Label playerInformation = new Label ("Player Name: " + player.getName() + "\n" + "Player age: " + player.getAge() + "\n" + "Player height: "+ player.getHeight() + "\n" + "Player weight: " + player.getWeight() + "\n" + "Player position: " + player.getPosition() + "\n" +"Player's college: " + player.getCollege() + player.getTeam() + player.getExperienceInYears() + ((OffensivePlayers) player).getFumbles() + ((OffensivePlayers) player).getYards() + "\n" + "Offensive layer's Average: " +((OffensivePlayers) player).getAverage() + "\n" + "Offensive Player's Touchdowns: " + ((OffensivePlayers) player).getTouchdowns() +"\n" + "Defensive Player's Interceptions: " + ((DefensivePlayers) player).getInterceptions() + "\n" + "Defensive Player's Yards: " + ((DefensivePlayers) player).getYards() + "\n" + "Defensive Player's Touchdowns: " + ((DefensivePlayers) player).getTouchdowns() + "\n" + "Defensive Player's Assists: " + ((DefensivePlayers) player).getAssists() + "\n" + "Defensive Player's Tackles: " +  ((DefensivePlayers) player).getTackles());
        playerInformation.setPadding(new Insets(0, 0, 25, 25));
        playerInformation.setMinWidth(150);


        if (DRAFT_LIST.contains(player))
            listOnePlayerHBox.getChildren().addAll(playerInformation, addBtn);
        else
            listOnePlayerHBox.getChildren().addAll(playerInformation, removeBtn);
        return listOnePlayerHBox;



        }
    public VBox allPlayersVBoxList(ArrayList<NFLPlayers> list) {
        VBox playersList = new VBox();
        playersList.setMinWidth(555);

        for (int i=0; i< list.size(); i++) {
            playersList.getChildren().add(playerInfo(list.get(i)));
        }
        return playersList;

    }
    public void main(){
        VBox mainVBox = new VBox();
        mainVBox.setAlignment(Pos.CENTER);

        Label aTitle = new Label("NFL Draft");

        HBox mainNavigationBar = navigationBar();

        mainVBox.getChildren().addAll(aTitle, allPlayersVBoxList(PlayerManager.PLAYERS_LIST), mainNavigationBar);

        Scene scene = new Scene(mainVBox, 600, 700);
        primaryStage.setScene(scene);
        primaryStage.setTitle("NFL Draft");
        primaryStage.show();

    }
    public void myDraft() {
        VBox myDraftMainVBox = new VBox();
        myDraftMainVBox.setAlignment(Pos.TOP_CENTER);

        Label draftTitle = new Label("My Team");
        draftTitle.setPadding(new Insets (25, 0, 50, 0));

        HBox navBar = navigationBar();
        navBar.getChildren().remove(1);

        if(DRAFT_LIST.size() > 0) {
            myDraftMainVBox.getChildren().addAll(draftTitle, allPlayersVBoxList(DRAFT_LIST), navBar);
            myDraftMainVBox.setPadding(new Insets(0, 0, 25, 25));
        }
        else {
            Label emptyList = new Label ("Browse Players And Add Them To Your Draft!");
            myDraftMainVBox.getChildren().addAll(draftTitle, emptyList, navBar);
        }

        Scene scene = new Scene(myDraftMainVBox, 500, 700);
        primaryStage.setScene(scene);
        primaryStage.setTitle("NFL Draft");
        primaryStage.show();
    }
    @Override
    public void start(Stage primaryStage) {
        try {

            PlayerManager playerManager = new PlayerManager();
            main();

    } catch(Exception e) {
        e.printStackTrace();
    }
}

public static void main(String[] args) {
    launch(args);
}

1 个答案:

答案 0 :(得分:1)

您在position方法中使用playerInfo(NFLPlayers)的方式不正确。目前,position未定义。

在您提及的问题评论中,您在position课程中定义了NFLPlayers。但是,您不能使用NFLPlayers方法参数(名为player)来访问position。假设position的{​​{1}}字段NFLPlayersMain可见,则更改:

if (position.equals("offensive") {

if (player.position.equals("offensive") {

就像我说的,这假设您的NFLPlayers类被定义为:

public class NFLPlayers {
    public String position;
    // Or...
    // String position; <--- depending on if NFLPLayers is in the same package as Main
}

如果封装position(更好,或至少更多Java方式),如:

public class NFLPLayers {

    private String position;

    public String getPosition() {
        return position;
    }

    // if position is writable
    public void setPosition(String position) {
        this.position = position;
    }

}

然后if语句应为:

if (player.getPosition().equals("offensive")) {

或者,甚至更好,因为如果NullPointerException返回getPosition(),则可以避免潜在的null

if ("offensive".equals(player.getPosition()) {