因此,每当我尝试运行我的应用程序时,我都会遇到很多错误,但如果我从create方法中删除model
代码,应用程序将运行没有任何问题。
我想做的是做一个记忆游戏并将存储卡添加到场景中,这样你就可以用图形方式看到这些卡片。
public class memory3 extends Application {
private Cards card;
private Model model;
private Parent create(){
Pane root = new Pane();
root.setPrefSize(1280, 720);
model = new Model("Ben", "Jerry", 10);
ArrayList<Cards> cards = model.getCards();
List<Tile> tiles = new ArrayList<>();
for (Cards card1 : cards) {
tiles.add(new Tile(card1.getValue()));
}
return root;
}
@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("Memory");
primaryStage.setScene(new Scene(create()));
primaryStage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
模型构造函数,设置游戏:
public Model(String name1, String name2, int pairs){
player1 = name1;
player2 = name2;
score1 = 0;
score2 = 0;
char c = 'A';
for(int i = 0; i < pairs; i++){
cards.add(new Cards(c));
cards.add(new Cards(c));
c++;
}
//Shuffle cards
Collections.shuffle(cards);
}
Tile class,以图形方式创建存储卡:
public class Tile extends StackPane{
private Text text = new Text();
public Tile(char value){
Rectangle border = new Rectangle(50,50);
border.setFill(null);
border.setStroke(Color.BLACK);
text.setText(String.valueOf(value));
text.setFont(Font.font(30));
setAlignment(Pos.CENTER);
getChildren().addAll(border, text);
}
}
错误:
Executing C:\Java Objektorienterad\memory33\memory3\dist\run1052614504\memory3.jar using platform C:\Program Files\Java\jdk1.8.0_101\jre/bin/java
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$155(LauncherImpl.java:182)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NullPointerException
at model.Model.<init>(Model.java:32)
at view.memory3.create(memory3.java:32)
at view.memory3.start(memory3.java:49)
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 view.memory3