質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
JavaFX

JavaFXとは、Java仮想マシン上で動作するリッチインターネットアプリケーション (RIA) のGUIライブラリです。Swingとは異なり、FXMLと呼ばれる XMLとCSSを併用してデザインを記述します。

Q&A

解決済

1回答

1540閲覧

JavaFXでのTableViewにおけるコンパイルエラー

junkjunk

総合スコア26

JavaFX

JavaFXとは、Java仮想マシン上で動作するリッチインターネットアプリケーション (RIA) のGUIライブラリです。Swingとは異なり、FXMLと呼ばれる XMLとCSSを併用してデザインを記述します。

0グッド

0クリップ

投稿2019/06/27 04:20

編集2019/06/27 04:32

JavaFXでTableviewを表示させたいのですが、コンパイルエラーが起きてしまいます。
エラー文からControllerクラスのinitializeメソッドを以下のように確認したところ、
getItemsする前のtableがNULL値となっており参照出来ていませんでした。

JavaFX

1System.out.println(table); 2table.getItems().add(new LogHandymanTable(101, "Lucius Junius Brutus", -550));

他クラスを確認してみましたが自分ではエラーを見つけられませんでした。
もしわかる方いらっしゃいましたらご教示お願い致します。

いつも質問ばかりですみません。

Main.java

JavaFX

1//JavaFXアプリケーションはJavaの基本通り「mainメソッド」を実装したクラスを書いて起動する 2package log_handyman; 3 4//import:mainの中でjava.io.FileをFileと省略できる 5import javafx.application.Application; 6import javafx.stage.Stage; 7import javafx.scene.Scene; 8import javafx.fxml.FXMLLoader; 9import javafx.scene.layout.AnchorPane; 10 11//JavaFXの基本形。Applicationクラスを継承して作る必要がある 12public class Main extends Application { 13 //必ずstartメソッドを実装する必要がある。アプリがスタートする際の処理を作成するためのもの 14 //Stage(引数)はpackage javafxのクラスのインスタンス 15 //StageはSUI組み込みの土台となるコンテナ 16 @Override 17 public void start(Stage primaryStage) { 18 try { 19 //FXMLファイルの読み込み 20 //Botにいくかもしれない 21 AnchorPane root = (AnchorPane)FXMLLoader.load(getClass().getResource("LogHandyman.fxml")); 22 Scene scene = new Scene(root,556,519);//ここがウィンドウの表示サイズ 23 scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm()); 24 25 //シーンをステージに貼る 26 primaryStage.setScene(scene); 27 // ステージの表示 28 //primary 29 primaryStage.show(); 30 } catch(Exception e) { 31 e.printStackTrace(); 32 } 33 } 34 35 36 //public:アクセス修飾子。どの範囲から参照可能かのスコープを制御する。publicは全てのクラスからの参照を許す 37 //static:クラスがインスタンス化(型の初期化、宣言)されていなくても、そのクラス、メソッド、変数などが参照可能 38 //void:戻り値 39 //(引数)String型の配列、 argsは配列名 40 public static void main(String[] args) { 41 //JavaFXのスレッドを起動 42 launch(args); 43 } 44} 45 46

コントローラークラス

JavaFX

