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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21153352/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 06:57:20  来源:igfitidea点击:

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

javahibernatejavafxh2

提问by ILikeProgramming

I get a NullPointerException when I run the following program. Please note that I'm using Hibernate.

运行以下程序时出现 NullPointerException。请注意,我正在使用 Hibernate。

I can't figure out how to fix the Null Pointer error. This is the error:

我不知道如何修复空指针错误。这是错误:

Caused by: java.lang.NullPointerException
    at wakiliproject.SampleController.setSettersLose(SampleController.java:22)

The KIWI_TABLE is created in the database by this class:

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

This is how I call the NewBeautifulKiwi to persist items into the table 'KIWI_TABLE': (partial extract of the class)

这就是我调用 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();
    }

More of the error:

更多错误:

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.access00(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.access00(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.access0(WinApplication.java:17)
    at com.sun.glass.ui.win.WinApplication.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

Thank you in advance.

先感谢您。

EDIT: The SampleController Class with everything else:

编辑:带有其他所有内容的 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);
    }
}

采纳答案by aljipa

So since the line of interest is newBeautifulKiwi.setKiwi(KIWITextField.getText());and taking into account that newBeautifulKiwican never be null (it has just been created using default constructor) it's KIWITextFieldthat is null and thus not inject properly. make sure the id of this element in .fxml file exactlymatches this field's name (I'd made it private).

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

Most probable cause is setSettersLoseannotated with @FXML. Check the similar thread.

最可能的原因是setSettersLose用@FXML 注释的。检查类似的线程