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

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

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

TableView(UITableView)とは、リスト形式で表示するコントロールで、ほとんどのアプリに使用されています。画面を「行」に分けて管理し、一般的には各行をタップした際に詳細画面に移動します。

JavaFX

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

Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Q&A

0回答

1046閲覧

TableViewにデータを追加したい

ryu-bamboo

総合スコア9

TableView

TableView(UITableView)とは、リスト形式で表示するコントロールで、ほとんどのアプリに使用されています。画面を「行」に分けて管理し、一般的には各行をタップした際に詳細画面に移動します。

JavaFX

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

Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

0グッド

0クリップ

投稿2021/04/09 10:53

編集2021/04/11 09:11

前提・実現したいこと

 JavaFXでSchedule表を作っています。
そこで、TableViewにデータを入れたいと思い、サイトを参考にしてCellクラス、データクラスを作って実装しようとしたところ、NullPointExceptionが起きてしまいました。
一日粘ってみたのですが、Controllerクラスがどう実行されるか理解出来ていないのもあり、何が原因かわからず、質問させていただきました。よろしくお願いします。

参考にしたサイト:Cellをカスタマイズ

発生している問題・エラーメッセージ

java.lang.NullPointerException at fxproject/application.ScheduleDataCell.updateItem(ScheduleDataCell.java:50) at fxproject/application.ScheduleDataCell.updateItem(ScheduleDataCell.java:1) at javafx.controls/javafx.scene.control.TableCell.updateItem(TableCell.java:665) at javafx.controls/javafx.scene.control.TableCell.indexChanged(TableCell.java:469) at javafx.controls/javafx.scene.control.IndexedCell.updateIndex(IndexedCell.java:120) at javafx.controls/javafx.scene.control.skin.TableRowSkinBase.updateCells(TableRowSkinBase.java:539) at javafx.controls/javafx.scene.control.skin.TableRowSkinBase.<init>(TableRowSkinBase.java:159) at javafx.controls/javafx.scene.control.skin.TableRowSkin.<init>(TableRowSkin.java:89) at javafx.controls/javafx.scene.control.TableRow.createDefaultSkin(TableRow.java:213) at javafx.controls/javafx.scene.control.Control.doProcessCSS(Control.java:897) at javafx.controls/javafx.scene.control.Control.access$000(Control.java:83) at javafx.controls/javafx.scene.control.Control$1.doProcessCSS(Control.java:89) at javafx.controls/com.sun.javafx.scene.control.ControlHelper.processCSSImpl(ControlHelper.java:67) at javafx.graphics/com.sun.javafx.scene.NodeHelper.processCSS(NodeHelper.java:145) at javafx.graphics/javafx.scene.Node.processCSS(Node.java:9529) at javafx.graphics/javafx.scene.Node.applyCss(Node.java:9616) at javafx.controls/javafx.scene.control.skin.VirtualFlow.setCellIndex(VirtualFlow.java:1715) at javafx.controls/javafx.scene.control.skin.VirtualFlow.getCell(VirtualFlow.java:1692) at javafx.controls/javafx.scene.control.skin.VirtualFlow.getCellLength(VirtualFlow.java:1801) at javafx.controls/javafx.scene.control.skin.VirtualFlow.computeViewportOffset(VirtualFlow.java:2639) at javafx.controls/javafx.scene.control.skin.VirtualFlow.layoutChildren(VirtualFlow.java:1245) at javafx.graphics/javafx.scene.Parent.layout(Parent.java:1204) at javafx.graphics/javafx.scene.Parent.layout(Parent.java:1211) at javafx.graphics/javafx.scene.Parent.layout(Parent.java:1211) at javafx.graphics/javafx.scene.Scene.doLayoutPass(Scene.java:576) at javafx.graphics/javafx.scene.Scene.preferredSize(Scene.java:1748) at javafx.graphics/javafx.scene.Scene$2.preferredSize(Scene.java:393) at javafx.graphics/com.sun.javafx.scene.SceneHelper.preferredSize(SceneHelper.java:66) at javafx.graphics/javafx.stage.Window$12.invalidated(Window.java:1086) at javafx.base/javafx.beans.property.BooleanPropertyBase.markInvalid(BooleanPropertyBase.java:110) at javafx.base/javafx.beans.property.BooleanPropertyBase.set(BooleanPropertyBase.java:145) at javafx.graphics/javafx.stage.Window.setShowing(Window.java:1174) at javafx.graphics/javafx.stage.Window.show(Window.java:1189) at javafx.graphics/javafx.stage.Stage.show(Stage.java:273) at fxproject/application.SceduleMain.start(SceduleMain.java:18) at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:846) at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:455) at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428) at java.base/java.security.AccessController.doPrivileged(Native Method) at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427) at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96) at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method) at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174) at java.base/java.lang.Thread.run(Thread.java:834)

該当のソースコード

ScheduleMain

1package application; 2 3import javafx.application.Application; 4import javafx.fxml.FXMLLoader; 5import javafx.scene.Scene; 6import javafx.scene.layout.VBox; 7import javafx.stage.Stage; 8 9 10public class SceduleMain extends Application { 11 @Override 12 public void start(Stage primaryStage) { 13 try { 14 VBox root = (VBox)FXMLLoader.load(getClass().getResource("ScheduleIndividual.fxml")); 15 Scene scene = new Scene(root,100,600); 16 scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm()); 17 primaryStage.setScene(scene); 18 primaryStage.show(); 19 } catch(Exception e) { 20 e.printStackTrace(); 21 } 22 } 23 24 public static void main(String[] args) { 25 launch(args); 26 } 27} 28