1package log_handyman; 2 3import java.io.File; 4import java.io.IOException; 5import java.net.URL; 6import java.nio.file.Files; 7import java.nio.file.Path; 8import java.nio.file.Paths; 9import java.util.Calendar; 10import java.util.ResourceBundle; 11import javafx.event.ActionEvent; 12import javafx.fxml.FXML; 13import javafx.fxml.Initializable; 14import javafx.scene.control.Button; 15import javafx.scene.control.TextField; 16import javafx.scene.control.cell.PropertyValueFactory; 17import log_handyman.LogHandymanTable; 18//import javafx.scene.control.TableCell; 19import javafx.scene.control.TableView; 20import javafx.scene.control.TableColumn; 21import javafx.fxml.Initializable; 22 23 24 25 26//★レコード用のクラスを作成する 27public class LogHandymanController implements Initializable{/////変更 28 29 @FXML 30 private ResourceBundle resources; 31 32 @FXML 33 private URL location; 34 35 @FXML 36 private TextField text1; 37 38 @FXML 39 private TextField text2; 40 41 @FXML 42 private TextField text3; 43 44 @FXML 45 private Button Add; 46 47 @FXML 48 private Button Delete; 49 50 @FXML 51 private Button Zi 52 53 @FXML 54 void Action(ActionEvent event) { 55 System.out.println("Add button clicked"); 56 } 57 58  略 59 60 //////////////////////////////// 61 //Javaコード側で操作したいUIコントロールのインスタンス 62 @FXML 63 private TableView<LogHandymanTable> table; 64 @FXML 65 private TableColumn nameColumn; 66 @FXML 67 private TableColumn originalColumn; 68 @FXML 69 private TableColumn saveColumn; 70 71 72 73 74 75 76 77 //UIコントロールでイベントが発生したときに呼び出すメソッド 78 @FXML 79 private void handleButtonAction(ActionEvent event) { 8081 } 82 83 //a初期化コードを定義するメソッドinitialize 84 @Override 85 public void initialize(URL url, ResourceBundle rb) { 8687 88 89 90 nameColumn.setCellValueFactory(new PropertyValueFactory<LogHandymanTable, Integer>("name")); 91 originalColumn.setCellValueFactory(new PropertyValueFactory<LogHandymanTable, String>("original")); 92 saveColumn.setCellValueFactory(new PropertyValueFactory<LogHandymanTable, Integer>("save")); 93 94 System.out.println(table); 95 96 // aサンプルデータを1行追加 97 table.getItems().add(new LogHandymanTable(101, "Lucius Junius Brutus", -550)); 98 } 99 100}

テーブル定義クラス

JavaFX

1package log_handyman; 2 3//aテーブルに表示する3つの属性を定義するクラス 4 5//JavaBeans:銀行、情報を一時的に預かってくれる 6//a・JavaBeansにデータを格納する時は口座を開設する(newする) 7//a・データの出し入れはgetさんとsetさん経由で行う(getter/setter) 8//a・JavaBeansは1件しか保持することができないので、データを複数件格納したい時は名前を変えて複数口座開設する。 9 10//aJavaFXのBeansは、「プロパティ」というクラスを使って値の更新を知るようになっている。 11//aプロパティクラスは、整数、文字列とデータ種類ごとにクラスが用意されている。 12//a整数値であるIDの場合、フィールドに保持するプロパティはIntegerProperty型(抽象クラス)とし、 13//a実装型はここではSimpleIntegerProperty型を使っています 14//aこのフィールドのgetterメソッドは命名規約が定まっており、プロパティ名+Property です。 15 16import javafx.beans.property.IntegerProperty; 17import javafx.beans.property.SimpleIntegerProperty; 18import javafx.beans.property.SimpleStringProperty; 19import javafx.beans.property.StringProperty; 20 21public class LogHandymanTable { 22 //aラッパークラス:基本データ型の値をラップ(=包み込む)してオブジェクトとして利用できるようにするクラス 23 //Integerがラッパークラス 24 private IntegerProperty name; 25 private StringProperty original; 26 private IntegerProperty save; 27 28 public LogHandymanTable(int aName, String anOriginal, int aSave) { 29 name = new SimpleIntegerProperty(aName); 30 original = new SimpleStringProperty(anOriginal); 31 save = new SimpleIntegerProperty(aSave); 32 } 33 34 //Beansのgetterクラス? 35 public IntegerProperty idProperty() { 36 return name; 37 } 38 39 public StringProperty nameProperty() { 40 return original; 41 } 42 43 public IntegerProperty birthYearProperty() { 44 return save; 45 } 46 47}

FXML

