PROBLEMA [Java+JSF+JSP] Aiuto con Facade e Controller !!

Basionkler

Nuovo Utente
53
9
CPU
Intel core i7 4771 3.5GHz @ 3.9GHz
Scheda Madre
Asus H87-PRO
HDD
SSD Samsung 840 PRO 256GB
RAM
G.Skill Ares 2x4GB 1600Mhz CL8
GPU
Asus GeForce GTX 780 3GB OC
Audio
Creative SoundBlaster Z
PSU
Be quiet! Straight power CM 680W 80+G
Case
CM Silencio 650 Pure
OS
Windows 8.1
Salve a tutti. Ho un problema che non riesco a risolvere.. ho cercato su internet una soluzione ma non ho trovato nulla di utile/funzionante. Il problema che mi da è un NullPointerException. Dovrebbe essere perché non fa correttamente l'inject della Facade. Ma non so dove mettere le mani.. chi può aiutarmi?

Gli strumenti che uso sono:
-Postgress & pgAdmin III
-Tomcat 8
-Eclipse

Product.Java
Codice:
package it.uniroma3.siw.model;

import javax.persistence.*;
import java.util.*;
 [MENTION=23649]Entity[/MENTION] [MENTION=39125]tab[/MENTION]le(name="tb_product") [MENTION=143227]Nam[/MENTION]edQuery(name = "findAllProducts", query = "SELECT p FROM tb_product p")
public class Product {

    @Id
    [MENTION=15713]GeNe[/MENTION]ratedValue(strategy = GenerationType.AUTO)
    private Long id;

    [MENTION=95331]Col[/MENTION]umn(nullable = false)
    private String name;

    @Lob
    [MENTION=95331]Col[/MENTION]umn(nullable = false)
    private String description;

    [MENTION=95331]Col[/MENTION]umn(nullable = false)
    private Float price;

    [MENTION=95331]Col[/MENTION]umn(nullable = false)
    private String code;

    [MENTION=58731]One[/MENTION]ToOne(mappedBy = "product")
    private OrderLine orderLine;

    [MENTION=17078]man[/MENTION]yToMany(mappedBy = "product")
    private List<Provider> providers;

    //Costruttore

    public Product() {
    }

    public Product(String name, Float price, String description, String code) {
        this.name = name;
        this.price = price;
        this.description = description;
        this.code = code;
}




    //Getter&Setter
    public Long getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public Float getPrice() {
        return price;
    }

    public void setPrice(Float price) {
        this.price = price;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public OrderLine getOrderLine() {
        return orderLine;
    }

    public void setOrderLine(OrderLine orderLine) {
        this.orderLine = orderLine;
    }

    public List<Provider> getProviders() {
        return providers;
    }

    public void setProviders(List<Provider> providers) {
        this.providers = providers;
    }

    [MENTION=75845]Override[/MENTION]
    public String toString() {
        return "[name=" + name + ", description=" + description
                + ", price=" + price + "]\n";
    }

}

-----------

ProductFacade.Java
Codice:
package it.uniroma3.siw.model.facade;

import it.uniroma3.siw.model.Product;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.criteria.CriteriaQuery;

import java.util.List;
 [MENTION=125913]Stateless[/MENTION](name="productFacade")
public class ProductFacade{
    
    @PersistenceContext(unitName = "prodFacade")
    private EntityManager em;
    
    public Product createProduct(String name, String code, Float price, String description) {
        Product product = new Product(name, price, description, code);
        em.persist(product);
        return product;
    }
    
    public Product getProduct(Long id) {
        Product product = em.find(Product.class, id);
        return product;
    }
    
    public List<Product> getAllProducts() {
        CriteriaQuery<Product> cq = em.getCriteriaBuilder().createQuery(Product.class);
        cq.select(cq.from(Product.class));
        List<Product> products = em.createQuery(cq).getResultList();
        return products;
    }

    public void updateProduct(Product product) {
        em.merge(product);
    }
    
    public void deleteProduct(Product product) {
        em.remove(product);
    }

    public void deleteProduct(Long id) {
        Product product = em.find(Product.class, id);
        deleteProduct(product);
    }
}

-------

ProductController.Java

Codice:
package it.uniroma3.siw.controller;

import it.uniroma3.siw.model.Product;
import it.uniroma3.siw.model.facade.ProductFacade;

import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
 [MENTION=240517]Mana[/MENTION]gedBean(name="productController")
public class ProductController {
    
    [MENTION=240517]Mana[/MENTION]gedProperty(value="#{param.id}")
    private Long id;
    private String name;
    private Float price;
    private String description;
    private String code;
    private Product product;
    private List<Product> products;
    
