JavaFX TableView从数据库空指针填充

时间:2016-02-09 15:47:38

标签: java javafx nullpointerexception tableview

我一直在使用JavaFX构建一个前端Java应用程序,它将数据带出数据库,并填充tableView。目前,程序会填充数据,但会将单元格显示为StringProperty [value:null],而不是仅显示该值。我将变量从SimpleStringProperty更改为String,这可以正确显示值。但是,如果任何单元格的值为null,则会给出nullpointerexception错误。我很高兴有空。

最后,出于安全原因,我不得不删除数据库连接信息,但请假设连接正常。 SQL查询也是如此

下图显示了我将某些类更改为String / Int的位置并正确显示了这些类,但是具有空值的列不是

Results Display

主类

package theDuplicateExtractor;

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableArray;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.scene.*;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.*;
import javafx.stage.*;

import java.sql.*;
import java.util.Arrays;
import java.util.Collection;

import javax.swing.table.DefaultTableModel;

@SuppressWarnings({"unused","unchecked"})
public  class TheDuplicateExtrator extends Application  {

String username;
String password;

TextArea usernamefield = new TextArea();
PasswordField passwordfield = new PasswordField();

Button blogin = new Button("Log In");
Button b1 = new Button("Butty2");

VBox layout = new VBox(20);
VBox layout1 = new VBox(20);

Stage window = new Stage();
Scene loginpage = new Scene(layout,800,800);
Scene mainpage = new Scene(layout1,1400,600);

TableView<Tabledata> mastertable = new TableView <Tabledata>(); 


String SQL = "select * from somewhere";
String SQLcount = "select count(*) from somewhere";

Connection connect;

TableColumn<Tabledata,Integer> UDPRNcol = new TableColumn<>("UDPRN");
TableColumn<Tabledata,Integer> UPRNcol = new TableColumn<>("UPRN");
TableColumn<Tabledata,Integer> authcol = new TableColumn<>("AUTH_CODE");
TableColumn<Tabledata,String> matchcol = new TableColumn<>("MATCH_DATE");
TableColumn<Tabledata,String> subbuildingnamecol = new TableColumn<>("SUB_BUILDING_NAME");
TableColumn<Tabledata,String> buildingnocol = new TableColumn<>("BUILDING_NUMBER");
TableColumn<Tabledata,String> buildingnamecol = new TableColumn<>("BUILDING_NAME");
TableColumn<Tabledata,String> streetnocol = new TableColumn<>("STREET_NUMBER");
TableColumn<Tabledata,String> streetnamecol = new TableColumn<>("STREET_NAME");
TableColumn<Tabledata,String> posttowncol = new TableColumn<>("POST_TOWN");
TableColumn<Tabledata,String> postcodecol = new TableColumn<>("POSTCODE");
TableColumn<Tabledata,String> organisationcol = new TableColumn<>("ORGANISATION_NAME");
TableColumn<Tabledata,Boolean> invalidcol = new TableColumn<>("INVALID_FG");
TableColumn<Tabledata,String> outcomecol = new TableColumn<>("OUTCOME");
TableColumn<Tabledata,String> suboutcomecol = new TableColumn<>("SUBOUTCOME");

public void start (Stage primaryStage) throws Exception{
    window = primaryStage;
    primaryStage.setTitle("The Duplicate Extractor");
    passwordfield.setMaxWidth(100);
    usernamefield.setPromptText("Enter Username");
    usernamefield.setMaxWidth(100);
    usernamefield.setMaxHeight(5);

    layout.getChildren().addAll(usernamefield,passwordfield,blogin);
    layout1.getChildren().addAll(b1);

    mastertable.setEditable(true);  


        blogin.setOnAction(e -> {
            try {
                setLogin();
                connection();
            } catch (Exception e1) {
                e1.printStackTrace();
            }
        });
        b1.setOnAction(e ->     {
            try {
                getTabledata();
            } catch (Exception e1) {
            e1.printStackTrace();
            }
        });

    primaryStage.setScene(loginpage);
    primaryStage.show();

}

public void connection () throws ClassNotFoundException{
    Class.forName();

        try{
    connect = DriverManager.getConnection();
        }
        catch (SQLException e1){
            window.setScene(loginpage);
            layout.getChildren().add(new Label("Connection Failed, Please try again"));
        }
}

public void closeconnection() throws SQLException{
    connect.close();
}

public void setLogin() throws ClassNotFoundException, SQLException{
    window.setScene(mainpage);
    this.password= passwordfield.getText();
    this.username= usernamefield.getText();
}

public ObservableList<Tabledata> getTabledata() throws SQLException{
    System.out.println("SQL Running");
    Statement stmt=connect.createStatement();
    ResultSet rs = stmt.executeQuery(SQL);
    System.out.println("SQL finished");
    ObservableList<Tabledata> tabledata = FXCollections.observableArrayList();
    UDPRNcol.setCellValueFactory(new PropertyValueFactory<Tabledata,Integer>("UDPRN"));
    UPRNcol.setCellValueFactory(new PropertyValueFactory<Tabledata,Integer>("UPRN"));
    authcol.setCellValueFactory(new PropertyValueFactory<Tabledata,Integer>("AUTH_CODE"));
    matchcol.setCellValueFactory(new PropertyValueFactory<Tabledata,String>("MATCH_DATE"));
    subbuildingnamecol.setCellValueFactory(new PropertyValueFactory<Tabledata,String>("SUB_BUILDING_NAME"));
    buildingnocol.setCellValueFactory(new PropertyValueFactory<Tabledata,String>("BUILDING_NUMBER"));
    buildingnamecol.setCellValueFactory(new PropertyValueFactory<Tabledata,String>("BUILDING_NAME"));
    streetnocol.setCellValueFactory(new PropertyValueFactory<Tabledata,String>("STREET_NUMBER"));
    streetnamecol.setCellValueFactory(new PropertyValueFactory<Tabledata,String>("STREET_NAME"));
    posttowncol.setCellValueFactory(new PropertyValueFactory<Tabledata,String>("POST_TOWN"));
    postcodecol.setCellValueFactory(new PropertyValueFactory<Tabledata,String>("POSTCODE"));
    organisationcol.setCellValueFactory(new PropertyValueFactory<Tabledata,String>("ORGANISATION_NAME"));
    outcomecol.setCellValueFactory(new PropertyValueFactory<Tabledata,String>("OUTCOME"));
    System.out.println("While loop starting");
    while(rs.next()){
        tabledata.add(new Tabledata(
                rs.getLong("UDPRN"),
                rs.getLong("UPRN"),
                rs.getString("AUTH_CODE"),
                rs.getString("MATCH_DATE"),
                rs.getString("SUB_BUILDING_NAME"),
                rs.getString("BUILDING_NUMBER"),
                rs.getString("BUILDING_NAME"),
                rs.getString("STREET_NUMBER"),
                rs.getString("STREET_NAME"),
                rs.getString("POST_TOWN"),
                rs.getString("POSTCODE"),
                rs.getString("ORGANISATION_NAME"),
                rs.getString("OUTCOME")
                ));
    }
    System.out.println("While loop complete");
    mastertable.getItems().addAll(tabledata);
    mastertable.getColumns().addAll(UDPRNcol,UPRNcol,authcol,matchcol,subbuildingnamecol,buildingnocol,buildingnamecol,streetnocol,streetnamecol,posttowncol,postcodecol,organisationcol,invalidcol,outcomecol,suboutcomecol);
    layout1.getChildren().add(mastertable);
    return tabledata;
}


public int rowCounter() throws SQLException{
    Statement stmt=connect.createStatement();
    ResultSet rowcount = stmt.executeQuery(SQLcount);
    rowcount.next();
    int rowcounter = rowcount.getInt(1);
    return rowcounter;
}

public static void main(String[] args) {
    launch(args);
}

}

