java - 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);
}
解决方案
您在方法中使用position
的playerInfo(NFLPlayers)
方式不正确。目前,position
未定义。
在您提到的问题评论中,您已在课堂上position
定义。NFLPlayers
但是,您不使用您的NFLPlayers
方法参数(名为player
)来访问position
. 假设字段position
是NFLPlayers
可见的,Main
然后更改:
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
的 s ifgetPosition()
返回null
:
if ("offensive".equals(player.getPosition()) {
推荐阅读
- mysql - 如何将文本框绑定到绑定到 DataTable 的 DataGrid 的 SelectedItem
- xcode - 为什么会出现“将 app_id 作为 gadapplicationidentifier 写入密钥”提示?
- reactjs - uniform1i 切换到错误的纹理单元
- google-cloud-platform - 谷歌云平台权限错误:为什么错误消息没有说明哪个帐户/服务帐户正在尝试该操作?
- java - 如何在抽象数组列表中添加抽象数组列表
- pandas - 如何对 pandas 应用滚动 t.test?
- docker - Mac M1 上的 Apache Airflow 安装问题
- regex - 在 Perl 中修剪回车符 (\r)
- java - JPA 在 @ManyToOne 和 @OneToMany 中持久化和获取
- java - VSCode Maven“X.java 不在项目 Y 的类路径中”