以下是我的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文件
在这里,我正在尝试打印BillProductDetails
和ProductDetails
的详细信息,因为我正在使用以下代码
在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
如何打印上面的值。
答案 0 :(得分:0)
如果指定的行为150,则cd
为null
。请调试以检查列表bpd
中的值。这意味着列表bpd
具有null
值而不是BillProductDetails
个实例。因此,在迭代列表中的元素时,在NPE
引用
getQty()
方法时会抛出null
答案 1 :(得分:0)
正如我在评论中所说的那样,你调用的cd
的一些方法可能正在返回Null或cd
本身为null。检查bpd
是否已初始化。
问题可能来自getBillProductDetailses()
,您应该确保调用cd.getQty()
cd.getSellingPrice()
的所有方法都不返回null。但我猜是cd
是否为空而导致问题的原因。