0

I get mentioned error when I click to add To Cart button which takes place in my .xhtml file as follow;

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

And there here is my bean for addToCart button

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));
    }
}

Fınally my EJB is;

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;
    }
}

I dont know why ı am facing with this. do you have any ideas? Thanks!!

marc_s
  • 675,133
  • 158
  • 1,253
  • 1,388
fet.atas
  • 51
  • 7
  • You asked "Why do I get a NullPointerException?" instead of "Why is variable X null?". The duplicate answers the former. Once you're able to ask "Why is variable X null?" while pointing out the exact variable, and still can't figure out the cause, feel free to ask a new question. – BalusC Apr 27 '16 at 06:47
  • Thanks For the information @BalusC. for the records ı have solved the problem. there was a complexity with calling methods. – fet.atas Apr 27 '16 at 15:33

0 Answers0