Hi Meike, first of all you have to go through your code and correct it at some places, e.g. fill() always returns null ! Then you should be sure that in your excel file sheet at position 1 (sheets are zero-based) exists, row at position 0 exists and least the cells exists. After that it works fine.
Kind regards, Michael. > -----Original Message----- > From: Meike Reichle (RZ) [mailto:[EMAIL PROTECTED] > Sent: Sunday, March 30, 2003 4:32 PM > To: [EMAIL PROTECTED] > Subject: NullPointerException generating 0 byte files > > > Hi everyone, > > I am rather new to POI, but I am planing to use it for a project, > so I decided to first test it a bit before actually starting using it > in a big scale. > But unfortunatelly I just don't get it to work properly. > What I wanted to do is to open an existing file, add some values > and write that in a new file. Opening seems to work, but then > I am doing something wrong and I can't really figure out what it is. > The class compiles without errors, but when I try to execute it, > it throws three NullPointerExceptions and generates a new file with > the size of 0 bytes. > > It's probably a rather dumb newbie mistake I did. But since I > am still > trying to understand the concept of POI I'd be really glad > if someone > had a hint or some enlightening words for me. > > Thanks in advance, > Meike > > > Here's my complete testing class: > > > import java.io.*; > import org.apache.poi.poifs.filesystem.*; > import org.apache.poi.hssf.dev.*; > import org.apache.poi.hssf.eventmodel.*; > import org.apache.poi.hssf.model.*; > import org.apache.poi.hssf.record.*; > import org.apache.poi.hssf.usermodel.*; > import org.apache.poi.hssf.util.*; > > public class ExcelInterface > { > > //opening an existing file > public static HSSFWorkbook openFile() > { > try > { > FileInputStream in = new FileInputStream(Pfade.excel); > POIFSFileSystem fs = new POIFSFileSystem(in); > HSSFWorkbook wb = new HSSFWorkbook(fs); > return wb; > } > catch(Exception e) > { > System.out.println(e); > } > return null; > } > > > //write in some values > public static HSSFWorkbook fill() > { > try > { > HSSFWorkbook wb = openFile(); > HSSFSheet sheet = wb.getSheetAt(1); > HSSFRow row = sheet.getRow(0); > HSSFCell cell1 = row.getCell((short)1); > HSSFCell cell2 = row.getCell((short)2); > > cell1.setCellType(HSSFCell.CELL_TYPE_NUMERIC); > cell2.setCellType(HSSFCell.CELL_TYPE_STRING); > cell1.setCellValue(15); > cell2.setCellValue("test :-)"); > } > catch(Exception e) > { > System.out.println(e); > } > return null; > } > > > > //Save it to a new file > public static void closeFile() > { > try > { > HSSFWorkbook wb = fill(); > FileOutputStream out = new FileOutputStream(Pfade.excel2); > wb.write(out); > out.close(); > } > catch(Exception e) > { > System.out.println(e); > } > } > > > public static void main(String args[]) > { > openFile(); > fill(); > closeFile(); > } > > }//end of class > > > --------------------------------------------------------------------- > To unsubscribe, e-mail: [EMAIL PROTECTED] > For additional commands, e-mail: [EMAIL PROTECTED] > --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