    @EJB(beanName="productFacade")
    private ProductFacade productFacade;
    
    public String createProduct() {
        this.product = productFacade.createProduct(name, code, price, description);
        return "product"; 
    }
    
    public String listProducts() {
        this.products = productFacade.getAllProducts();
        return "products"; 
    }

    public String findProduct() {
        this.product = productFacade.getProduct(id);
        return "product";
    }
    
    public String findProduct(Long id) {
        this.product = productFacade.getProduct(id);
        return "product";
    }

    
//Getter&Setter
    
    public Long getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

    public Float getPrice() {
        return price;
    }

    public void setPrice(Float price) {
        this.price = price;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public Product getProduct() {
        return product;
    }

    public void setProduct(Product product) {
        this.product = product;
    }

    public List<Product> getProducts() {
        return products;
    }

    public void setProducts(List<Product> products) {
        this.products = products;
    }

    public void setProductFacade(ProductFacade productFacade) {
        this.productFacade = productFacade;
    }
}

-------
NewProduct.xhtml

Codice:
<!DOCTYPE html>
<html     xmlns="http://www.w3.org/1999/xhtml"
        xmlns:f="http://java.sun.com/jsf/core"
        xmlns:h="http://java.sun.com/jsf/html">

<head>
<title>New Product</title>
</head>

<body>
<f:view>
<h:form>
    <div>
        <h:outputLabel value="Nome:"/>
        <h:inputText value="#{productController.name}"
            required="true"
            requiredMessage="Il nome è obligatorio" id="name">
        </h:inputText>
        <h:message for="name" styleClass="error" />
    </div>
    <div>
         <h:outputLabel value="Codice Prodotto:"/>
        <h:inputText value="#{productController.code}"
            required="true"
            requiredMessage="Il Codice è obligatorio"
            validatorMessage="Il Codice deve essere lungo tra i 6 e gli 8 caratteri"
            id="code">
            <f:validateLength minimum="6" maximum="10" />
        </h:inputText>
        <h:message for="code" styleClass="error" />
    </div>
    <div>
        <h:outputLabel value="Prezzo:"/>
        <h:inputText value="#{productController.price}"
            required="true"
            requiredMessage="Il prezzo è obligatorio"
            converterMessage="Il prezzo deve essere un numero"
            validatorMessage="il prezzo deve essere maggiore di zero" id="price">
            <f:validateDoubleRange minimum="0" />
        </h:inputText>
        <h:message for="price" styleClass="error" />
    </div>
    <div>
        <h:outputLabel value="Descrizione:"/>
        <h:inputText value="#{productController.description}" required="false">
        </h:inputText>
        <h:commandButton value="Submit"    action="#{productController.createProduct}" /> 
     </div>
        </h:form>
    </f:view>
</body>
</html>
-----


Questo è lo stacktrace
Codice:
[B]type[/B] Exception report
[B]message[/B] [U]java.lang.NullPointerException[/U]
[B]description[/B] [U]The server encountered an internal error that prevented it from fulfilling this request.[/U]
[B]exception[/B]
javax.servlet.ServletException: java.lang.NullPointerException
    javax.faces.webapp.FacesServlet.service(FacesServlet.java:659)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
[B]root cause[/B]
javax.faces.el.EvaluationException: java.lang.NullPointerException
    javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:101)
    com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
    javax.faces.component.UICommand.broadcast(UICommand.java:315)
    javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:790)
    javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1282)
    com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
    com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:198)
    javax.faces.webapp.FacesServlet.service(FacesServlet.java:646)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
[B]root cause[/B]
java.lang.NullPointerException
    it.uniroma3.siw.controller.ProductController.createProduct(ProductController.java:30)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    java.lang.reflect.Method.invoke(Unknown Source)
    org.apache.el.parser.AstValue.invoke(AstValue.java:245)
    org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:267)
    com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)
    javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:87)
    com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
    javax.faces.component.UICommand.broadcast(UICommand.java:315)
    javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:790)
    javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1282)
    com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
    com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:198)
    javax.faces.webapp.FacesServlet.service(FacesServlet.java:646)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
 

Entra

oppure Accedi utilizzando

Hot: E3 2021, chi ti è piaciuto di più?

  • Ubisoft

    Voti: 17 18.3%
  • Gearbox

    Voti: 1 1.1%
  • Xbox & Bethesda

    Voti: 66 71.0%
  • Square Enix

    Voti: 3 3.2%
  • Capcom

    Voti: 5 5.4%
  • Nintendo

    Voti: 14 15.1%
  • Altro (Specificare)

    Voti: 8 8.6%

Discussioni Simili