ScheduleData

1package application; 2 3import java.time.LocalTime; 4 5import javafx.beans.property.SimpleStringProperty; 6import javafx.beans.property.StringProperty; 7 8/** 9 * ブックマークを表すモデル. 10 */ 11public class ScheduleData { 12 private StringProperty title = new SimpleStringProperty(); 13 private StringProperty time = new SimpleStringProperty(); 14 private StringProperty detail = new SimpleStringProperty(); 15 16 /** 17 * コンストラクタ. 18 * @param title スケジュールのタイトル 19 * @param startTime 開始時間 20 * @param finishTime 終了時刻 21 * @param detail 詳細 22 */ 23 public ScheduleData(String title,LocalTime startTime, LocalTime finishTime, String detail) { 24 this.title.set(title); 25 String time = startTime+"~"+finishTime; 26 this.time.set(time); 27 this.detail.set(detail); 28 } 29 30 public StringProperty titleProperty() { 31 return title; 32 } 33 34 public StringProperty timeProperty() { 35 return time; 36 } 37 38 public StringProperty detailProperty() { 39 return detail; 40 } 41}

ScheduleDataCell

1package application; 2 3import javafx.scene.control.TableCell; 4import javafx.scene.layout.Priority; 5import javafx.scene.layout.VBox; 6import javafx.scene.text.Font; 7import javafx.scene.text.Text; 8 9public class ScheduleDataCell extends TableCell<String,ScheduleData> { 10 private VBox cellContainer; 11 private Text scheduleTitle; 12 private Text time; 13 private Text txtDetail; 14 private boolean bound = false; 15 16 public ScheduleDataCell() { 17 initComponent(); 18 initStyle();//省略しました。 19 } 20 21 22 23 24 25 @Override 26 public void updateItem(ScheduleData scheduleData, boolean empty) { 27 super.updateItem(scheduleData, empty); 28 29 30 31 32 if (empty) { 33 setText(null); 34 setGraphic(null); 35 } else { // タイトルやら詳細やらを設定 36 scheduleTitle.setText(scheduleData.titleProperty().get()); 37 time.setText(scheduleData.timeProperty().get());//→50行目 38 txtDetail.setText(scheduleData.detailProperty().get()); 39 setGraphic(cellContainer); 40 } 41 } 42} 43

Controller

1package application; 2 3/** 4 * Sample Skeleton for 'ScheduleIndividual.fxml' Controller Class 5 */ 6 7import java.net.URL; 8import java.time.LocalTime; 9import java.util.ResourceBundle; 10 11import javafx.collections.FXCollections; 12import javafx.collections.ObservableList; 13import javafx.fxml.FXML; 14import javafx.fxml.Initializable; 15import javafx.scene.control.TableCell; 16import javafx.scene.control.TableColumn; 17import javafx.scene.control.TableView; 18import javafx.util.Callback; 19 20public class SceduleTableController implements Initializable { 21 @FXML // ResourceBundle that was given to the FXMLLoader 22 private ResourceBundle resources; 23 24 @FXML // URL location of the FXML file that was given to the FXMLLoader 25 private URL location; 26 27 @FXML // fx:id="table" 28 private TableView<ScheduleData> table; // Value injected by FXMLLoader 29 30 @FXML // fx:id="scheduleColumn" 31 private TableColumn<String,ScheduleData> scheduleColumn; // Value injected by FXMLLoader 32 33 @FXML // fx:id="timeColumn" 34 private TableColumn<?, ?> timeColumn; // Value injected by FXMLLoader 35 36 37 @Override 38 public void initialize(URL location, ResourceBundle resources) { 39 scheduleColumn.setCellFactory(new Callback<TableColumn<String,ScheduleData>, TableCell<String,ScheduleData>>() { // (1) 40 @Override 41 public TableCell<String,ScheduleData> call(TableColumn<String,ScheduleData> tableColumn) { 42 return new ScheduleDataCell(); 43 } 44 }); 45 ObservableList<ScheduleData> scheduleDatas = createScheduleDatas(); 46 table.setItems(scheduleDatas); 47 48 assert scheduleColumn != null : "fx:id=\"sheduleColumn\" was not injected: check your FXML file 'ScheduleIndividual.fxml'."; 49 assert timeColumn != null : "fx:id=\"timColumn\" was not injected: check your FXML file 'ScheduleIndividual.fxml'."; 50 51 } 52 53 private ObservableList<ScheduleData> createScheduleDatas(){ 54 ObservableList<ScheduleData> scheduleDatas = FXCollections.observableArrayList(); 55 56 ScheduleData data1 = new ScheduleData("部活",LocalTime.of(12,30),LocalTime.of(16,0),"試合"); 57 58 scheduleDatas.add(data1); 59 60 return scheduleDatas; 61 } 62} 63

試したこと

・scheduleData.titleProperty().get()がNullではないか→Nullではなかった。
・setItemsの第二引数がfaulseになっていないか確認。trueだった。

補足情報(FW/ツールのバージョンなど)

jdk:11

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

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

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

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

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

satokei

2021/04/13 13:36

> time.setText(scheduleData.timeProperty().get());//→50行目 time が NULL になっていないでしょうか?
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問