You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@poi.apache.org by Saad Shakil <ss...@rim.com> on 2010/12/16 00:01:18 UTC

NPE when trying to IO an xlsx fle

Hi, I'm getting an NPE while trying out some tutorials:
Exception in thread "Main Thread" java.lang.NullPointerException
      at prv.ba.ave.cli.testingWorksheet.main(testingWorksheet.java:32)


In:
package prv.ba.ave.cli;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;


public class testingWorksheet {


    public static void main(String[] args) {

        InputStream inp=null;
        Workbook wb=null;
        FileOutputStream fileOut=null;

        try {
            inp = new FileInputStream("c:\\book.xlsx");
            wb = WorkbookFactory.create(inp);

            Sheet sheet = wb.getSheetAt(0);
            Row row = sheet.getRow(2);
            Cell cell = row.getCell(3);
            if (cell == null)
                cell = row.createCell(3);
            cell.setCellType(Cell.CELL_TYPE_STRING);
            cell.setCellValue("a test");

            fileOut = new FileOutputStream("book.xlsx");
            wb.write(fileOut);
            fileOut.close();
        }
        catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        catch (InvalidFormatException e) {
            e.printStackTrace();
        }
        catch (IOException e) {
            e.printStackTrace();
        }
    }

}



I also tried:
package prv.ba.ave.cli;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;


public class testingWorksheet {


    public static void main(String[] args) {

        InputStream inp=null;
        Workbook wb=null;
        FileOutputStream fileOut=null;

        try {
            inp = new FileInputStream("c:\\book.xlsx");
            wb = WorkbookFactory.create(inp);

            Sheet sheet = wb.getSheetAt(0);
            Row row = sheet.getRow(2);

            Cell cell = null;
            try {
                cell = row.getCell(3);
            } catch(NullPointerException ex) {
                cell = row.createCell(3);
            }

            cell.setCellType(Cell.CELL_TYPE_STRING);
            cell.setCellValue("a test");

            fileOut = new FileOutputStream("book.xlsx");
            wb.write(fileOut);
            fileOut.close();
        }
        catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        catch (InvalidFormatException e) {
            e.printStackTrace();
        }
        catch (IOException e) {
            e.printStackTrace();
        }
    }

}
Getting:
Exception in thread "Main Thread" java.lang.NullPointerException
      at prv.ba.ave.cli.testingWorksheet.main(testingWorksheet.java:37)


Any help?

Thanks,
-saad

---------------------------------------------------------------------
This transmission (including any attachments) may contain confidential information, privileged material (including material protected by the solicitor-client or other applicable privileges), or constitute non-public information. Any use of this information by anyone other than the intended recipient is prohibited. If you have received this transmission in error, please immediately reply to the sender and delete this information from your system. Use, dissemination, distribution, or reproduction of this transmission by unintended recipients is not authorized and may be unlawful.

Re: NPE when trying to IO an xlsx fle

Posted by Saad Shakil <ss...@rim.com>.
Define what doesn't exist mean.  There is a row there.  It has no content.


----- Original Message -----
From: Nick Burch [mailto:nick.burch@alfresco.com]
Sent: Thursday, December 16, 2010 02:26 AM
To: POI Users List <us...@poi.apache.org>
Subject: Re: NPE when trying to IO an xlsx fle

On Thu, 16 Dec 2010, Saad Shakil wrote:
> The file is empty, with no content.  I'd expect that it return a null, 
> instead of an NPE.

The call to get a row that doesn't exist does return a null. You get a NPE 
if you then try to use that null as a row...

Nick

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org


---------------------------------------------------------------------
This transmission (including any attachments) may contain confidential information, privileged material (including material protected by the solicitor-client or other applicable privileges), or constitute non-public information. Any use of this information by anyone other than the intended recipient is prohibited. If you have received this transmission in error, please immediately reply to the sender and delete this information from your system. Use, dissemination, distribution, or reproduction of this transmission by unintended recipients is not authorized and may be unlawful.

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org


Re: NPE when trying to IO an xlsx fle

Posted by Nick Burch <ni...@alfresco.com>.
On Thu, 16 Dec 2010, Saad Shakil wrote:
> The file is empty, with no content.  I'd expect that it return a null, 
> instead of an NPE.

The call to get a row that doesn't exist does return a null. You get a NPE 
if you then try to use that null as a row...

Nick

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org


Re: NPE when trying to IO an xlsx fle

Posted by Saad Shakil <ss...@rim.com>.
That is to say, the xlsx has sheets and rows, but no text or anything anywhere.


----- Original Message -----
From: Saad Shakil [mailto:sshakil@rim.com]
Sent: Thursday, December 16, 2010 02:23 AM
To: 'user@poi.apache.org' <us...@poi.apache.org>
Subject: Re: NPE when trying to IO an xlsx fle

The file is empty, with no content.  I'd expect that it return a null, instead of an NPE.



