Estou criando um projeto em Java Fx e quando vou fazer um teste em uma classe estou recebendo uma nullpointer segue as classes.
@Entity @Table(name = "Veiculo") @XmlRootElement @NamedQueries({ @NamedQuery(name = "Veiculo.findAll", query = "SELECT v FROM Veiculo v")}) public class Veiculo implements Serializable { private static final long serialVersionUID = 1L; @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Basic(optional = false) @Column(name = "id_veiculo") private Integer idVeiculo; @Basic(optional = false) @Column(name = "modelo_veiculo") private String modeloVeiculo; @Basic(optional = false) @Column(name = "marca_veiculo") private String marcaVeiculo; @Basic(optional = false) @Column(name = "ano_veiculo") private int anoVeiculo; @Basic(optional = false) @Column(name = "cor_veiculo") private String corVeiculo; @Basic(optional = false) @Column(name = "km_veiculo") private double kmVeiculo; @Basic(optional = false) @Column(name = "placa_veiculo") private String placaVeiculo; @Basic(optional = false) @Column(name = "uf_veiculo") private String ufVeiculo; @Basic(optional = false) @Column(name = "nome_dono_veiculo") private String nomeDonoVeiculo; @Basic(optional = false) @Column(name = "cpf_dono_veiculo") private String cpfDonoVeiculo; @OneToMany(cascade = CascadeType.ALL, mappedBy = "idVeiculo") private List<OrdemServico> ordemServicoList;
o DAO generico
`public abstract class GenericDAO implements Serializable {
private static final long serialVersionUID = 1L;
private static final EntityManagerFactory emf = Persistence.createEntityManagerFactory("OSMPU");
private EntityManager em;
private final Class<T> entityClass;
public void beginTransaction() {
em = emf.createEntityManager();
em.getTransaction().begin();
}
public void commit() {
em.getTransaction().commit();
}
public void rollback() {
em.getTransaction().rollback();
}
public void closeTransaction() {
em.close();
}
public void commitAndCloseTransaction() {
commit();
closeTransaction();
}
public void flush() {
em.flush();
}
public void joinTransaction() {
em = emf.createEntityManager();
em.joinTransaction();
}
public GenericDAO(Class<T> entityClass) {
this.entityClass = entityClass;
}
public void save(T entity) {
em.persist(entity);
}
protected void delete(Object id , Class<T> classe) {
T entityToBeRemoved = em.getReference(classe, id);
em.remove(entityToBeRemoved);
}
public T update(T entity) {
return em.merge(entity);
}
public T find(int entityId) {
return em.find(entityClass, entityId);
}
public T findReferenceOnly(int entityId) {
return em.getReference(entityClass, entityId);
}
public List<T> findAll() {
CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
cq.select(cq.from(entityClass));
return em.createQuery(cq).getResultList();
}
protected T findOneResult(String namedQuery , Map<String,Object> parameters){
T result = null;
try{
Query query = em.createNamedQuery(namedQuery);
if(parameters != null && !parameters.isEmpty()) {
populaQueryParameters(query , parameters);
}
result = (T) query.getSingleResult();
}catch(NoResultException e) {
System.out.println("Não tem resultados para pesquisa");
}catch(Exception e) {
System.out.println("Houve uma exceçao!");
}
return result;
}
private void populaQueryParameters(Query query , Map<String , Object> parameters) {
for(Entry<String,Object> entry : parameters.entrySet()) {
query.setParameter(entry.getKey(), entry.getValue());
}
}
}`
DAO do Veiculo
`public class VeiculoDAO extends GenericDAO{
public VeiculoDAO() {
super(Veiculo.class);
}
public void delete(Veiculo veiculo) {
super.delete(veiculo.getIdVeiculo(), Veiculo.class);
}
}`
o controller
public class VeiculoController {
private final VeiculoDAO veiculoDAO;
public VeiculoController() {
this.veiculoDAO = new VeiculoDAO();
}
public void salvar(Veiculo veiculo) {
veiculoDAO.save(veiculo);
}
public List<Veiculo> listarVeiculos() {
return veiculoDAO.findAll();
}
public void update(Veiculo veiculo) {
veiculoDAO.update(veiculo);
}
public void remover(Veiculo veiculo) {
veiculoDAO.delete(veiculo);
}
}
Persistence Unit
<?xml version="1.0" encoding="UTF-8"?> <persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"> <persistence-unit name="OSMPU" transaction-type="RESOURCE_LOCAL"> <!-- <provider>org.hibernate.ejb.HibernatePersistence</provider> --> <provider>org.hibernate.ejb.HibernatePersistence</provider> <class>br.com.osmfx.model.Fornecedor</class> <class>br.com.osmfx.model.Item</class> <class>br.com.osmfx.model.OrdemServico</class> <class>br.com.osmfx.model.Servico</class> <class>br.com.osmfx.model.Usuario</class> <class>br.com.osmfx.model.Veiculo</class> <properties> <property name="hibernate.cache.provider_class" value="org.hibernate.cache.NoCacheProvider"/> <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/osmDatabase?zeroDateTimeBehavior=convertToNull"/> <property name="javax.persistence.jdbc.user" value="root"/> <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/> <property name="javax.persistence.jdbc.password" value=""/> </properties> </persistence-unit> </persistence>
Classe Main
public class TestDriver { public static void main(String[] args) { Veiculo veiculo = new Veiculo(); veiculo.setAnoVeiculo(2006); veiculo.setCorVeiculo("Vermelho"); veiculo.setCpfDonoVeiculo("223506"); veiculo.setKmVeiculo(3408.00); veiculo.setMarcaVeiculo("Volkswagem"); veiculo.setModeloVeiculo("Hatch"); veiculo.setNomeDonoVeiculo("Eliel"); veiculo.setPlacaVeiculo("PFI2222"); veiculo.setUfVeiculo("BA"); VeiculoController controller = new VeiculoController(); try{ controller.salvar(veiculo); System.out.println("Deu tudo certo!"); }catch(Exception e) { e.printStackTrace(); } } }
error
java.lang.NullPointerException at br.com.osmfx.dao.GenericDAO.save(GenericDAO.java:69) at br.com.osmfx.controller.VeiculoController.salvar(VeiculoController.java:26) at br.com.osmfx.driver.TestDriver.main(TestDriver.java:30)