title: Jxl NullPointerException to do with workbook.close() method
body: <p>I am attempting to write to an excel file, but I have encountered two problems. Here is the relevant code:</p>
  
  <pre><code>public ExcelFormWriter() throws IOException, WriteException{
    setOutputFile("DataAnalysis.xls");
  
      File file = new File(inputFile);
      WorkbookSettings wbSettings = new WorkbookSettings();
      wbSettings.setLocale(new Locale("en", "EN"));
      workbook = Workbook.createWorkbook(file, wbSettings);
      workbook.createSheet("RawData", 0);
  
    }
  
    public void addLabel(int column, int row, String s) throws WriteException, RowsExceededException, IOException {
      excelSheet = workbook.getSheet(0);
      createLabel(excelSheet);
      Label label = new Label(column, row, s, times);
      excelSheet.addCell(label);
      workbook.write();
      workbook.close();
    }
  </code></pre>
  
  <p>In another class, I call it like this:</p>
  
  <pre><code>    ExcelFormWriter test = new ExcelFormWriter();
   int row = 1;
          for (int i = 100; i &gt; 0; i--){
              test.addLabel(0,row,getDateNDaysAgo(i));
              row++;
          }
  </code></pre>
  
  <p>If I run it just like this, it writes the first line in the excel file and then returns a NullPointerException. </p>
  
  <p>I found that if I comment out the 'workbook.close()', it runs through the for loop and the build finishes successfully, but when I look in the excel file, nothing is actually written. </p>
  
  <p>I'm not entirely familiar with writing to spreadsheets, so it's likely that there's a basic flaw in my code, like something being in the wrong place. Any answer is appreciated, but please be specific in your answers, because I'm feeling around in the dark with this code and I don't know what does what. </p>
  
  <p>Suggestions for future diagnostics/troubleshooting of this stuff would also be appreciated. </p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>I have put in a separate method for closing the workbook. Thanks for the tip. However, the second problem persists. Only the first line of the spreadsheet is written.</p>
  
  <pre><code>  public void close() throws IOException, WriteException{
        workbook.close();
    }
  </code></pre>
  
  <p>This is where I called the method. Perhaps there's a problem with that?</p>
  
  <pre><code>int row = 1;
          for (int i = 100; i &gt; 0; i--){
              test.addLabel(0,row,getDateNDaysAgo(i));
              row++;
          }
          test.close();
  </code></pre>