线程“main”中的异常java.lang.NullPointerException打印Pojo值

时间:2014-05-24 09:25:08

标签: java hibernate

以下是我的pojo课程

public class BillDetails implements java.io.Serializable {

    private Long billNo;
    private CustomerDetails customerDetails;
    private Float subTotal;
    private Float vat;
    private Float total;
    private String paymentType;
    private String status;
    private Date addDate;
    private List billProductDetailses = new ArrayList();
 //getter and setter
}
public class BillProductDetails  implements java.io.Serializable {
     private BillProductDetailsId id;
     private ProductDetails productDetails;
     private BillDetails billDetails;
     private long qty;
     private float unitPrice;
     private float sellingPrice;
     private Integer discountPercent;
    //getter and setter
}
public class ProductDetails  implements java.io.Serializable {
     private Long barcode;
     private ProductBrand productBrand;
     private Sizes sizes;
     private ProductModelDetails productModelDetails;
     private SupplierDetails supplierDetails;
     private Colors colors;
     private ProductTypes productTypes;
     private long quntity;
     private float unitPrice;
     private float sellingPrice;
     private Integer discountPercent;
     private Date addDate;
     private String status;
     private Set billProductDetailses = new HashSet(0);
//getter and setter
}

我有适合所有类的.hbm.xml文件 在这里,我正在尝试打印BillProductDetailsProductDetails的详细信息,因为我正在使用以下代码

在BillDAO.java

public Map fetchAll(int start, int pageSize) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = session.beginTransaction();
        List<BillDetails> obj = null;
        long count = 0;
        try {
            count = (Long) session.createQuery("select count(*) from BillDetails").uniqueResult();
            String hql = "from BillDetails as bd "
                    + "left join fetch bd.customerDetails as cd "
                    + "left join fetch bd.billProductDetailses as bpd "
                    + "left join fetch bpd.billDetails";
            Query query = session.createQuery(hql).setFirstResult(start).setMaxResults(pageSize);
            obj = query.list();
            tx.commit();

        } catch (HibernateException e) {
            if (tx != null) {
                e.printStackTrace();
                tx.rollback();
            }
        } finally {
            session.close();
        }
        Map data = new HashMap();
        data.put("list", obj);
        data.put("count", count);
        return data;
    }

在下面的课程中,我正在从班级

打印价值
public class BillNewAction{
     public static void main(String[] s) {
            BillDAO dao = new BillDAO();
            Map m = dao.fetchAll(0, 10);

            List<BillDetails> billList = (List<BillDetails>) m.get("list");
            for (BillDetails d : billList) {
                System.out.println("bill no "+d.getBillNo() + "  paymentType " + d.getPaymentType());
                System.out.println("name "+d.getCustomerDetails().getName() + " address " + d.getCustomerDetails().getAddress() + " dob "
                        + d.getCustomerDetails().getDob() + "  anni " + d.getCustomerDetails().getAnniversery());
                List<BillProductDetails> bpd = d.getBillProductDetailses();
                System.out.println("bpd size is " + bpd.size());
                for (BillProductDetails cd : bpd) {
       /*Line 150*/             System.out.println("qty "+cd.getQty() + " sp " + cd.getSellingPrice() + " up " + cd.getUnitPrice());//Throwing nullPointer Exception
                    System.out.println(" barcode "+cd.getProductDetails().getBarcode() + "prType " + cd.getProductDetails().getProductTypes());

                }
            }

        }
}

以上程序的输出

bill no 3  paymentType Cash
name manish address durg dob 2014-05-16  anni 2014-05-18
bpd size is 2
Exception in thread "main" java.lang.NullPointerException
    at iland.bill.BillNewAction.main(BillNewAction.java:150)
Java Result: 1

如何打印上面的值。

2 个答案:

答案 0 :(得分:0)

如果指定的行为150,则cdnull。请调试以检查列表bpd中的值。这意味着列表bpd具有null值而不是BillProductDetails个实例。因此,在迭代列表中的元素时,在NPE引用

上调用getQty()方法时会抛出null

答案 1 :(得分:0)

正如我在评论中所说的那样,你调用的cd的一些方法可能正在返回Null或cd本身为null。检查bpd是否已初始化。

问题可能来自getBillProductDetailses(),您应该确保调用cd.getQty() cd.getSellingPrice()的所有方法都不返回null。但我猜是cd是否为空而导致问题的原因。