运行以下程序时出现NullPointerException。请注意我正在使用Hibernate。
我无法弄清楚如何修复空指针错误。这是错误:
Caused by: java.lang.NullPointerException
at wakiliproject.SampleController.setSettersLose(SampleController.java:22)
KIWI_TABLE由此类在数据库中创建:
import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Entity(name = "KIWI_TABLE")
public class NewBeautifulKiwi implements Serializable {
@Id
@GeneratedValue
private int KiwiId;
private String Kiwi;
public int getKiwiId() {
return KiwiId;
}
public void setKiwiId(int KiwiId) {
this.KiwiId = KiwiId;
}
public String getKiwi() {
return Kiwi;
}
public void setKiwi(String Kiwi) {
this.Kiwi = Kiwi;
}
}
这就是我调用NewBeautifulKiwi将项目持久保存到表'KIWI_TABLE'中的方法: (班级的部分摘录)
public class SampleController implements Initializable, ControlledScreen {
@FXML
TextField KIWITextField;
@FXML
public void setSettersLose () {
NewBeautifulKiwi newBeautifulKiwi = new NewBeautifulKiwi();
newBeautifulKiwi.setKiwi(KIWITextField.getText());
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
Session session = sessionFactory.openSession();
session.beginTransaction();
session.save(newBeautifulKiwi);
session.getTransaction().commit();
}
更多错误:
java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1449)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:69)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:217)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:170)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:38)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:37)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:53)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:33)
at javafx.event.Event.fireEvent(Event.java:171)
at javafx.scene.Scene$ClickGenerator.postProcess(Scene.java:3100)
at javafx.scene.Scene$ClickGenerator.access$8600(Scene.java:3038)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3320)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3151)
at javafx.scene.Scene$MouseHandler.access$1900(Scene.java:3106)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1563)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2248)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:250)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:173)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:292)
at com.sun.glass.ui.View.handleMouseEvent(View.java:530)
at com.sun.glass.ui.View.notifyMouse(View.java:924)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.access$100(WinApplication.java:17)
at com.sun.glass.ui.win.WinApplication$3$1.run(WinApplication.java:67)
at java.lang.Thread.run(Thread.java:744)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:75)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:279)
at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1446)
... 30 more
Caused by: java.lang.NullPointerException
at wakiliproject.SampleController.setSettersLose(SampleController.java:23)
... 40 more
提前谢谢。
编辑:SampleController类及其他所有内容:
import java.net.URL;
import java.util.ResourceBundle;
import javafx.application.Platform;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.TextField;
import javafx.scene.layout.Pane;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import wakiliproject.Forms.AddNew.DB.NewBeautifulKiwi;
public class SampleController implements Initializable, ControlledScreen {
@FXML
TextField KIWITextField = null;
@FXML
public void setSettersLose () {
NewBeautifulKiwi newBeautifulKiwi = new NewBeautifulKiwi();
newBeautifulKiwi.setKiwi(KIWITextField.getText());
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
Session session = sessionFactory.openSession();
session.beginTransaction();
session.save(newBeautifulKiwi);
session.getTransaction().commit();
}
ScreensController myController;
// Initializes the controller class.
@Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}
@Override
public void setScreenParent(ScreensController screenParent) {
myController = screenParent;
}
@FXML
public void windowClose() {
Platform.exit();
}
// Pages
@FXML
private void goToClients() {
myController.setScreen(WakiliProject.clientsID);
}
@FXML
private void goToMatters() {
myController.setScreen(WakiliProject.mattersID);
}
@FXML
private void goToEvents() {
myController.setScreen(WakiliProject.eventsID);
}
@FXML
private void goToFirmProfileView() {
myController.setScreen(WakiliProject.firmProfileID);
}
// Hover menus
@FXML
Pane clientAccountsHoverMenu;
@FXML
private void clientAccountsHover() {
clientAccountsHoverMenu.setVisible(true);
}
@FXML
private void clientAccountsHoverOut() {
clientAccountsHoverMenu.setVisible(false);
}
// Hidden Panes
@FXML
Pane notesHomePane;
@FXML
Pane navItems;
@FXML
Pane mainHome;
@FXML
Pane homeContentDisplay;
@FXML
private void notesHomePaneShow() {
notesHomePane.setVisible(true);
mainHome.setVisible(false);
}
@FXML
private void goToHome() {
notesHomePane.setVisible(false);
mainHome.setVisible(true);
}
@FXML
private void navItemsShow() {
navItems.setVisible(true);
homeContentDisplay.setVisible(false);
}
@FXML
private void goToHomeFronNavAll() {
navItems.setVisible(false);
homeContentDisplay.setVisible(true);
}
}
答案 0 :(得分:2)
因为感兴趣的行是newBeautifulKiwi.setKiwi(KIWITextField.getText());
并且考虑到newBeautifulKiwi
永远不能为空(它刚刚使用默认构造函数创建),它的KIWITextField
为null,因此不适当注射。确保.fxml文件 完全 中此元素的ID与此字段的名称匹配(我将其设为私有)。
最可能的原因是setSettersLose
使用@FXML注释。查看similar thread。