I am trying to add a comment to a cell in Excel. I am using jxl library to do that:
cell = sheet.getWritableCell(1, 2); // cols, rows
WritableCellFeatures wcf = cell.getWritableCellFeatures();
wcf.setComment("comment2");
The last line returns: Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException. Despite many attempts I can’t fix it. Help will be appreciated. Thank you.
–EDIT–
This is the addNumber method after modifications:
private static void addNumber(WritableSheet sheet, int column, int row,
double d) throws WriteException, RowsExceededException {
Number number;
number = new Number(column, row, d, timesStandard);
//sheet.addCell(number); // need to add the cell first
if (user wants to add a comment) {
WritableCellFeatures wcf = new WritableCellFeatures();
wcf.setComment("the comment");
number.setCellFeatures(wcf);
}
//sheet.addCell(number); // but may need to add the cell with comments as well
}
Have you previously added a cell at that location? The problem is that you can’t set cell features on an
EmptyCelland it will always returnnullas its cell features.If you add a cell first, it works (try/catch omitted for clarity), as shown by the code below. Note that it also sets a
WritableCellFeatureson the newLabelcell first, since initially, cell features are alwaysnull.Using this with the method in the OP:
I modified the method to return the created (and added!)
Numberinstance. If you don’t want that, you could instead retrieve the same cell usingWritableWorkbook.getWritableCell()using the same row/col.