从Sqlite DB填充TableView会抛出NullPointerException

时间:2017-06-28 14:54:36

标签: java sqlite javafx

我想绘制测量温度的数据。数据存储在Sqlite数据库中。

我现在遇到的问题是,我无法以某种方式将数据从数据库添加到TableView。关于类似问题的大多数其他主题对我没有帮助,他们也主要使用我没有使用的FXML。

编辑我的问题不在于NullPointerException是什么!我猜TableView存在问题,因为在加载数据时似乎没有初始化但我无法找到解决方案。在设置View之后加载数据也不起作用。

EDIT2 添加了更新后的代码,但仍无效。它在控制器类中此时失败:

view.tableView.setItems(allData);

EDIT3 终于解决了。我将以下内容添加到主类的start()方法中:

TempViewerWindow view = new TempViewerWindow();
ReadData controller = new ReadData(view);
controller.selectAll();
mainStage.setScene(new Scene(view));

以下是异常消息:

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

这是我的主要课程:

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);

    }

}

这是我的View类:

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);

    }

}

这是我的Model类:

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);
        }

    }

最后我的控制器:

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);
        }
    }

}

0 个答案:

没有答案