Tabledata Class

package theDuplicateExtractor;

import javafx.beans.property.*;

public class Tabledata {
private long UDPRN;
private long UPRN;
private int AUTH_CODE;
private String MATCH_DATE;
private SimpleStringProperty SUB_BUILDING_NAME;
private SimpleStringProperty BUILDING_NUMBER;
private SimpleStringProperty BUILDING_NAME;
private SimpleStringProperty STREET_NUMBER;
private SimpleStringProperty STREET_NAME;
private SimpleStringProperty POST_TOWN;
private SimpleStringProperty POSTCODE;
private SimpleStringProperty ORGANISATION_NAME;
private SimpleBooleanProperty INVALID_FG;
private SimpleStringProperty OUTCOME;
private SimpleStringProperty SUBOUTCOME;

public Tabledata(long UDPRN, long UPRN, String AUTH_CODE, String MATCH_DATE, String SUB_BUILDING_NAME,
        String BUILDING_NUMBER,String BUILDING_NAME,String STREET_NUMBER,String STREET_NAME,String POST_TOWN,
        String POSTCODE,String ORGANISATION_NAME, String OUTCOME){

    this.UDPRN = new Long(UDPRN);
    this.UPRN = new Long(UPRN);
    this.AUTH_CODE = new Integer(AUTH_CODE);
    this.MATCH_DATE = new String(MATCH_DATE);
    this.SUB_BUILDING_NAME = new SimpleStringProperty(SUB_BUILDING_NAME);
    this.BUILDING_NUMBER = new SimpleStringProperty(BUILDING_NUMBER);
    this.BUILDING_NAME = new SimpleStringProperty(BUILDING_NAME);
    this.STREET_NUMBER = new SimpleStringProperty(STREET_NUMBER);
    this.STREET_NAME = new SimpleStringProperty(STREET_NAME);
    this.POST_TOWN = new SimpleStringProperty(POST_TOWN);
    this.POSTCODE = new SimpleStringProperty(POSTCODE);
    this.ORGANISATION_NAME = new SimpleStringProperty(ORGANISATION_NAME);
    //this.INVALID_FG = new SimpleBooleanProperty(INVALID_FG);
    this.OUTCOME = new SimpleStringProperty(OUTCOME);
    //this.SUBOUTCOME = new SimpleStringProperty(SUBOUTCOME);
}

public long getUDPRN() {
    return UDPRN;
}
public long getUPRN() {
    return UPRN;
}
public int getAUTH_CODE() {
    return AUTH_CODE;
}
public String getMATCH_DATE() {
    return MATCH_DATE;
}
public SimpleStringProperty getSUB_BUILDING_NAME() {
    return SUB_BUILDING_NAME;
}
public SimpleStringProperty getBUILDING_NUMBER() {
    return BUILDING_NUMBER;
}
public SimpleStringProperty getBUILDING_NAME() {
    return BUILDING_NAME;
}
public SimpleStringProperty getSTREET_NUMBER() {
    return STREET_NUMBER;
}
public SimpleStringProperty getSTREET_NAME() {
    return STREET_NAME;
}
public SimpleStringProperty getPOST_TOWN() {
    return POST_TOWN;
}
public SimpleStringProperty getPOSTCODE() {
    return POSTCODE;
}
public SimpleStringProperty getORGANISATION_NAME() {
    return ORGANISATION_NAME;
}
public SimpleBooleanProperty getINVALID_FG() {
    return INVALID_FG;
}
public SimpleStringProperty getOUTCOME() {
    return OUTCOME;
}
public SimpleStringProperty getSUBOUTCOME() {
    return SUBOUTCOME;
}
public void setUDPRN(long UDPRN) {
    this.UDPRN = UDPRN;
}
public void setUPRN(long UPRN) {
    this.UPRN = UPRN;
}
public void setAUTH_CODE(int AUTH_CODE) {
    this.AUTH_CODE = AUTH_CODE;
}
public void setMATCH_DATE(String MATCH_DATE) {
    this.MATCH_DATE = MATCH_DATE;
}
public void setSUB_BUILDING_NAME(SimpleStringProperty SUB_BUILDING_NAME) {
    this.SUB_BUILDING_NAME = SUB_BUILDING_NAME;
}
public void setBUILDING_NUMBER(SimpleStringProperty BUILDING_NUMBER) {
    this.BUILDING_NUMBER = BUILDING_NUMBER;
}
public void setBUILDING_NAME(SimpleStringProperty BUILDING_NAME) {
    this.BUILDING_NAME = BUILDING_NAME;
}
public void setSTREET_NUMBER(SimpleStringProperty STREET_NUMBER) {
    this.STREET_NUMBER = STREET_NUMBER;
}
public void setSTREET_NAME(SimpleStringProperty STREET_NAME) {
    this.STREET_NAME = STREET_NAME;
}
public void setPOST_TOWN(SimpleStringProperty POST_TOWN) {
    this.POST_TOWN = POST_TOWN;
}
public void setPOSTCODE(SimpleStringProperty POSTCODE) {
    this.POSTCODE = POSTCODE;
}
public void setORGANISATION_NAME(SimpleStringProperty ORGANISATION_NAME) {
    this.ORGANISATION_NAME = ORGANISATION_NAME;
}
public void setINVALID_FG(SimpleBooleanProperty INVALID_FG) {
    this.INVALID_FG = INVALID_FG;
}
public void setOUTCOME(SimpleStringProperty OUTCOME) {
    this.OUTCOME = OUTCOME;
}
public void setSUBOUTCOME(SimpleStringProperty SUBOUTCOME) {
    this.SUBOUTCOME = SUBOUTCOME;
}



}

