前提・実現したいこと
現在、JavaFXでpingコマンドのデモを作成しようと考えています。
しかし、コンパイルも実行もできるのですが、時折以下のエラーメッセージが発生してしまいます。
お恥ずかしい話、どこからエラーが出ているかさっぱりな状態です。
どのようにエラー処理すればよいのか教えていただければと思います。
発生している問題・エラーメッセージ
「実行」のボタンを押すと時折このエラーが発生します。
Exception in thread "JavaFX Application Thread" java.lang.NullPointerException at javafx.graphics/com.sun.javafx.text.PrismTextLayout.addTextRun(PrismTextLayout.java:756) at javafx.graphics/com.sun.javafx.text.GlyphLayout.addTextRun(GlyphLayout.java:140) at javafx.graphics/com.sun.javafx.text.GlyphLayout.breakRuns(GlyphLayout.java:210) at javafx.graphics/com.sun.javafx.text.PrismTextLayout.buildRuns(PrismTextLayout.java:771) at javafx.graphics/com.sun.javafx.text.PrismTextLayout.layout(PrismTextLayout.java:1022) at javafx.graphics/com.sun.javafx.text.PrismTextLayout.ensureLayout(PrismTextLayout.java:222) at javafx.graphics/com.sun.javafx.text.PrismTextLayout.getBounds(PrismTextLayout.java:245) at javafx.graphics/javafx.scene.text.Text.getLogicalBounds(Text.java:430) at javafx.graphics/javafx.scene.text.Text.doComputeGeomBounds(Text.java:1173) at javafx.graphics/javafx.scene.text.Text.access$300(Text.java:127) at javafx.graphics/javafx.scene.text.Text$1.doComputeGeomBounds(Text.java:148) at javafx.graphics/com.sun.javafx.scene.shape.TextHelper.computeGeomBoundsImpl(TextHelper.java:90) at javafx.graphics/com.sun.javafx.scene.NodeHelper.computeGeomBounds(NodeHelper.java:117) at javafx.graphics/javafx.scene.Node.updateGeomBounds(Node.java:3831) at javafx.graphics/javafx.scene.Node.getGeomBounds(Node.java:3793) at javafx.graphics/javafx.scene.Node.getLocalBounds(Node.java:3741) at javafx.graphics/javafx.scene.Node.updateTxBounds(Node.java:3895) at javafx.graphics/javafx.scene.Node.getTransformedBounds(Node.java:3687) at javafx.graphics/javafx.scene.Parent.getChildTransformedBounds(Parent.java:1864) at javafx.graphics/javafx.scene.Parent.recomputeBounds(Parent.java:1656) at javafx.graphics/javafx.scene.Parent.doComputeGeomBounds(Parent.java:1520) at javafx.graphics/javafx.scene.Parent.access$200(Parent.java:81) at javafx.graphics/javafx.scene.Parent$1.doComputeGeomBounds(Parent.java:117) at javafx.graphics/com.sun.javafx.scene.ParentHelper.computeGeomBoundsImpl(ParentHelper.java:86) at javafx.graphics/com.sun.javafx.scene.NodeHelper.computeGeomBounds(NodeHelper.java:117) at javafx.graphics/javafx.scene.Node.updateGeomBounds(Node.java:3831) at javafx.graphics/javafx.scene.Node.getGeomBounds(Node.java:3793) at javafx.graphics/javafx.scene.Node.getLocalBounds(Node.java:3741) at javafx.graphics/javafx.scene.Node.updateTxBounds(Node.java:3895) at javafx.graphics/javafx.scene.Node.getTransformedBounds(Node.java:3687) at javafx.graphics/javafx.scene.Parent.getChildTransformedBounds(Parent.java:1864) at javafx.graphics/javafx.scene.Parent.updateCachedBounds(Parent.java:1728) at javafx.graphics/javafx.scene.Parent.recomputeBounds(Parent.java:1667) at javafx.graphics/javafx.scene.Parent.doComputeGeomBounds(Parent.java:1520) at javafx.graphics/javafx.scene.Parent.access$200(Parent.java:81) at javafx.graphics/javafx.scene.Parent$1.doComputeGeomBounds(Parent.java:117) at javafx.graphics/com.sun.javafx.scene.ParentHelper.computeGeomBoundsImpl(ParentHelper.java:86) ~以下略~
該当のソースコード
java
1import javafx.application.Application; 2import javafx.geometry.Insets; 3import javafx.scene.Scene; 4import javafx.scene.control.*; 5import javafx.scene.layout.*; 6import javafx.stage.Stage; 7import javafx.scene.paint.Color; 8import javafx.scene.text.Font; 9import java.util.*; 10 11public class pp extends Application { 12Label l1, l2,l3; // ラベル 13Label[] lb = new Label[5];//ラジオボタン 14 String[] lbin = {" →自分のPCのIPアドレス(仮)"," →Yahoo!のサイト"," →コロンビアのサイト"," →ブラジルのサイト"," →存在しないIPアドレス"}; 15TextField tx1, tx2; // テキストフィールド 16Button start,clear,kaisetu,ryouhou; // ボタン 17TextArea textArea,ta2; // テキストエリア 18RadioButton[] rb = new RadioButton[5];//ラジオボタン 19 ToggleGroup ping; 20 String[] cm = {"ping 10.40.83.63","ping www.yahoo.co.jp","ping www.columbia.edu","ping www.ibge.gov.br","ping 999.999.999.999"}; 21int num1, num2; 22 23public void start(Stage stage) { 24stage.setWidth(1200); 25stage.setHeight(750); 26 27////////////////////ラベル設定//////////////////// 28l1 = new Label("pingコマンドデモ"); 29l1.setFont(new Font(36)); 30//label1.setTextFill(Color.RED);色の設定 31l2 = new Label("コマンド選択してください"); 32l3=new Label(" "); 33 34for (int i = 0; i <lb.length ; i++) { 35 lb[i] = new Label(lbin[i]);//ラベルの作成 36} 37 38////////////////////ラジオボタン設定//////////////////// 39ping=new ToggleGroup(); 40for (int i = 0; i <rb.length ; i++) { 41 rb[i] = new RadioButton(cm[i]);//ラジオボタンの作成 42 rb[i].setToggleGroup(ping);//ラジオボタンをpingグループに追加 43 rb[i].setStyle("-fx-font-size: 18;");//フォント設定 44} 45rb[0].setSelected(true); 46VBox rbAll = new VBox(); 47 48for(int i=0; i <rb.length ; i++){ 49 rbAll.getChildren().add(rb[i]); 50 rbAll.getChildren().add(lb[i]); 51} 52 53//for (RadioButton r:rb) {//ラジオボタンをVBoxに入れる 54 //rbAll.getChildren().add(r); } 55 56////////////////////ボタン設定//////////////////// 57start = new Button("実行結果表示"); 58start.setPrefSize(100,50); 59start.setOnAction(event -> buttonStartPressed()); 60kaisetu = new Button("解説表示"); 61kaisetu.setPrefSize(100,50); 62kaisetu.setOnAction(event -> buttonKaisetuPressed()); 63ryouhou = new Button("両方とも表示"); 64ryouhou.setPrefSize(205,50); 65ryouhou.setOnAction(event -> buttonRyouhouPressed()); 66clear = new Button("Clear"); 67clear.setPrefSize(205,50); 68clear.setOnAction(event -> buttonClearPressed()); 69 70////////////////////テキストエリア設定//////////////////// 71textArea = new TextArea("実行結果が表示されます。\nC:¥Users>"); 72textArea.setStyle("-fx-text-fill: white;-fx-font-size: 16;"); 73ta2=new TextArea("解説を表示します。");//22文字まで表示可能 74ta2.setStyle("-fx-font-size: 14;"); 75//ta2.setPrefRowCount(10);// 行数を指定 76ta2.setPrefColumnCount(20); // 列数を設定 77ta2.setWrapText(true); 78 79////////////////////配置設定//////////////////// 80HBox hbox = new HBox(5); 81hbox.getChildren().addAll(start,kaisetu); 82 83VBox vbox = new VBox(5); 84vbox.setPadding(new Insets(20, 25, 25, 25)); 85vbox.getChildren().addAll(l1,l2,rbAll,l3,hbox,ryouhou,clear); 86 87BorderPane root = new BorderPane(); 88root.setLeft(vbox); 89root.setCenter(textArea); 90root.setRight(ta2); 91 92stage.setScene(new Scene(root)); 93stage.show(); 94 95textArea.lookup(".content").setStyle("-fx-background-color: black;");//色の変更 96} 97 98void buttonClearPressed(){ 99textArea.clear(); 100textArea.setText("実行結果が表示されます。\nC:¥Users>"); 101ta2.setText("解説が表示されます。"); 102} 103 104void buttonStartPressed(){ 105if (rb[0].isSelected()){ 106 class Hyouzi extends Thread{ 107 public void run(){ 108 textArea.appendText("ping 10.40.83.63\n10.40.83.63 に ping を送信しています 32 バイトのデータ:"); 109 for(int i=0;i<4;i++){ 110 textArea.appendText("\n10.40.83.63 からの応答: バイト数 =32 時間 <1ms TTL=128"); 111 try{ 112 Thread.sleep(1000); 113 }catch(InterruptedException e){ 114 textArea.appendText("\n\nエラーが発生しました。\n\nC:¥Users>"); 115 return; 116 } 117 118 } 119 textArea.appendText("\n\n10.40.83.63 の ping 統計:\n パケット数: 送信 = 4、受信 = 4、損失 = 0 (0% の損失)\n ラウンド トリップの概算時間 (ミリ秒):\n 最小 = 0ms、最大 = 0ms、平均 = 0ms"); 120 textArea.appendText("\n\nC:¥Users>"); 121 } 122 } 123 Hyouzi t=new Hyouzi(); 124 t.start(); } 125else if(rb[1].isSelected()){ 126 ~略~(if文の最初の内容とほぼ同じなので省略します。) 127} 128else if(rb[2].isSelected()){ 129 ~略~ 130} 131else if(rb[3].isSelected()){ 132 ~略~ 133} 134else{ 135 ~略~ 136} 137} 138 139void buttonKaisetuPressed(){ 140 if (rb[0].isSelected()){ 141 ta2.setText("自分のPC(仮)にping"); 142 } 143 else if(rb[1].isSelected()){ 144 ta2.setText("yahoo!にping"); 145 } 146 else if(rb[2].isSelected()){ 147 ta2.setText("コロンビアにping"); 148 } 149 else if(rb[3].isSelected()){ 150 ta2.setText("ブラジルにping"); 151 } 152 else{ 153 ta2.setText("偽ipアドレスにping"); 154 } 155} 156 157void buttonRyouhouPressed(){ 158 buttonKaisetuPressed(); 159 buttonStartPressed(); 160} 161 162public static void main(String[] args) { 163launch(); 164} 165} 166
お願い
がちがちの初心者なのでoracleのサイトだけ載せていただいても理解するのが難しいためどのように書けばいいか教えて頂けると幸いです。
また、質問のマナー等が間違っている、内容がわかりにくい等ありましたらご指摘お願いします。
回答1件
あなたの回答
tips
プレビュー