java.lang.RuntimeException: java.lang.reflect.InvocationTargetException

Posted

技术标签:

【中文标题】java.lang.RuntimeException: java.lang.reflect.InvocationTargetException【英文标题】: 【发布时间】:2014-01-16 04:27:02 【问题描述】:

我在运行以下程序时收到 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();
    

更多错误:


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

【问题讨论】:

哪一行是wakiliproject.SampleController.setSettersLose(SampleController.java:23) 使第 23 行的东西不为空。 您能发布您的SampleController 完整源代码吗? 嘿Jim Garrison。那就是我们有 newBeautifulKiwi.setKiwi(KIWITextField.getText());在 SampleController 类中,第 23 行。 刚刚添加了完整的 SampleController 类,CycDemo 【参考方案1】:

因此,由于感兴趣的行是newBeautifulKiwi.setKiwi(KIWITextField.getText());,并且考虑到newBeautifulKiwi 永远不能为空(它刚刚使用默认构造函数创建),因此KIWITextField 为空,因此无法正确注入。确保 .fxml 文件中此元素的 id 完全 匹配此字段的名称(我已将其设为私有)。

最可能的原因是使用@FXML 注释的setSettersLose。检查similar thread。

【讨论】:

以上是关于java.lang.RuntimeException: java.lang.reflect.InvocationTargetException的主要内容,如果未能解决你的问题,请参考以下文章

Android Studio 按钮导致崩溃

添加 ImageView 时应用程序崩溃?