java - catch Exception with add -
this question has answer here:
- what nullpointerexception, , how fix it? 12 answers
i wrote statement in main method exception in (guaranteeyears) put in try block.
first in main make store object , pass number 4 in parameter, in store class have array called arrproduct have length 4:
boolean flag1 = true; while (flag1) { try { system.out.println("ente guaranteeyears"); int guaranteeyears=read.nextint(); strongdevice s = new strongdevice(code,type,price,model,capacity,guaranteeyears); //the object flag1 = false; x.addproduct(s); } and add method
public boolean addproduct(product p) { if(arrproduct.length==numofproduct) return false; arrproduct[numofproduct]=p; numofproduct++; return true; } my question is: when make object , add array gives me nullpointerexception - why happened?
as far understood, line cause npe is
x.addproduct(s); most caused array arrproduct not initialized , cause npe when being accessed.
to fix that, please verify arrproduct correctly initialized @ store object creation.
Comments
Post a Comment