2017-02-23 1 views
0

基本的に挑戦はタイトルにあります。それはかなり基本的ですが、私はそれがほとんどだと思います。私が持っている1つの問題は、カードの絵がどこから来ているのかということです。私は通常あなたのコンピュータから来ていることを知っていますが、私は写真がWeb URLから来ているところでやっています。これは私が今まで持っているものですが、私はエラー(InvocationTargetException、ランタイム、およびIndexOutOfBounds)のホストを取得しています。3枚のランダムカードを表示JavaFX

import java.util.ArrayList; 
import javafx.application.Application; 
import javafx.stage.Stage; 
import javafx.scene.Scene; 
import javafx.scene.image.ImageView; 
import javafx.scene.image.Image; 
import javafx.scene.layout.HBox; 
public class Exercise14_03 extends Application{ 
    @Override 
    public void start(Stage primaryStage){ 
     ArrayList<String> cards = new ArrayList<>(); 

     for(int i = 0; i < 52; i++){ 
      cards.add(String.valueOf(i + 1)); 

     java.util.Collections.shuffle(cards); 

     ImageView view1 = new ImageView(new Image("https://liveexample.pearsoncmg.com/book/image/card/" + cards.get(0) + ".png")); 
     ImageView view2 = new ImageView(new Image("https://liveexample.pearsoncmg.com/book/image/card/" + cards.get(1) + ".png")); 
     ImageView view3 = new ImageView(new Image("https://liveexample.pearsoncmg.com/book/image/card/" + cards.get(2) + ".png")); 

     HBox root = new HBox(); 

     root.getChildren().add(view1); 
     root.getChildren().add(view2); 
     root.getChildren().add(view3); 

     Scene scene = new Scene(root); 

     primaryStage.setTitle("Exercise14_03"); 
     primaryStage.setScene(scene); 
     primaryStage.show(); 
     } 
    } 

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

(例外)

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.IndexOutOfBoundsException: Index: 1, Size: 1 
    at java.util.ArrayList.rangeCheck(ArrayList.java:653) 
    at java.util.ArrayList.get(ArrayList.java:429) 
    at Exercise14_03.start(Exercise14_03.java:29) 
    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

正確な例外メッセージを投稿できますか? –

+0

インデントなどを修正しないでください。エラーが明らかになります。 –

答えて

1

人々がなど、インデントのためのスタイルガイドを使用する理由は、それがエラーの可能性を減少させることです。あなたのコード取る場合:

import java.util.ArrayList; 
import javafx.application.Application; 
import javafx.stage.Stage; 
import javafx.scene.Scene; 
import javafx.scene.image.ImageView; 
import javafx.scene.image.Image; 
import javafx.scene.layout.HBox; 
public class Exercise14_03 extends Application{ 
    @Override 
    public void start(Stage primaryStage){ 
     ArrayList<String> cards = new ArrayList<>(); 

     for(int i = 0; i < 52; i++){ 
      cards.add(String.valueOf(i + 1)); 

     java.util.Collections.shuffle(cards); 

     ImageView view1 = new ImageView(new Image("https://liveexample.pearsoncmg.com/book/image/card/" + cards.get(0) + ".png")); 
     ImageView view2 = new ImageView(new Image("https://liveexample.pearsoncmg.com/book/image/card/" + cards.get(1) + ".png")); 
     ImageView view3 = new ImageView(new Image("https://liveexample.pearsoncmg.com/book/image/card/" + cards.get(2) + ".png")); 

     HBox root = new HBox(); 

     root.getChildren().add(view1); 
     root.getChildren().add(view2); 
     root.getChildren().add(view3); 

     Scene scene = new Scene(root); 

     primaryStage.setTitle("Exercise14_03"); 
     primaryStage.setScene(scene); 
     primaryStage.show(); 
     } 
    } 

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

をし、それを正しくインデント、あなたを(ほとんどのIDEはEclipseで、たとえば、あなたのためにこれを行いますあなたは、CTRL-Aですべてを選択し、CTRL-SHIFT-Fでそれをフォーマットすることができます)さて、問題は明らかである

import java.util.ArrayList; 
import javafx.application.Application; 
import javafx.stage.Stage; 
import javafx.scene.Scene; 
import javafx.scene.image.ImageView; 
import javafx.scene.image.Image; 
import javafx.scene.layout.HBox; 

public class Exercise14_03 extends Application { 
    @Override 
    public void start(Stage primaryStage) { 
     ArrayList<String> cards = new ArrayList<>(); 

     for (int i = 0; i < 52; i++) { 
      cards.add(String.valueOf(i + 1)); 

      java.util.Collections.shuffle(cards); 

      ImageView view1 = new ImageView(
        new Image("https://liveexample.pearsoncmg.com/book/image/card/" + cards.get(0) + ".png")); 
      ImageView view2 = new ImageView(
        new Image("https://liveexample.pearsoncmg.com/book/image/card/" + cards.get(1) + ".png")); 
      ImageView view3 = new ImageView(
        new Image("https://liveexample.pearsoncmg.com/book/image/card/" + cards.get(2) + ".png")); 

      HBox root = new HBox(); 

      root.getChildren().add(view1); 
      root.getChildren().add(view2); 
      root.getChildren().add(view3); 

      Scene scene = new Scene(root); 

      primaryStage.setTitle("Exercise14_03"); 
      primaryStage.setScene(scene); 
      primaryStage.show(); 
     } 
    } 

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

取得:ほとんどのあなたの全体のstart(...)方法は、あなたのforループ内です。ループの最初の反復で、最初の要素を追加してから、要素0,1、および2にアクセスして、IndexOutOfBoundsExceptionを発生させます。

ちょうどforループの外にコードを移動:

import java.util.ArrayList; 
import javafx.application.Application; 
import javafx.stage.Stage; 
import javafx.scene.Scene; 
import javafx.scene.image.ImageView; 
import javafx.scene.image.Image; 
import javafx.scene.layout.HBox; 

public class Exercise14_03 extends Application { 
    @Override 
    public void start(Stage primaryStage) { 
     ArrayList<String> cards = new ArrayList<>(); 

     for (int i = 0; i < 52; i++) { 
      cards.add(String.valueOf(i + 1)); 
     } 

     java.util.Collections.shuffle(cards); 

     ImageView view1 = new ImageView(
       new Image("https://liveexample.pearsoncmg.com/book/image/card/" + cards.get(0) + ".png")); 
     ImageView view2 = new ImageView(
       new Image("https://liveexample.pearsoncmg.com/book/image/card/" + cards.get(1) + ".png")); 
     ImageView view3 = new ImageView(
       new Image("https://liveexample.pearsoncmg.com/book/image/card/" + cards.get(2) + ".png")); 

     HBox root = new HBox(); 

     root.getChildren().add(view1); 
     root.getChildren().add(view2); 
     root.getChildren().add(view3); 

     Scene scene = new Scene(root); 

     primaryStage.setTitle("Exercise14_03"); 
     primaryStage.setScene(scene); 
     primaryStage.show(); 
    } 

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

how to read the stack traceを学ぶのにも有用です。

+0

Idkなぜ私はhahaが見えなかったのですか?そして、私はまだあなたが私の最後に投稿したものと同じように見えるので、私はまだstackoverflowのコピーと貼り付けに慣れていると思います。ご協力ありがとうございました! – Jmpollock56

+0

@ Jmpollock56正しくインデントされていないため、またエラーメッセージの読み方が気にならなかったので、表示されませんでした。 –

関連する問題