Cannot invoke "jakarta.persistence.EntityManagerFactory.createEntityManager()" "this.emf" is null

I mapped my classes and transformed it in tables for my database. When I try to create a register I get this error. How I should to initialize emf?
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "jakarta.persistence.EntityManagerFactory.createEntityManager()" because "this.emf" is null
at org.persistence.RolJpaController.getEntityManager(RolJpaController.java:30)
at org.persistence.RolJpaController.create(RolJpaController.java:36)
at org.persistence.PersistenceController.createRol(PersistenceController.java:29)
at org.logic.LogicController.createRol(LogicController.java:14)
at org.logic.Main.main(Main.java:16)
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "jakarta.persistence.EntityManagerFactory.createEntityManager()" because "this.emf" is null
at org.persistence.RolJpaController.getEntityManager(RolJpaController.java:30)
at org.persistence.RolJpaController.create(RolJpaController.java:36)
at org.persistence.PersistenceController.createRol(PersistenceController.java:29)
at org.logic.LogicController.createRol(LogicController.java:14)
at org.logic.Main.main(Main.java:16)
PersistenceController
package org.persistence;

import jakarta.persistence.EntityManagerFactory;
import jakarta.persistence.Persistence;
import org.logic.Action;
import org.logic.Rol;

public class PersistenceController {

private static final String PERSISTENCE_UNIT = "IceCreamShopPU";
private static EntityManagerFactory emf;

public PersistenceController(){ emf = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT); }

ActionJpaController ActionJpa = new ActionJpaController(emf);
FlavorJpaController FlavorJpa = new FlavorJpaController(emf);
IceCreamJpaController IceCreamJpa = new IceCreamJpaController(emf);
InvoiceJpaController InvoiceJpa = new InvoiceJpaController(emf);
OrdenJpaController OrdenJpa = new OrdenJpaController(emf);
RolJpaController RolJpa = new RolJpaController(emf);
SizeJpaController SizeJpa = new SizeJpaController(emf);
UserJpaController UserJpa = new UserJpaController(emf);

public void createAction(Action act) { ActionJpa.create(act); }

public void createRol(Rol rol) { RolJpa.create(rol); }

}
package org.persistence;

import jakarta.persistence.EntityManagerFactory;
import jakarta.persistence.Persistence;
import org.logic.Action;
import org.logic.Rol;

public class PersistenceController {

private static final String PERSISTENCE_UNIT = "IceCreamShopPU";
private static EntityManagerFactory emf;

public PersistenceController(){ emf = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT); }

ActionJpaController ActionJpa = new ActionJpaController(emf);
FlavorJpaController FlavorJpa = new FlavorJpaController(emf);
IceCreamJpaController IceCreamJpa = new IceCreamJpaController(emf);
InvoiceJpaController InvoiceJpa = new InvoiceJpaController(emf);
OrdenJpaController OrdenJpa = new OrdenJpaController(emf);
RolJpaController RolJpa = new RolJpaController(emf);
SizeJpaController SizeJpa = new SizeJpaController(emf);
UserJpaController UserJpa = new UserJpaController(emf);

public void createAction(Action act) { ActionJpa.create(act); }

public void createRol(Rol rol) { RolJpa.create(rol); }

}
9 Replies
JavaBot
JavaBot4d ago
This post has been reserved for your question.
Hey @Franscis123$#! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically marked as dormant after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
Franscis123$#
Franscis123$#OP4d ago
LogicController
package org.logic;

import org.persistence.PersistenceController;

public class LogicController {

PersistenceController PersistenceCtl = new PersistenceController();

public void createAction (Action act){ PersistenceCtl.createAction(act); }

public void createRol(Rol rol){ PersistenceCtl.createRol(rol); }

}
package org.logic;

import org.persistence.PersistenceController;

public class LogicController {

PersistenceController PersistenceCtl = new PersistenceController();

public void createAction (Action act){ PersistenceCtl.createAction(act); }

public void createRol(Rol rol){ PersistenceCtl.createRol(rol); }

}
Franscis123$#
Franscis123$#OP4d ago
Main
package org.logic;

import java.util.Date;

public class Main {

public static void main (String[] args){

LogicController LogicCtl = new LogicController();

Rol role = new Rol("admin", new Date());

LogicCtl.createRol(role);

}

}
package org.logic;

import java.util.Date;

public class Main {

public static void main (String[] args){

LogicController LogicCtl = new LogicController();

Rol role = new Rol("admin", new Date());

LogicCtl.createRol(role);

}

}
Rol
package org.logic;

import jakarta.persistence.*;
import java.io.Serializable;
import java.util.Date;
import java.util.Set;

@Entity
@Table(name = "rol")
public class Rol implements Serializable {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Long id;

@Column(length = 30, unique = true, name = "rol")
private String rol;

@Temporal(TemporalType.TIMESTAMP)
@Column(name = "created_at")
private Date created_at;

@Temporal(TemporalType.TIMESTAMP)
@Column(name = "updated_at")
private Date updated_at;

@ManyToMany(mappedBy = "roles")
private Set<Action> actions;

public Rol() {}

public Rol(String rol, Date created_at) {

this.rol = rol;
this.created_at = created_at;

}

public Long getId() { return id; }

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

public String getRol() { return rol; }

public void setRole(String rol) { this.rol = rol; }

public Date getCreated_at() { return created_at; }

public void setCreated_at(Date created_at) { this.created_at = created_at; }

public Date getUpdated_at() { return updated_at; }

public void setUpdated_at(Date updated_at) { this.updated_at = updated_at; }

public Set<Action> getActions() { return actions; }

public void setActions(Set<Action> actions) { this.actions = actions; }

}
package org.logic;

import jakarta.persistence.*;
import java.io.Serializable;
import java.util.Date;
import java.util.Set;

@Entity
@Table(name = "rol")
public class Rol implements Serializable {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Long id;

@Column(length = 30, unique = true, name = "rol")
private String rol;

@Temporal(TemporalType.TIMESTAMP)
@Column(name = "created_at")
private Date created_at;

@Temporal(TemporalType.TIMESTAMP)
@Column(name = "updated_at")
private Date updated_at;

@ManyToMany(mappedBy = "roles")
private Set<Action> actions;

public Rol() {}

public Rol(String rol, Date created_at) {

this.rol = rol;
this.created_at = created_at;

}

public Long getId() { return id; }

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

public String getRol() { return rol; }

public void setRole(String rol) { this.rol = rol; }

public Date getCreated_at() { return created_at; }

public void setCreated_at(Date created_at) { this.created_at = created_at; }

public Date getUpdated_at() { return updated_at; }

public void setUpdated_at(Date updated_at) { this.updated_at = updated_at; }

public Set<Action> getActions() { return actions; }

public void setActions(Set<Action> actions) { this.actions = actions; }

}
dan1st
dan1st4d ago
What is RolJpa.create(rol)? I think you are using new somewhere where you shouldn't
Franscis123$#
Franscis123$#OP4d ago
Its calls to create method from RolJpaController create method. I'm new using JPA, and I watched a tutorials to apply it to my proyect, In its was done like that way.
dan1st
dan1st4d ago
Can you show LogicController and main? Did the tutorials you were watching use a DI framework?
Franscis123$#
Franscis123$#OP4d ago
No, Its just uses Maven, and me too
dan1st
dan1st4d ago
.
JavaBot
JavaBot4d ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.

Did you find this page helpful?