I want to plot data i got from measuring the temperature. The data is stored in a Sqlite database.
The problem I now have is that I somehow can't add the data from the database to the TableView. The most other topics about similar problems didn't help me, they also mostly work with FXML which I'm not using.
EDIT My question is not about what a NullPointerException is! I guess there is a problem with the TableView, since it seems not to be initialized when the data is loaded but I can't find a solution for it. Also loading the data after the View is setup does not work.
EDIT2 Added the updated code, but still does not work. It fails at this point in the controller class:
view.tableView.setItems(allData);
EDIT3 Finally solved it. I added the following to the start() method in the main class:
TempViewerWindow view = new TempViewerWindow();
ReadData controller = new ReadData(view);
controller.selectAll();
mainStage.setScene(new Scene(view));
Here is the exception message:
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:483)
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:367)
at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:305)
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:483)
at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.NullPointerException
at ReadData.selectAll(ReadData.java:63)
at main.main(main.java:22)
... 11 more
Exception running application main
Here is my main class:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class main extends Application {
@Override
public void start(Stage mainStage) throws Exception {
TempViewerWindow view = new TempViewerWindow();
ReadData controller = new ReadData(view);
controller.selectAll();
mainStage.setScene(new Scene(view));
mainStage.setMinWidth(300);
mainStage.setMinHeight(300);
mainStage.setTitle("Temperature Viewer");
mainStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Here is my View class:
import javafx.scene.canvas.Canvas;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.StackPane;
public class TempViewerWindow extends BorderPane {
TableView<TempData> tableView;
public TempViewerWindow() {
tableView = new TableView<>();
TableColumn<TempData, String> col_date = new TableColumn<>("Date");
TableColumn<TempData, Float> col_temperature = new TableColumn<>("Temperature");
col_date.setCellValueFactory(e -> e.getValue()
.dateProperty());
col_temperature.setCellValueFactory(e -> e.getValue()
.temperatureProperty()
.asObject());
tableView.getColumns()
.addAll(col_date, col_temperature);
setLeft(tableView);
StackPane holder = new StackPane();
Canvas canvas = new Canvas(300, 250);
holder.getChildren()
.add(canvas);
holder.setStyle("-fx-background-color: white");
setRight(holder);
}
}
Here is my Model class:
import javafx.beans.property.*;
public class TempData {
SimpleStringProperty date;
SimpleFloatProperty temperature;
TempData(String name, Float temperature) {
this.date = new SimpleStringProperty();
this.temperature = new SimpleFloatProperty();
}
public StringProperty dateProperty() {
return date;
}
public String getDate() {
return date.get();
}
public void setDate(String date) {
this.date.set(date);
}
public FloatProperty temperatureProperty() {
return temperature;
}
public float getTemperature() {
return temperature.get();
}
public void setTemperature(Float temperature) {
this.temperature.set(temperature);
}
}
and finally my controller:
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
public class ReadData {
private TempViewerWindow view;
private Connection connect() {
// SQLite connection string
String url = "jdbc:sqlite:F://python27_workspace/TempSensor/temperature.db";
Connection conn = null;
try {
conn = DriverManager.getConnection(url);
} catch (SQLException e) {
System.out.println(e.getMessage());
}
return conn;
}
public void selectAll() {
ObservableList<TempData> allData = FXCollections.observableArrayList();
String sql = "SELECT Id, Temperature FROM data";
try (Connection conn = this.connect();
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql)) {
// loop through the result set
while (rs.next()) {
TempData data = new TempData(rs.getString("Id"), rs.getFloat("Temperature"));
if (!data.equals(null)) {
allData.add(data);
} else {
System.out.println("data not found!");
}
System.out.println(rs.getString("Id") + "\t" + rs.getDouble("Temperature"));
}
} catch (SQLException e) {
System.out.println(e.getMessage());
}
if (allData != null) {
view.tableView.setItems(allData);
}
}
}