Java and apach poi = read Excel cell

Discussion in 'Java' started by Tk, Jul 11, 2006.

  1. Tk

    Tk Guest

    I have code :

    public class ReadXL {
    /** Location where the Excel has to be read from. Note the forward Slash
    */

    //C:\Documents and Settings\tkulej\Pulpit\java excel\jw-0322-poi
    public static void main(String argv[]){
    String fileToBeRead="C:/cos.xls";
    try{
    System.out.print("start"+"\n");
    HSSFWorkbook workbook = new HSSFWorkbook(new
    FileInputStream(fileToBeRead));
    HSSFSheet sheet = workbook.getSheetAt(0);
    HSSFRow row = sheet.getRow(0);
    HSSFCell cell = row.getCell((short)3);

    if( cell == null)
    {
    System.out.print("empty");
    } else {
    System.out.print("something is
    there");
    }


    }catch(Exception c) {
    System.out.println("!! Bang !! xlRead() : " + c );
    }

    }

    }

    This code is not good because I have exception : !! Bang !! xlRead() :
    java.lang.NullPointerException
    I must read a cell but if this cell is empty this exeception happen, how to
    fix this application ?
     
    Tk, Jul 11, 2006
    #1
    1. Advertising

  2. Tk

    Oliver Wong Guest

    "Tk" <> wrote in message
    news:e90t76$fm8$...
    >I have code :
    >
    > public class ReadXL {
    > /** Location where the Excel has to be read from. Note the forward
    > Slash */
    >
    > //C:\Documents and Settings\tkulej\Pulpit\java excel\jw-0322-poi
    > public static void main(String argv[]){
    > String fileToBeRead="C:/cos.xls";
    > try{
    > System.out.print("start"+"\n");
    > HSSFWorkbook workbook = new HSSFWorkbook(new
    > FileInputStream(fileToBeRead));
    > HSSFSheet sheet = workbook.getSheetAt(0);
    > HSSFRow row = sheet.getRow(0);
    > HSSFCell cell = row.getCell((short)3);
    >
    > if( cell == null)
    > {
    > System.out.print("empty");
    > } else {
    > System.out.print("something is
    > there");
    > }
    >
    >
    > }catch(Exception c) {
    > System.out.println("!! Bang !! xlRead() : " + c );
    > }
    >
    > }
    >
    > }
    >
    > This code is not good because I have exception : !! Bang !! xlRead() :
    > java.lang.NullPointerException
    > I must read a cell but if this cell is empty this exeception happen, how
    > to fix this application ?


    Don't catch the exception. Instead, throw it. That way you'll get a full
    stack trace which will help pinpoint the line on which the exception occurs.

    Failing that, check that sheet and row are non-null.

    - Oliver
     
    Oliver Wong, Jul 11, 2006
    #2
    1. Advertising

  3. Tk

    Tk Guest

    U¿ytkownik "Oliver Wong" <> napisa³ w wiadomo¶ci
    news:uwTsg.139897$771.11125@edtnps89...
    >
    > "Tk" <> wrote in message
    > news:e90t76$fm8$...
    >>I have code :
    >>
    >> public class ReadXL {
    >> /** Location where the Excel has to be read from. Note the forward
    >> Slash */
    >>
    >> //C:\Documents and Settings\tkulej\Pulpit\java excel\jw-0322-poi
    >> public static void main(String argv[]){
    >> String fileToBeRead="C:/cos.xls";
    >> try{
    >> System.out.print("start"+"\n");
    >> HSSFWorkbook workbook = new HSSFWorkbook(new
    >> FileInputStream(fileToBeRead));
    >> HSSFSheet sheet = workbook.getSheetAt(0);
    >> HSSFRow row = sheet.getRow(0);
    >> HSSFCell cell = row.getCell((short)3);
    >>
    >> if( cell == null)
    >> {
    >> System.out.print("empty");
    >> } else {
    >> System.out.print("something is
    >> there");
    >> }
    >>
    >>
    >> }catch(Exception c) {
    >> System.out.println("!! Bang !! xlRead() : " + c );
    >> }
    >>
    >> }
    >>
    >> }
    >>
    >> This code is not good because I have exception : !! Bang !! xlRead() :
    >> java.lang.NullPointerException
    >> I must read a cell but if this cell is empty this exeception happen, how
    >> to fix this application ?

    >
    > Don't catch the exception. Instead, throw it. That way you'll get a full
    > stack trace which will help pinpoint the line on which the exception
    > occurs.
    >
    > Failing that, check that sheet and row are non-null.
    >
    > - Oliver


    I`am begginer and I cant understood what I do to fix, any sugestions ?
     
    Tk, Jul 11, 2006
    #3
  4. Tk

    Oliver Wong Guest

    "Tk" <> wrote in message
    news:e911vt$6pi$...
    >
    > U¿ytkownik "Oliver Wong" <> napisa³ w wiadomo¶ci
    > news:uwTsg.139897$771.11125@edtnps89...
    >>
    >> "Tk" <> wrote in message
    >> news:e90t76$fm8$...
    >>>I have code :
    >>>
    >>> public class ReadXL {
    >>> /** Location where the Excel has to be read from. Note the forward
    >>> Slash */
    >>>
    >>> //C:\Documents and Settings\tkulej\Pulpit\java excel\jw-0322-poi
    >>> public static void main(String argv[]){
    >>> String fileToBeRead="C:/cos.xls";
    >>> try{
    >>> System.out.print("start"+"\n");
    >>> HSSFWorkbook workbook = new HSSFWorkbook(new
    >>> FileInputStream(fileToBeRead));
    >>> HSSFSheet sheet = workbook.getSheetAt(0);
    >>> HSSFRow row = sheet.getRow(0);
    >>> HSSFCell cell = row.getCell((short)3);
    >>>
    >>> if( cell == null)
    >>> {
    >>> System.out.print("empty");
    >>> } else {
    >>> System.out.print("something is
    >>> there");
    >>> }
    >>>
    >>>
    >>> }catch(Exception c) {
    >>> System.out.println("!! Bang !! xlRead() : " + c );
    >>> }
    >>>
    >>> }
    >>>
    >>> }
    >>>
    >>> This code is not good because I have exception : !! Bang !! xlRead() :
    >>> java.lang.NullPointerException
    >>> I must read a cell but if this cell is empty this exeception happen, how
    >>> to fix this application ?

    >>
    >> Don't catch the exception. Instead, throw it. That way you'll get a full
    >> stack trace which will help pinpoint the line on which the exception
    >> occurs.
    >>
    >> Failing that, check that sheet and row are non-null.
    >>

    >
    > I`am begginer and I cant understood what I do to fix, any sugestions ?


    Do you know what exceptions are and what "catch" keyword does?

    - Oliver
     
    Oliver Wong, Jul 11, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. [XaToA]
    Replies:
    0
    Views:
    3,230
    [XaToA]
    Jul 29, 2003
  2. Bala via JavaKB.com

    POI - Excel cell formatting for negative values

    Bala via JavaKB.com, Mar 5, 2005, in forum: Java
    Replies:
    0
    Views:
    2,747
    Bala via JavaKB.com
    Mar 5, 2005
  3. clement
    Replies:
    0
    Views:
    1,067
    clement
    Mar 7, 2005
  4. Replies:
    7
    Views:
    6,750
  5. Rob Y.

    POI HSSF Cell Style question

    Rob Y., Oct 14, 2008, in forum: Java
    Replies:
    0
    Views:
    3,129
    Rob Y.
    Oct 14, 2008
Loading...

Share This Page