java - Null pointer exception, taking data form lists and arrays -
i've checked several time, can't wrong..
main class:
try { file productdata = new file("productdata.txt"); product [] consideredrange = inputfiledata .readproductdatafile(productdata); electronicsequipmentsupplier management = new electronicsequipmentsupplier(1, 12, consideredrange); file customerdata = new file("customerdata.txt"); scanner filescan = new scanner(customerdata); while(filescan.hasnext()) management.addnewcustomer(inputfiledata. readcustomerdata(filescan)); management.addnewpurchaseorder("21/01/12", "psc-1235", "kd/9767", 50); } catch(exception e) { system.out.println(e); e.printstacktrace(); }
inputfiledata class works perfectly. have created object of electronicsequipmentsupplier consideredrange of products. added customers customerslist.
here electronicsequipmentsupplier class:
public class electronicsequipmentsupplier { private int currentmonth; private int currentyear; private product [] productrange; private customerdetailslist customerslist; private purchaseorderlist currentyearlist; private purchaseorderlist lastyearlist; public electronicsequipmentsupplier(int currentmonth, int currentyear, product [] range) { this.currentmonth = currentmonth; this.currentyear = currentyear; productrange = new product[range.length]; customerslist = new customerdetailslist(); currentyearlist = new purchaseorderlist(); lastyearlist = new purchaseorderlist(); } public void addnewpurchaseorder(string datestr, string customerid, string productcode, int qty) throws incorrectpurchaseorderexception { // check positive order quantity if(qty < 1) throw new incorrectpurchaseorderexception("order quantity must be" + " positive!"); // check product code in given range , product product foundproduct = null; for(int = 0; < productrange.length; i++) { if(productrange[i].getproductcode().equals(productcode)) { foundproduct = productrange[i]; break; } } if(foundproduct == null) throw new incorrectpurchaseorderexception("product code not in" + " product range!"); try { // creating orderdate object , getting appropriate discount orderdate newdate = new orderdate(datestr); int discount = customerslist.findcustomer(customerid). getdiscountrate(); // creating purchase order , adding list purchaseorder givenorder = new purchaseorder(newdate, customerid, foundproduct, qty, discount); currentyearlist.addpurchaseorder(givenorder); // updating record of purchasing customer int pricevalue = givenorder.getfullpricevalue(); customerslist.findcustomer(customerid) .updatetotalordersvalue(pricevalue); } catch(exception e) { throw new incorrectpurchaseorderexception("the problem with: " + "\n" + e); } }
it shows i've got nullpointerexception at: if(productrange[i].getproductcode().equals(productcode))
and in main class at:
management.addnewpurchaseorder("21/01/12", "psc-1235", "kd/9767", 50);
can't why, have required info..
thank you!
update 1:
added main method solve first issue:
for(int = 0; < consideredrange.length; i++) management.getproductrange()[i] = consideredrange[i];
but id of customer cannot found... that's method in customerdetailslist class, throws exception:
public customerdetails findcustomer(string givenid) throws customernotfoundexception { int = 0; boolean match = false; while(!match && < listofcustomerdetails.size()) { match = listofcustomerdetails.get(i).getcustomerid() .equals(givenid); i++; } if(!match) throw new customernotfoundexception("the provided id has not been" + " found"); else return listofcustomerdetails.get(i); }
update 2: updated .findcustomer() sma suggested
you trying initialize product in constructor like
productrange = new product[range.length];
and using like:
if(productrange[i].getproductcode().equals(productcode))
now allocated space array individual array elements i.e. products not initialized , hence nullpointerexception. resolve issue, like:
productrange[i] = new product(..);//and use
Comments
Post a Comment