0

I am trying to insert text into a blank cell

getRow( 0 ).getCell( 0 ).setCellValue( 'Hat Selection' ) 

and I keep getting

Cannot invoke method setCellValue() on null object

Why can't I insert text into this blank cell? How can I?

Marco Polo
  • 267
  • 1
  • 5
  • 16
  • 1
    Tim is right. You should first search your solution within the existing threads. If not found, only then you should create new thread. This will help this site to be clean. You must got your answer from me or from Tim's response. Now request you to delete this thread before moderators will remove it. – Sankumarsingh Jul 10 '13 at 22:26

3 Answers3

5

In case any of your object is null, you should first create it and then should set value on that. use createRow if you have not created the row already.

sheet.createRow(0);

if you have already created the row, but not the respective cell then first create the respective cell and then put the desired value. use

sheet.getRow(0).createCell(0).setCellValue(value);

Or you can combine all

sheet.createRow(0).createCell(0).setCellValue(value);
Sankumarsingh
  • 9,889
  • 11
  • 50
  • 74
1

As I showed you in the other question you asked (but seemingly ignored the answer to)

Scriptom Groovy formating Excel Examples

you need to call createCell when creating a new cell

Community
  • 1
  • 1
tim_yates
  • 167,322
  • 27
  • 342
  • 338
0

Your program thinks that getRow(0).getCell(0) is null, NOT a cell. That's why you can't set the value because null doesn't have a setCellValue() method.

I don't know what sort of structure you're using but I'd check to make sure you're initializing first a table then each cell in said table sometime before you try to invoke this line of code. That way, each cell exists regardless of whether or not it actually has a text value.

user2521937
  • 19
  • 1
  • 2