堆栈跟踪

java.lang.NullPointerException
at java.lang.String.<init>(String.java:152)
at theDuplicateExtractor.Tabledata.<init>(Tabledata.java:30)
at     theDuplicateExtractor.TheDuplicateExtrator.getTabledata(TheDuplicateExtrator.jav    a:156)
at theDuplicateExtractor.TheDuplicateExtrator.lambda$1(TheDuplicateExtrator.java:89)
at theDuplicateExtractor.TheDuplicateExtrator$$Lambda$100/12048934.handle(Unknown Source)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Node.fireEvent(Node.java:8216)
at javafx.scene.control.Button.fire(Button.java:185)
at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:96)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89)
at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3724)
at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3452)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1728)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2461)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:348)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:273)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:382)
at com.sun.glass.ui.View.handleMouseEvent(View.java:553)
at com.sun.glass.ui.View.notifyMouse(View.java:925)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$141(WinApplication.java:102)
at com.sun.glass.ui.win.WinApplication$$Lambda$37/2075313.run(Unknown Source)
at java.lang.Thread.run(Thread.java:745)

1 个答案:

答案 0 :(得分:0)

为什么this.MATCH_DATE = new String(MATCH_DATE)而不仅仅是this.MATCH_DATE = MATCH_DATE;? (我假设这是抛出异常的那一行。) - James_D

这解决了这个问题 - 感谢James_D