java - catch Exception with add -


this question has answer here:

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

Popular posts from this blog

javascript - jquery or ashx not working -

opencv - DataType<cv::detail::deriv_type>::depth what is it used for -

python 3.x - Mapping specific letters onto a list of words -