excel - Getting java.lang.NullPointerException while writing into workbook WorkBook.write(out) Apache POI -
i getting java.lang.nullpointerexception
while writing in output stream:
workbook.write(new fileoutputstream("test1.xslx"));
the exception is:
exception: java.lang.nullpointerexception @ org.apache.poi.poixmldocument.write(poixmldocument.java:201) @ excelcompare.writeoutputintoexcel.addrow(writeexcel.java:124) @ excelcompare.compareexcel.main(mainclassexcelcompare.java:113)
here have 2 class: compareexcel
class , writeoutputintoexcel
i want compare 2 excel sheets excel1.xslx
, excel2.xslx
, put result in result.xslx
.
i don't want put in result.xslx
, want put rows don't match in both excel1
, excel2.
here main
class
package excelcompare; import java.io.file; import java.io.fileinputstream; import java.io.filenotfoundexception; import java.io.ioexception; import java.util.iterator; import org.apache.poi.ss.usermodel.cell; import org.apache.poi.ss.usermodel.cellstyle; import org.apache.poi.ss.usermodel.indexedcolors; import org.apache.poi.ss.usermodel.row; import org.apache.poi.xssf.usermodel.xssfsheet; import org.apache.poi.xssf.usermodel.xssfworkbook; public class compareexcel { public static void main(string[] args) { try { int temp; file excel1 = new file("c://users/ckothakapax076037/desktop/demo1.xlsx"); fileinputstream fis1 = new fileinputstream(excel1); xssfworkbook book1 = new xssfworkbook(fis1); xssfsheet sheet1 = book1.getsheetat(0); file excel2 = new file("c://users/ckothakapax076037/desktop/demo2.xlsx"); fileinputstream fis2 = new fileinputstream(excel2); xssfworkbook book2 = new xssfworkbook(fis2); xssfsheet sheet2 = book2.getsheetat(0); writeexcel obj1 = new writeexcel(); obj1.setoutputfile("c://users/ckothakapax076037/desktop/result.xlsx"); //get iterator rows in current sheet iterator<row> itr1 = sheet1.iterator(); iterator<row> itr2 = sheet2.iterator(); // iterating through cells row row while (itr1.hasnext() && itr2.hasnext()) { temp = 0; int j = 0; row row1 = itr1.next(); row row2 = itr2.next(); //get iterator cells of current row iterator<cell> celliterator1 = row1.celliterator(); iterator<cell> celliterator2 = row2.celliterator(); cellstyle style = book1.createcellstyle(); style = book1.createcellstyle(); style.setfillforegroundcolor(indexedcolors.red.getindex()); style.setfillpattern(cellstyle.solid_foreground); while (celliterator1.hasnext() && celliterator2.hasnext()) { cell cell1 = celliterator1.next(); cell cell2 = celliterator2.next(); switch (cell1.getcelltype()) { case cell.cell_type_string: system.out.print(cell1.getstringcellvalue() + "\t"); system.out.print(cell2.getstringcellvalue() + "\t"); if (!cell1.getstringcellvalue().equalsignorecase(cell2.getstringcellvalue())) { temp++; cell1.setcellstyle(style); } break; case cell.cell_type_numeric: system.out.print(cell1.getnumericcellvalue() + "\t"); system.out.print(cell2.getnumericcellvalue() + "\t"); if (cell1.getnumericcellvalue() != cell2.getnumericcellvalue()) { temp++; cell1.setcellstyle(style); } break; case cell.cell_type_boolean: system.out.print(cell1.getbooleancellvalue() + "\t"); system.out.print(cell2.getbooleancellvalue() + "\t"); break; case cell.cell_type_blank: system.out.print(cell1.getnumericcellvalue() + "\t"); system.out.print(cell2.getnumericcellvalue() + "\t"); if (cell2.getstringcellvalue() != " ") { temp++; cell1.setcellstyle(style); } break; default: } j++; } system.out.print("\n"); system.out.print("flag value:" + temp); system.out.print("\n"); if (temp >= 1) { obj1.addrow(row1.celliterator(), row2.celliterator()); } } book1.close(); fis1.close(); book2.close(); fis2.close(); obj1.closeractivity(); } catch (filenotfoundexception fe) { fe.printstacktrace(); } catch (ioexception ie) { ie.printstacktrace(); } catch (exception ee) { ee.printstacktrace(); } } }
and have writeoutputintoexcel
class put result in result.xslx
package excelcompare; import java.io.fileoutputstream; import java.util.iterator; import org.apache.poi.ss.usermodel.cell; import org.apache.poi.ss.usermodel.cellstyle; import org.apache.poi.ss.usermodel.indexedcolors; import org.apache.poi.xssf.usermodel.xssfrow; import org.apache.poi.xssf.usermodel.xssfsheet; import org.apache.poi.xssf.usermodel.xssfworkbook; public class writeoutputintoexcel { private static string outputfile; private static xssfworkbook myworkbook = new xssfworkbook(); private static xssfsheet mysheet = myworkbook.createsheet("report"); public static int = 0; public void setoutputfile(string outputfile1) { outputfile = outputfile1; } public void addrow(iterator<cell> celliterator1, iterator<cell> celliterator2) { try { xssfrow row = mysheet.createrow(i++); cellstyle style = myworkbook.createcellstyle(); style = myworkbook.createcellstyle(); style.setfillforegroundcolor(indexedcolors.red.getindex()); style.setfillpattern(cellstyle.solid_foreground); fileoutputstream out = new fileoutputstream(outputfile); system.out.print("writing result sheet1"); system.out.print("\n"); while (celliterator1.hasnext()) { int j = 0; cell cell1 = celliterator1.next(); switch (cell1.getcelltype()) { case cell.cell_type_string: system.out.print(cell1.getstringcellvalue() + "\t"); row.createcell(j).setcellvalue(cell1.getstringcellvalue()); row.createcell(j).setcellstyle(style); break; case cell.cell_type_numeric: system.out.print(cell1.getnumericcellvalue() + "\t"); row.createcell(j).setcellvalue(cell1.getnumericcellvalue()); row.createcell(j).setcellstyle(style); break; case cell.cell_type_blank: system.out.print(cell1.getstringcellvalue() + "\t"); row.createcell(j).setcellvalue(cell1.getstringcellvalue()); row.createcell(j).setcellstyle(style); break; default: system.out.print(cell1.getstringcellvalue() + "\t"); row.createcell(j).setcellvalue(cell1.getstringcellvalue()); row.createcell(j).setcellstyle(style); } j++; } system.out.print("\n"); system.out.print("writing result sheet2"); system.out.print("\n"); while (celliterator2.hasnext()) { int j = 0; cell cell2 = celliterator2.next(); switch (cell2.getcelltype()) { case cell.cell_type_string: system.out.print(cell2.getstringcellvalue() + "\t"); row.createcell(j).setcellvalue(cell2.getstringcellvalue()); break; case cell.cell_type_numeric: system.out.print(cell2.getnumericcellvalue() + "\t"); row.createcell(j).setcellvalue(cell2.getnumericcellvalue()); break; case cell.cell_type_blank: system.out.print(cell2.getstringcellvalue() + "\t"); row.createcell(j).setcellvalue(cell2.getstringcellvalue()); break; default: system.out.print(cell2.getstringcellvalue() + "\t"); row.createcell(j).setcellvalue(cell2.getstringcellvalue()); } j++; } system.out.print("\n"); myworkbook.write(out); out.close(); myworkbook.close(); } catch (exception e) { e.printstacktrace(); } } public void closeractivity() { try { system.out.println(" hi in close"); } catch (exception e) { e.printstacktrace(); } } }
i want highlight cells in excel1 not same excel2
.
please me achieve this.
the best way tackle kind of problems attaching sourcecode of library (in case poi) ide, can lookup happening @ line 201 in library. please note need sure library lib version same src version, because else things can @ wrong line.
ok, 1 of arguments addrow null (iterators). first check (println).
Comments
Post a Comment