<?xml version="1.0" encoding="UTF-8"?> <?import javafx.scene.control.Button?> <?import javafx.scene.control.Label?> <?import javafx.scene.control.TableColumn?> <?import javafx.scene.control.TableView?> <?import javafx.scene.control.TextField?> <?import javafx.scene.layout.AnchorPane?> <?import javafx.scene.shape.Polygon?> <?import javafx.scene.text.Font?> <AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="519.0" prefWidth="556.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="log_handyman.LogHandymanController"> <children> <Label layoutX="20.0" layoutY="14.0" text="Log Handyman"> <font> <Font size="24.0" /> </font> </Label> <Label layoutX="20.0" layoutY="59.0" text="title of log directory" /> <TextField fx:id="text1" layoutX="15.0" layoutY="76.0" prefHeight="25.0" prefWidth="120.0" /> <Label layoutX="155.0" layoutY="59.0" text="adrress of log directory" /> <TextField fx:id="text2" layoutX="155.0" layoutY="76.0" prefHeight="25.0" prefWidth="377.0" /> <Label layoutX="159.0" layoutY="117.0" text="addre of save directorys" /> <TextField fx:id="text3" layoutX="158.0" layoutY="141.0" prefHeight="25.0" prefWidth="377.0" /> <Polygon layoutX="266.0" layoutY="178.0" points="-15.0, 23.0, -2.0, 5.0, -30.0, 5.0" stroke="BLACK" strokeType="INSIDE" /> <Button fx:id="Add" layoutX="271.0" layoutY="180.0" mnemonicParsing="false" onAction="#Action" text="Add" /> <Button fx:id="Delete" layoutX="478.0" layoutY="180.0" mnemonicParsing="false" onAction="#handleButtonAction" text="Delete" /> <TableView editable="true" layoutX="21.0" layoutY="214.0" prefHeight="167.0" prefWidth="515.0"> <columns> <TableColumn fx:id="nameColumn" prefWidth="115.0" text="Log Name" /> <TableColumn fx:id="originalColumn" prefWidth="215.0" text="Original Address" /> <TableColumn fx:id="saveColumn" prefWidth="184.0" text="Save Address" /> </columns> </TableView>    略 <TextField fx:id="text4" layoutX="283.0" layoutY="476.0" prefHeight="25.0" prefWidth="170.0" /> <Button fx:id="Zip" layoutX="465.0" layoutY="476.0" mnemonicParsing="false" onAction="#handleButtonAction" text="Create Zip" /> </children> </AnchorPane>

エラー文

JavaFX

1null 2javafx.fxml.LoadException: 3/C:/Users/0000400435/eclipse-workspace/Log_Handyman/bin/log_handyman/LogHandyman.fxml 4 5 at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601) 6 at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2579) 7 at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441) 8 at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214) 9 at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175) 10 at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148) 11 at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124) 12 at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104) 13 at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097) 14 at log_handyman.Main.start(Main.java:21) 15 at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$161(LauncherImpl.java:863) 16 at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$174(PlatformImpl.java:326) 17 at com.sun.javafx.application.PlatformImpl.lambda$null$172(PlatformImpl.java:295) 18 at java.security.AccessController.doPrivileged(Native Method) 19 at com.sun.javafx.application.PlatformImpl.lambda$runLater$173(PlatformImpl.java:294) 20 at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95) 21 at com.sun.glass.ui.win.WinApplication._runLoop(Native Method) 22 at com.sun.glass.ui.win.WinApplication.lambda$null$147(WinApplication.java:177) 23 at java.lang.Thread.run(Unknown Source) 24Caused by: java.lang.NullPointerException 25 at log_handyman.LogHandymanController.initialize(LogHandymanController.java:280) 26 at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2548) 27 ... 17 more 28

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

FXMLファイルにおいてTableView要素の属性fx:idが定義されていないので、
FXMLファイルをロードして画面を生成した際に、コントローラークラスの
フィールド table にインスタンスが格納されません。

<TableView fx:id="table" ...

のようにfx:idを指定すればよいかと思います。

投稿2019/07/09 11:26

boochnich

総合スコア194

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

junkjunk

2019/07/10 06:50

ありがとうございます。ご指摘の通りfx:idを修正したところ表示されました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問