java - NPE with Current Thread Context Class Loader getResourceAsStream(file) -
i trying excel file resource folder. method works fine:
public jobordergenerator(list<shoporder> shoporder) throws invalidformatexception, ioexception { inputstream = thread.currentthread().getcontextclassloader().getresourceasstream("shop-order.xlsx"); createjoborder(shoporder); }
which calls
void createjoborder(list<shoporder> shoporder) throws invalidformatexception, ioexception{ (shoporder shoporder1 : shoporder) { system.out.println("inside createjoborder "+shoporder1.getpo_number()); writetospecificcell(2, 1, sheetnumber, shoporder1.getpo_number()); //po number writetospecificcell(7, 3, sheetnumber, shoporder1.getpo_number()); //part number localdate date = shoporder1.getpo_due_date(); string datetostring = date.tostring(); writetospecificcell(1, 2, sheetnumber, datetostring); //due_date writetospecificcell(7, 5, sheetnumber, integer.tostring(shoporder1.getpart_quantity())); //quantity //writetospecificcell(1,2,sheetnumber, shoporder.get); //material writetospecificcell(8, 3, sheetnumber, shoporder1.getpart_decription()); //part description //writetospecificcell(1,2,sheetnumber, shoporder.getcustomer()); //customer writetospecificcell(10, 1, sheetnumber, shoporder1.getmachine_number()); //machine sheetnumber++; } }
this culprit below. can see used println statements figure out going on, , see in console is:
inside writetospecificcell before try statement 1 0 111
right after java.lang.nullpointerexception:
therefore there going on excel file. cannot figure out what. since retrieved like:
inputstream = thread.currentthread().getcontextclassloader().getresourceasstream("shop-order.xlsx");
it should know location of file. since in resource file , not on class path (so in jar file) need file above way right?
if matters using intellji ultimate, still learning ins , outs, heavy eclipse user.
-----------update 1-----------------
inputstream = thread.currentthread().getcontextclassloader().getresourceasstream("/resources/shop-order.xlsx");
still getting same error...
--------update 2--------------------
try{ classloader classloader = getclass().getclassloader(); inputstream = this.getclass().getresourceasstream("/resources/shop-order.xlsx"); if (inputstream == null){ system.out.println("inputstream null...."); }
just confirm did:
inputstream = this.getclass().getresourceasstream("/resources/shop-order .xlsx");
just confirm there not space in shop-order.xlsx...
it confirmed inputstream null, because in console printing:
inputstream null...
from println statement
the method throwing null exception here...
void writetospecificcell(int rownumber, int cellnumber, int sheetnumber, string value) throws invalidformatexception, ioexception { system.out.println("inside writetospecificcell method before try statement"); try { system.out.println("inside writetospecificcell method inside try statement"); xssfworkbook workbook = new xssfworkbook(inputstream); xssfsheet sheet = workbook.getsheetat(sheetnumber); xssfrow row = sheet.getrow(rownumber); xssfcell cell = row.createcell(cellnumber); if (cell == null) { cell = row.createcell(cellnumber); } cell.setcelltype(cell.cell_type_string); cell.setcellvalue(value); system.out.println("inside writetospecificcell method after try statement"); workbook.close(); } catch (ioexception e) { system.out.println("error in writetospecificcell method " + e); }catch(nullpointerexception ex){ system.out.println("null in writetospecificcell method"); } }
it seems me have space before extension of file. can see if correct? :p
Comments
Post a Comment