----- Original Message -----
From: Nick Burch [mailto:nick.burch@alfresco.com]
Sent: Thursday, December 16, 2010 02:21 AM
To: POI Users List <us...@poi.apache.org>
Subject: Re: NPE when trying to IO an xlsx fle

On Thu, 16 Dec 2010, Saad Shakil wrote:
> Would someone be so kind as to suggest a way that I can run this without 
> error?  The exception is thrown from line Cell cell = row.getCell(3), 
> which is from the tutorial.

Did you check you actually have a row at this position? My hunch is you 
don't, which is why your call to fetch the row returned null

Nick

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org


---------------------------------------------------------------------
This transmission (including any attachments) may contain confidential information, privileged material (including material protected by the solicitor-client or other applicable privileges), or constitute non-public information. Any use of this information by anyone other than the intended recipient is prohibited. If you have received this transmission in error, please immediately reply to the sender and delete this information from your system. Use, dissemination, distribution, or reproduction of this transmission by unintended recipients is not authorized and may be unlawful.

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org


---------------------------------------------------------------------
This transmission (including any attachments) may contain confidential information, privileged material (including material protected by the solicitor-client or other applicable privileges), or constitute non-public information. Any use of this information by anyone other than the intended recipient is prohibited. If you have received this transmission in error, please immediately reply to the sender and delete this information from your system. Use, dissemination, distribution, or reproduction of this transmission by unintended recipients is not authorized and may be unlawful.

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org


Re: NPE when trying to IO an xlsx fle

Posted by Saad Shakil <ss...@rim.com>.
The file is empty, with no content.  I'd expect that it return a null, instead of an NPE.



----- Original Message -----
From: Nick Burch [mailto:nick.burch@alfresco.com]
Sent: Thursday, December 16, 2010 02:21 AM
To: POI Users List <us...@poi.apache.org>
Subject: Re: NPE when trying to IO an xlsx fle

On Thu, 16 Dec 2010, Saad Shakil wrote:
> Would someone be so kind as to suggest a way that I can run this without 
> error?  The exception is thrown from line Cell cell = row.getCell(3), 
> which is from the tutorial.

Did you check you actually have a row at this position? My hunch is you 
don't, which is why your call to fetch the row returned null

Nick

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org


---------------------------------------------------------------------
This transmission (including any attachments) may contain confidential information, privileged material (including material protected by the solicitor-client or other applicable privileges), or constitute non-public information. Any use of this information by anyone other than the intended recipient is prohibited. If you have received this transmission in error, please immediately reply to the sender and delete this information from your system. Use, dissemination, distribution, or reproduction of this transmission by unintended recipients is not authorized and may be unlawful.

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org


Re: NPE when trying to IO an xlsx fle

Posted by Nick Burch <ni...@alfresco.com>.
On Thu, 16 Dec 2010, Saad Shakil wrote:
> Would someone be so kind as to suggest a way that I can run this without 
> error?  The exception is thrown from line Cell cell = row.getCell(3), 
> which is from the tutorial.

Did you check you actually have a row at this position? My hunch is you 
don't, which is why your call to fetch the row returned null

Nick

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org


Re: NPE when trying to IO an xlsx fle

Posted by Saad Shakil <ss...@rim.com>.
Would someone be so kind as to suggest a way that I can run this without error?  The exception is thrown from line Cell cell = row.getCell(3), which is from the tutorial.

-saad 

----- Original Message -----
From: Nick Burch [mailto:nick.burch@alfresco.com]
Sent: Wednesday, December 15, 2010 10:51 PM
To: POI Users List <us...@poi.apache.org>
Subject: Re: NPE when trying to IO an xlsx fle

On Wed, 15 Dec 2010, Saad Shakil wrote:
> Hi, I'm getting an NPE while trying out some tutorials:
> Exception in thread "Main Thread" java.lang.NullPointerException
>      at prv.ba.ave.cli.testingWorksheet.main(testingWorksheet.java:32)

This exception isn't coming from poi. Looks to be in your own code

NIck

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org


---------------------------------------------------------------------
This transmission (including any attachments) may contain confidential information, privileged material (including material protected by the solicitor-client or other applicable privileges), or constitute non-public information. Any use of this information by anyone other than the intended recipient is prohibited. If you have received this transmission in error, please immediately reply to the sender and delete this information from your system. Use, dissemination, distribution, or reproduction of this transmission by unintended recipients is not authorized and may be unlawful.

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org


Re: NPE when trying to IO an xlsx fle

Posted by Nick Burch <ni...@alfresco.com>.
On Wed, 15 Dec 2010, Saad Shakil wrote:
> Hi, I'm getting an NPE while trying out some tutorials:
> Exception in thread "Main Thread" java.lang.NullPointerException
>      at prv.ba.ave.cli.testingWorksheet.main(testingWorksheet.java:32)

This exception isn't coming from poi. Looks to be in your own code

NIck

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org