Exception in thread "main" java.lang.NullPointerException

Hi,
I am trying to find the length of the cell and coloring the string which is not in limit. but i am getting Null pointer Exception.

Attached Demo File.


Code:-
int num=cells.getMaxDataRow();
int num1=cells.getMaxDataColumn();
for (int n1=0;n1<=num;n1++)
{
for(int nColIndex =0;nColIndex<=num1;nColIndex++)
{
Cell cell1=cells.getCell(n1, nColIndex);
System.out.println(cell1.getValue().toString());

if(cell1==null)

{
Font font = new Font();
int celllength =cell1.getValue().toString().length();
System.out.println(cell1.getValue().toString().length());
if(celllength>10)
{
font.setBold(true);
font.setColor(Color.RED);
cell1.setCharacters(10,celllength,font);
}
else
{
System.out.println(“Within Limit”);
}
}
}

}

Hi,

Thanks for your posting.

Please comment this line and your code will run fine.

System.out.println(cell1.getValue().toString());

Your complete code will look like this after commenting.

Java


Workbook workbook = new Workbook();

workbook.open(“F:\Shak-Data-RW\Downloads\Demo.xls”);


Worksheet worksheet = workbook.getWorksheets().getSheet(0);

Cells cells=worksheet.getCells();


int num=cells.getMaxDataRow();

int num1=cells.getMaxDataColumn();


for (int n1=0;n1<=num;n1++)

{

for(int nColIndex =0;nColIndex<=num1;nColIndex++)

{

Cell cell1=cells.getCell(n1, nColIndex);

// System.out.println(cell1.getValue().toString());


if(cell1==null)

{

Font font = new Font();

int celllength =cell1.getValue().toString().length();

System.out.println(cell1.getValue().toString().length());


if(celllength>10)

{

font.setBold(true);

font.setColor(Color.RED);

cell1.setCharacters(10,celllength,font);

}

else

{

System.out.println(“Within Limit”);

}

}//if

}//for

}//for

Thanks for your Valuable reply.

But it gives me the error at following line

int celllength =cell1.getValue().toString().length();

In my code if(cell1==null) it should be if(cell1!=null) then only we can check the Data.

Sorry for it.

Hi,

Also, there is one more correction, your for loops should iterate till n<cells.getMaxDataRow() and n<cells.getMaxDataColumn()

e.g

Java


Workbook workbook = new Workbook();
workbook.open(“F:\Shak-Data-RW\Downloads\Demo.xls”);


Worksheet worksheet = workbook.getWorksheets().getSheet(0);
Cells cells=worksheet.getCells();

for (int rowIdx=0;rowIdx<cells.getMaxDataRow();rowIdx++)
{
for(int colIdx =0;colIdx<cells.getMaxDataColumn();colIdx++)
{
Cell cell1=cells.getCell(rowIdx, colIdx);
//your rest of code
}//for
}//for

Thanks,
I have made changes but it won’t work for me.i am getting following error Message at this Line

int celllength =cell1.getValue().toString().length();

Exception in thread “main” java.lang.NullPointerException
at notfitting.Main.main(Main.java:45)


Here is my complete code.

Code:

try
{
Workbook workbook = new Workbook();
workbook.open(“C:\Demo.xls”);
Worksheet worksheet = workbook.getWorksheets().getSheet(0);
Cells cells=worksheet.getCells();
int num=cells.getMaxDataRow();
int num1=cells.getMaxDataColumn();
for (int n1=0;n1<num;n1++)
{
for(int nColIndex =0;nColIndex<num1;nColIndex++)
{
Cell cell1=cells.getCell(n1, nColIndex);
if(cell1!=null)
{
Font font = new Font();
int celllength =cell1.getValue().toString().length();
System.out.println(cell1.getValue().toString().length());
if(celllength>10)
{
font.setBold(true);
font.setColor(Color.RED);
cell1.setCharacters(10,celllength,font);
}
else
{
System.out.println(“Within Limit”);
}
}else//if
{

}
}//for
}//for
workbook.save(“C:\OutDemoxls”,FileFormatType.EXCEL97TO2003);
System.out.println(“Done”);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

Hi,

Firstly, if you only need to check the cell’s value, it is better for you to use cells.checkCell() or use the cell iterator to process every cell instead. Cells.getCell() will initialize the Cell object if it has not been initialized. So, your invocation of Cells.getCell() may cost much more memory than actually needed and the condition “cell1!=null” will always be true in your code.

Secondly, even if one cell is not null, its value may be null(when cell.getValueType == CellValueType.NULL), so it is sure that you may encounter NPE when using code like cell1.getValue().toString(). You should check the object returned by cell.getValue() and process it only when it is not null.