-2

when I try to save a user session, I get an exception. What is the problem?

Exception in thread "JavaFX Application Thread" java.lang.NullPointerException
at controllers.SignUpController.lambda$initialize$0(SignUpController.java:36)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Node.fireEvent(Node.java:8411)
at javafx.scene.control.Button.fire(Button.java:185)
at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:96)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89)
at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3757)
at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:394)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$2(GlassViewEventHandler.java:432)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:410)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:431)
at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
at com.sun.glass.ui.View.notifyMouse(View.java:937)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$3(WinApplication.java:177)
at java.lang.Thread.run(Thread.java:748)

SignUpController class:

import connection.HibernateSessionFactory;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
import org.hibernate.Session;
import tables.User;

import java.io.IOException;

public class SignUpController {
    public TextField username;
    public PasswordField password;
    public Button back;
    public TextField name;
    public TextField surname;
    public Button signUp;

    @FXML
    void initialize() {
        signUp.setOnAction(event -> {
            String usernameField = username.getText();
            String passwordField = password.getText();
            String nameField = name.getText();
            String surnameField = surname.getText();


            Session session = HibernateSessionFactory.getSessionFactory().openSession();
            User user = User.getInstance();
            user.setUsername(usernameField);
            user.setPassword(passwordField);
            user.setName(nameField);
            user.setSurname(surnameField);
            session.save(user);

            session.close();

//            User user = new User(usernameField, passwordField, nameField, surnameField);
//            Clicks clicks = new Clicks(0);
//
//            Session session = HibernateSessionFactory.getSessionFactory().openSession();
//            Transaction transaction = session.beginTransaction();
//            clicks.setUser(user);
//            user.addClicks(clicks);
//            session.save(user);
//            transaction.commit();
//            session.close();

        });

        back.setOnAction(event -> {
            back.getScene().getWindow().hide();
            FXMLLoader loader = new FXMLLoader();
            loader.setLocation(getClass().getResource("/fxml/login.fxml"));
            try {
                loader.load();
            } catch (IOException e) {
                e.printStackTrace();
            }

            Parent root = loader.getRoot();
            Stage stage = new Stage();
            stage.setScene(new Scene(root));
            stage.show();
        });
    }
}

My HibernateSessionFactory class :

import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import tables.Clicks;
import tables.User;

public class HibernateSessionFactory {
    private static SessionFactory sessionFactory;


    public static SessionFactory getSessionFactory() {
        if (sessionFactory == null) try {
            Configuration configuration = new Configuration().configure();
            configuration.addAnnotatedClass(User.class);
            configuration.addAnnotatedClass(Clicks.class);
            StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties());
            sessionFactory = configuration.buildSessionFactory(builder.build());
        } catch (Exception e) {
            System.out.println(e);
        }
        return sessionFactory;
    }
}

User class:

import javax.persistence.*;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

@Entity
@Table(name = "users")
public class User {
    private static User instance;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    public int id;
    @Column(name = "username")
    private String username;
    @Column(name = "password")
    private String password;
    @Column(name = "name")
    private String name;
    @Column(name = "surname")
    private String surname;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "user")
    private List<Clicks> clicks;

    public User() {
    }

    private User(int id) {
    }

    public User(String username, String password, String name, String surname) {
        this.username = username;
        this.password = password;
        this.name = name;
        this.surname = surname;
        clicks = new ArrayList<>();
    }

    public static User getInstance() {

        if (instance == null) {
            new User();
        }
        return instance;
    }


    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSurname() {
        return surname;
    }

    public void setSurname(String surname) {
        this.surname = surname;
    }

    public void addClicks(Clicks click) {
        click.setUser(this);
        clicks.add(click);

    }

    public List<Clicks> getClicks() {
        return clicks;
    }

    public void setClicks(List<Clicks> clicks) {
        this.clicks = clicks;
    }
}
VGR
  • 40,506
  • 4
  • 48
  • 63
danly
  • 1
  • 1

1 Answers1

0

The problem doesn't is Hibernate (at least not for this exception) but is the your class controller, so

You have a null pointer inside the class SignUpController because you committed two error, so, you have the propriety inside the class all nulled because you didn't insert the annotation @FXML on the propriety, so you change your declaration propriety with this

@FXML
public TextField username;
@FXML
public PasswordField password;
@FXML
public Button back;
@FXML
public TextField name;
@FXML
public TextField surname;
@FXML
public Button signUp;

Also inside the initialize you can initialized propriety and not the action, with JavaFx is possible to use annotation for marker the method such as an action, you can use @FXML on your method.

an example, if you have the method

@FXML
public void print(ActionEvent a){//op}

for work the @FXML annotation, you need the information inside the file FXML for creating GUI, an example, for the previous information

<JFXButton fx:id="singUp" onAction="#print" />
<JFXTextField fx:id="username" />

if you are new in the JavaFX you can see the my simple application here.


Hibernate Errors

In this code, you do an open session with Hibernate but not run the query and your instance of the User is null

Session session = HibernateSessionFactory.getSessionFactory().openSession();
            User user = User.getInstance();

for get the User to the DB, you must use the hibernate session, an example

Session session = HibernateSessionFactory.getSessionFactory().openSession();
 User user = HibernateSessionFactory.getSessionFactory().get(User.class, USER_ID);

if you don't have the id, you can use the Creteria if you use the Hibernate 4 or if you use the Hibernate 5 look this reference


An my opinion on this code

public class User {
    private static User instance;
}

I think with hibernate not have more sense the Singleton Pattern

vincenzopalazzo
  • 1,487
  • 2
  • 7
  • 35