I get a NullPointerException when I run the following program. Please note that I'm using 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:
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)
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.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
Thank you in advance.
EDIT: The SampleController Class with everything else:
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);
}
}
So since the line of interest is newBeautifulKiwi.setKiwi(KIWITextField.getText());
and taking into account that newBeautifulKiwi
can never be null (it has just been created using default constructor) it's KIWITextField
that is null and thus not inject properly. make sure the id of this element in .fxml file exactly matches this field's name (I'd made it private).
Most probable cause is setSettersLose
annotated with @FXML. Check the similar thread.