Java and apach poi = read Excel cell

T

Tk

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 ?
 
O

Oliver Wong

Tk said:
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
 
T

Tk

U¿ytkownik "Oliver Wong said:
Tk said:
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 ?
 
O

Oliver Wong

Tk said:
U¿ytkownik "Oliver Wong said:
Tk said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top