Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Java and apach poi = read Excel cell

Reply
Thread Tools

Java and apach poi = read Excel cell

 
 
Tk
Guest
Posts: n/a
 
      07-11-2006
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 ?

 
Reply With Quote
 
 
 
 
Oliver Wong
Guest
Posts: n/a
 
      07-11-2006

"Tk" <(E-Mail Removed)> wrote in message
news:e90t76$fm8$(E-Mail Removed)...
>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

 
Reply With Quote
 
 
 
 
Tk
Guest
Posts: n/a
 
      07-11-2006

Użytkownik "Oliver Wong" <(E-Mail Removed)> napisał w wiadomości
news:uwTsg.139897$771.11125@edtnps89...
>
> "Tk" <(E-Mail Removed)> wrote in message
> news:e90t76$fm8$(E-Mail Removed)...
>>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 ?

 
Reply With Quote
 
Oliver Wong
Guest
Posts: n/a
 
      07-11-2006

"Tk" <(E-Mail Removed)> wrote in message
news:e911vt$6pi$(E-Mail Removed)...
>
> Użytkownik "Oliver Wong" <(E-Mail Removed)> napisał w wiadomości
> news:uwTsg.139897$771.11125@edtnps89...
>>
>> "Tk" <(E-Mail Removed)> wrote in message
>> news:e90t76$fm8$(E-Mail Removed)...
>>>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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
POI HSSF Cell Style question Rob Y. Java 0 10-14-2008 09:14 PM
Jakarta POI and reading from an open Excel file? mrafn@lakenet.com Java 7 11-18-2005 07:12 PM
java api POI : "how add a comment in a Excel cell with java ?" clement Java 0 03-07-2005 08:30 PM
POI - Excel cell formatting for negative values Bala via JavaKB.com Java 0 03-05-2005 01:34 PM
problem with poi, XML and sorting excel file [XaToA] Java 0 07-29-2003 12:39 PM



Advertisments