Following are my pojo classes
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
}
I have appropriate .hbm.xml file for all classes Here I am trying to print details of BillProductDetails
and ProductDetails
for that i am using following code
In 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;
}
In following class I am printing values from classes
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());
}
}
}
}
Output of above program is
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
How to print values for above.
If the line specified is 150, then cd
is null
. Please debug to check the values in the list bpd
. It means that the list bpd
is having null
value instead of BillProductDetails
instance. So, while iterating the elements on the list, NPE
is thrown when getQty()
method is invoked on the null
reference