警告:StandardWrapperValve [Faces Servlet]:servlet的Servlet.service()Faces Servlet抛出异常java.lang.NullPointerException

时间:2016-04-26 22:18:38

标签: java jsf nullpointerexception

当我点击添加到我的.xhtml文件中的购物车按钮时,我提到了错误,如下所示;

<h:commandButton value="Add to CART" action="#{product.addToCart()}">
          <f:ajax execute="@form" render=":cartSize" />
</h:commandButton>

这里有我的bean for addToCart按钮

package WEB;

import ejb.operations;
import entities.Products;
import java.io.IOException;
import java.io.Serializable;
import javax.ejb.EJB;
import javax.inject.Named;
import javax.enterprise.context.RequestScoped;
import javax.faces.bean.ManagedProperty;
import javax.faces.context.FacesContext;

/**
 *
 * @author fet
 */
@Named(value = "product")
@RequestScoped

public class product  implements Serializable {

    @EJB
    private operations operations;

    @ManagedProperty(value="#(cart)")
    cart myCart;

    public cart getMyCart() {
        return myCart;
    }

    public void setMyCart(cart myCart) {
        this.myCart = myCart;
    }


    public product() {
    }


    public String getQuery() {

        return FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("query");


    }

    public void checkIfQueryExists() throws IOException{

    if(operations.checkIfQueryExists(getQuery())==0){

      FacesContext.getCurrentInstance().getExternalContext().redirect("error.xhtml");

    }


}


    public Products getProduct(){

        return operations.returnProduct(getQuery());


    }


    public void addToCart(){


        String query = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("query");

        myCart.add(operations.returnProduct(query));
    }
}

我的EJB就是这样;

package ejb;

import entities.Products;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;


/**
 *
 * @author fet
 */
@Stateless
public class operations {

    @PersistenceContext(unitName = "registerPU")
    private EntityManager em;


    public List<Products> retrieveProducts() {

        return  em.createQuery("SELECT p FROM Products p").getResultList();
    }


    public int checkIfQueryExists(String query){
        List<Products> products= em.createQuery("SELECT p FROM Products p WHERE p.productName = :productName").setParameter("productName", query).getResultList();

        return products.size();


                }

    public Products returnProduct(String query){

        Products product = (Products)em.createQuery("SELECT p FROM Products p WHERE p.productName = :productName").setParameter("productName", query).getSingleResult();

        return product;
    }
}

我不知道为什么我正面对这个。你有什么想法?谢谢!

0 个答案:

没有答案