0

I am trying to retrieve products from derby data database in netbeans, and I get a null pointer exception in line 33 : index.java . in addition I am new to java.

below is my code for my operation class:

package project.ejbs;

import java.util.List;
import javax.ejb.Stateless;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import project.entities.Products;


@Stateless
public class Operations {

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

    public List<Products> retrieveProducts(){
        return em.createQuery("SELECT p FROM Products p").getResultList();
    }
}

below is Index.java class:

package project.web;

import java.io.Serializable;
import java.util.List;
// import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import project.ejbs.Operations;
import project.entities.Products;

@ManagedBean
@RequestScoped
public class Index implements Serializable {

    // @EJB
    private Operations operations;   

    public Index() {}

    public List<Products> getProducts(){
        return operations.retrieveProducts();
    }  
}

below is my products entity class:

    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
    package project.entities;

    import java.io.Serializable;
    import javax.persistence.Basic;
    import javax.persistence.Column;
    import javax.persistence.Entity;
    import javax.persistence.Id;
    import javax.persistence.NamedQueries;
    import javax.persistence.NamedQuery;
    import javax.persistence.Table;



    /**
     *
     * @author hp
     */
    @Entity
    @Table(name = "PRODUCTS")
    @NamedQueries({
        @NamedQuery(name = "Products.findAll", query = "SELECT p FROM Products p")})

    public class Products implements Serializable {

        private static final long serialVersionUID = 1L;
        @Id
        @Basic(optional = false)
        @Column(name = "ID")
        private Integer id;
        @Column(name = "NAME")
        private String name;
        // @Max(value=?)  @Min(value=?)//if you know range of your decimal fields consider using these annotations to enforce field validation
        @Column(name = "PRICE")
        private Double price;
        @Column(name = "DESCRIPTION")
        private String description;
        @Column(name = "QTY")
        private Integer qty;

        public Products() {
        }

        public Products(Integer id) {
            this.id = id;
            System.out.println();
        }

        public Integer getId() {
            return id;
        }

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

        public String getName() {
            return name;
        }

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

        public Double getPrice() {
            return price;
        }

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

        public String getDescription() {
            return description;
        }

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

        public Integer getQty() {
            return qty;
        }

        public void setQty(Integer qty) {
            this.qty = qty;
        }

        @Override
        public int hashCode() {
            int hash = 0;
            hash += (id != null ? id.hashCode() : 0);
            return hash;
        }

        @Override
        public boolean equals(Object object) {
            // TODO: Warning - this method won't work in the case the id fields are not set
            if (!(object instanceof Products)) {
                return false;
            }
            Products other = (Products) object;
            if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
                return false;
            }
            return true;
        }





        @Override
        public String toString() {
            return "project.entities.Products[ id=" + id + " ]";
        }

    }

below is index.xhtml:

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 
        Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml"
          xmlns:h="http://xmlns.jcp.org/jsf/html"
          xmlns:f="http://xmlns.jcp.org/jsf/core">
        <h:head>
            <title>ONLINE SHOPPING </title>
            <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
        </h:head>
        <h:body>

                <h:dataTable value="#{index.products}" var="p">

                    <h:column>
                       <f:facet name="header"/> 
                       <h:outputLink value="product.xhtml">

                           <f:param name="query" value="#{p.description}"/>
                           <h:outputText value="#{p.name}"/>
                       </h:outputLink>
                    </h:column>
                </h:dataTable>

        </h:body>
    </html>

below is persitance xml (JPA) 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="Web2PU" transaction-type="RESOURCE_LOCAL">
            <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
            <class>project.entities.Products</class>
            <exclude-unlisted-classes>false</exclude-unlisted-classes>
            <properties>
              <property name="javax.persistence.jdbc.url" value="jdbc:derby://localhost:1527/Cart"/>
              <property name="javax.persistence.jdbc.user" value="root"/>
              <property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.ClientDriver"/>
              <property name="javax.persistence.jdbc.password" value=""/>
            </properties>
          </persistence-unit>
        </persistence>
Andrei Olar
  • 2,270
  • 1
  • 15
  • 34
Neo Sono
  • 186
  • 1
  • 4
  • 18
  • accidentally I tried to post my answer and posted an empty answer, I will post it using comments one after the other... – Neo Sono Jan 13 '17 at 09:25
  • Hi Guys, thank you for lending me your time. I have managed to display from database – Neo Sono Jan 13 '17 at 09:25
  • 1
    1. I changed my method below : public List getProducts(){ EntityManagerFactory factory = Persistence.createEntityManagerFactory("Web2PU"); EntityManager manager = factory.createEntityManager(); List resultList;// = manager.createQuery("SELECT p FROM Products p").getResultList(); return manager.createQuery("SELECT p FROM Products p").getResultList(); } – Neo Sono Jan 13 '17 at 09:26
  • 1
    2. I have changed the default derby database in netbeans to mysql(php admin) instead. I hope this helps anyone who is new || old to java like myself who is new to java (or java EE). – Neo Sono Jan 13 '17 at 09:26
  • I made that "post answer" mistake because am also new to stackoverflow...I guess somebody has to start somewhere... – Neo Sono Jan 13 '17 at 09:28

0 Answers0