检索产品,netbeans,我得到一个空指针异常

时间:2017-01-13 07:20:23

标签: java jsf jpa derby

我正在尝试从netbeans中的derby数据库中检索产品,我在第33行得到一个空指针异常:index.java。另外我是java的新手。

下面是我的操作类的代码:

int main( void ) {
    char word[26], reverse[26];
    int length, i;

    printf("Enter word: ");
    scanf("%s", &word);   
    length=strlen(word);

    for (i=0; i<length; i++) {
        reverse[i] = word[length - i - 1];

        /* Put \0 to terminate the string */
        reverse[i + 1]='\0';
        printf("%s\n", reverse);
    }       
}

下面是Index.java类:

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

下面是我的产品实体类:

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

下面是index.xhtml:

    /*
     * 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 + " ]";
        }

    }

下面是persitance xml(JPA)单位:

    <?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>

0 个答案:

没有答案