Search code examples
javajavafxstacktrace

How do I fix my java fx issues?


I have problems with my assignment. I am supposed to create an NFL Draft using JavaFX but having never worked with JavaFX I have no idea how to solve any of the issues. If anybody could help me, that would be amazing. I have posted my code as well as all the stack trace errors. I know that one of the errors is in the position.equals part of the code, and I have no idea how to fix that. I am very new to coding as well as this website.

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)

Exception running application application.Main

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);
}

Solution

  • The way you are using position in your playerInfo(NFLPlayers) method is incorrect. Currently, position is not defined.

    In the question comments you mention you have position defined in your NFLPlayers class. However, you don't use your NFLPlayers method argument (named player) to access the position. Assuming that the field position of NFLPlayers is visible to Main then change:

    if (position.equals("offensive") {
    

    to

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

    Like I said, this assumes that your NFLPlayers class is defined something like:

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

    If you encapsulated position (the better, or at least more Java way) like:

    public class NFLPLayers {
    
        private String position;
    
        public String getPosition() {
            return position;
        }
    
        // if position is writable
        public void setPosition(String position) {
            this.position = position;
        }
    
    }
    

    Then the if statement should be:

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

    Or, even better because you avoid potential NullPointerExceptions if getPosition() returns null:

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