title: java object instantiation to return null?
body: <p>Maybe it looks like a very ordinary question but I am also surprised at how this can be possible.
  I am creating an object in one of my class using the following statement:</p>
  
  <pre><code>TestVO testVO = new TestVO();
  </code></pre>
  
  <p>it returns null. However when passing the <code>testVO</code> object to a method( which parses an Excel sheet and sets the data in VO) it does not throw <code>NullPointerException</code>( In eclipse debug mode, testVO value is shown as null). When trying to use the same object in next set of statements, it throws the NPE.</p>
  
  <p>I am also surprised why above statement returns null and not assigning the memory to the object.</p>
  
  <p>Please note when creating the <code>TestVO</code> class object in another class, it is working fine. This is happening only a particular class.</p>
  
  <p>Below is the class source code:</p>
  
  <pre><code>public class SheetParser {
  public SheetParser() {
  
  }
  public void parseSheet(Workbook workbook, List&lt;TestVO&gt; list) {
      TestVO testVO = null;
      Row row = null;
      Cell cell = null;
      String header = null;
      boolean hasHeaderRow = false;
      boolean hasDataRow = false;
      Sheet sheet = workbook.getSheetAt(0);
      Iterator&lt;Row&gt; rows = sheet.rowIterator();
      List&lt;String&gt; headers = new ArrayList&lt;String&gt;();
      try {
          while (rows.hasNext()) {
              row = rows.next();
              if (row.getRowNum() == 0) {
                  hasHeaderRow = true;
                  int i = 0;
                  while (true) {
                      try {
                          cell = row.getCell(i++);
                          if (cell == null) {
                              break;
                          }
                          cell.setCellType(Cell.CELL_TYPE_STRING);
                          header = cell.getStringCellValue();
                          if (header == null || "".equals(header.trim())) {
                              break;
                          }
                          headers.add(header.toUpperCase());
                      } catch (NullPointerException e) {
                          break;
                      }
                  }
                  validateHeaders(headers);
              } else {
                  hasDataRow = true;
                  testVO = new TestVO();
                  // HERE the above statement failed to initialized the object
                  // and shows the testVO as null
                  // But below in setRowData method, value is set in testVo
                  // object after reading from excel sheet
                  boolean error = setRowData(testVO, headers, row, errorList);
                  if (!error) {
                      list.add(testVO);
                  }
              }
          }
      } catch (Exception e) {
          errorList.add(e.getMessage());
      }
      if (!hasHeaderRow) {
          errorList.add("Uploaded Excel file is invalid, headers missing");
      }
      if (!hasDataRow) {
          errorList
                  .add("Uploaded Excel file is invalid, data row(s) are missing.");
      }
  }
  
  private boolean setRowData(TestVO testVO, List&lt;String&gt; headersList,
          Row row, List&lt;String&gt; errors) {
      boolean error = false;
      boolean allCellsBlank = true;
      List&lt;String&gt; errorList = new ArrayList&lt;String&gt;();
      String rowPrefixText = "At Row #";
      for (int i = 0; i &lt; headersList.size(); i++) {
          String header = headersList.get(i);
          String value = "";
          try {
              row.getCell(i).setCellType(Cell.CELL_TYPE_STRING);
              value = row.getCell(i).toString().trim();
          } catch (NullPointerException e) {
              // do nothing
          }
          try {
              if (header.equalsIgnoreCase("ABC")) {
                  if (StringUtils.isBlank(value)) {
                      errorList.add(rowPrefixText + row.getRowNum()
                              + ", ABC is required.");
                      error = true;
                  } else {
                      testVO.setAbc(value);
                      allCellsBlank = false;
                  }
              } else if (header.equalsIgnoreCase("XYZ")) {
                  if (StringUtils.isBlank(value)) {
                      errorList.add(rowPrefixText + row.getRowNum()
                              + ", XYZ is required.");
                      error = true;
                  } else {
                      testVO.setXyz(value);
                      allCellsBlank = false;
                  }
              }
          } catch (Exception e) {
              errorList.add(rowPrefixText + row.getRowNum()
                      + ", incorrect data / format in column:" + header);
              error = true;
          }
      }
      if (allCellsBlank) {
          error = true;
      } else if (!errorList.isEmpty()) {
          errors.addAll(errorList);
      }
      return error;
  }
  
  private void validateHeaders(List&lt;String&gt; headersList) throws Exception {
      if (!headersList.contains("ABC")) {
          throw new Exception("Invalid Headers - missing ABC");
      } else if (!headersList.contains("XYZ")) {
          throw new Exception("Invalid Headers - missing XYZ");
      }
  }
  </code></pre>
  
  <p>}</p>