简体   繁体   English

JavaFX Hibernate 返回 Null 指针异常

[英]JavaFX Hibernate returning Null Pointer Exception

I'm a newbie here in Stackoverflow and this is my first time putting my questions here since I always research anything about my programming basics.我是 Stackoverflow 的新手,这是我第一次在这里提出问题,因为我总是研究有关我的编程基础知识的任何东西。
The problem I am facing now is the Null Pointer Exception.我现在面临的问题是 Null 指针异常。 I am working in JavaFX Hibernate for the first time and it seems Null Pointer Exception is absolutely present with my basic practice:(我第一次在 JavaFX Hibernate 工作,似乎 Null 指针异常在我的基本实践中绝对存在:(
so here is my problem.所以这是我的问题。 . . I will provide the codes.我会提供代码。 . . hope you will get it:(希望你能得到它:(

//Model(Pojo) Code //模型(Pojo)代码

@Entity
@Table(name = "person")
public class Person implements Serializable {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", nullable = false)
private Integer id;
@Column(name = "first_name", nullable = false)
private String firstName;
@Column(name = "last_name", nullable = false)
private String lastName; //including the setters and getters here}


//Repository Code //存储库代码

@Override
public List<Person> getPerson() {
    session = sessionFactory.openSession();
    try {
        String hql = "From Person";
        //System.out.println(hql);
        return session.createQuery(hql).list();
        
    } finally {
        session.close();
    }
}


//Service Code //服务代码

public interface PersonService {
List<Person> getPerson();}


Controller Code (where the NPE Persist) Controller 代码(NPE 持续存在的地方)

 @FXML
private TableView<Person> table;
@FXML
private TableColumn<Person, Integer> id;
@FXML
private TableColumn<Person, String> firstname;
@FXML
private TableColumn<Person, String> lastname;
private final ObservableList<Person> person = FXCollections.observableArrayList();

void getAllData() {
    List<Person> personList = personService.getPerson();
    person.addAll(personList);
    id.setCellValueFactory(new PropertyValueFactory<>("id")); //returns NPE here
    firstname.setCellValueFactory(new PropertyValueFactory<>("firstName"));
    lastname.setCellValueFactory(new PropertyValueFactory<>("lastName"));
    table.setItems(person);
}


//FXML Code in package 'forms' (forms/FormX.fxml) //package 'forms' (forms/FormX.fxml) 中的 FXML 代码
<AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="controller.Controller"> <children> <AnchorPane prefHeight="400.0" prefWidth="606.0"> <children> <TextField fx:id="fname" layoutX="14.0" layoutY="61.0" prefHeight="25.0" prefWidth="200.0" promptText="Firstname" /> <TextField fx:id="lname" layoutX="14.0" layoutY="102.0" prefHeight="25.0" prefWidth="200.0" promptText="Lastname" /> <Button fx:id="btn_save" layoutX="14.0" layoutY="141.0" mnemonicParsing="false" onAction="#SaveData" text="SAVE " /> <Button layoutX="154.0" layoutY="141.0" mnemonicParsing="false" text="CANCEL" /> <TableView fx:id="table" layoutX="258.0" layoutY="27.0" prefHeight="352.0" prefWidth="324.0"> <columns> <TableColumn fx:id="id" prefWidth="75.0" text="id" /> <TableColumn fx:id="firstname" prefWidth="75.0" text="firstname" /> <TableColumn fx:id="lastname" prefWidth="75.0" text="lastname" /> </columns> </TableView> </children> </AnchorPane> </children> </AnchorPane>


Error Returns: j ava.lang.NullPointerException: Cannot invoke "javafx.scene.control.TableColumn.setCellValueFactory(javafx.util.Callback)" because "this.id" is null错误返回:j ava.lang.NullPointerException:无法调用“javafx.scene.control.TableColumn.setCellValueFactory(javafx.util.Callback)”,因为“this.id”是 null


I created a Persistence Unit (persistence.xml) the connection is good and I can connect.我创建了一个持久性单元(persistence.xml),连接良好,我可以连接。

So this is my problem, the "id" always returning NPE and I'm pointing it on the Pojo.所以这是我的问题,“id”总是返回 NPE,我将它指向 Pojo。 I don't know how to get rid of this as I research about this for how many links:( I do hope I can get an answer in this query. thanks!我不知道如何摆脱这个,因为我研究了有多少链接:(我希望我能在这个查询中得到答案。谢谢!

So Basically I solved my problem for an hour.所以基本上我解决了一个小时的问题。 lol哈哈
so I put the getAllData();所以我把getAllData(); method in my Controller in the我的 Controller 中的方法

void intialize(URL url, ResourceBundle rb){//I put it here}


it sounds weird but I realized it after a lot of research without finding an answer lol.听起来很奇怪,但经过大量研究却没有找到答案,我意识到了这一点,哈哈。 so I tried to relax and stare at my code then the idea miraculously appear.所以我试着放松并盯着我的代码然后这个想法奇迹般地出现了。 LOL.哈哈。 I tried to figure it out now what is the problem with my previous logic.我现在试图弄清楚我以前的逻辑有什么问题。 the last one I did was putting the getAllData in the main Class or the Launcher after the stage.show();我做的最后一个是将getAllData放在主Class或stage.show(); and the NPE persist here. NPE 在这里持续存在。 if anyone can explain me why cause I can't figure it out.如果有人能解释我为什么,因为我无法弄清楚。 please share your idea to me.请与我分享你的想法。 thanks谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM