poi-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bugzi...@apache.org
Subject DO NOT REPLY [Bug 48846] New: TextObjectRecord sometimes become null, so it must be checked before using.
Date Wed, 03 Mar 2010 03:00:49 GMT
https://issues.apache.org/bugzilla/show_bug.cgi?id=48846

           Summary: TextObjectRecord sometimes become null, so it must be
                    checked before using.
           Product: POI
           Version: 3.6
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: normal
          Priority: P2
         Component: HSSF
        AssignedTo: dev@poi.apache.org
        ReportedBy: benbrand@mac.com


In findCellComment(Sheet, int, int) method of
org.apache.poi.hssf.usermodel.HSSFCell, TextObjectRecord sometimes become null.

-- original code --
        for (Iterator<RecordBase> it = sheet.getRecords().iterator();
it.hasNext();) {
            RecordBase rec = it.next();
            if (rec instanceof NoteRecord) {
                NoteRecord note = (NoteRecord) rec;
                if (note.getRow() == row && note.getColumn() == column) {
                    if(i < noteTxo.size()) {
                        TextObjectRecord txo = noteTxo.get(note.getShapeId());
                        comment = new HSSFComment(note, txo);
                        comment.setRow(note.getRow());
                        comment.setColumn((short) note.getColumn());
                        comment.setAuthor(note.getAuthor());
                        comment.setVisible(note.getFlags() ==
NoteRecord.NOTE_VISIBLE);
                        comment.setString(txo.getStr());
                    } else {
                        log.log(POILogger.WARN, "Failed to match NoteRecord and
TextObjectRecord, row: " + row + ", column: " + column);
                    }
                    break;
                }
                i++;
            } else if (rec instanceof ObjRecord) {
 --- end ------------

With this code, my program crashed with NullPointerException on line of
'comment.setString(txo.getStr());', because a local variable 'txo' is null.
I changed the code like bellow, then the program works correctly.

--- changed code ---
        for (Iterator<RecordBase> it = sheet.getRecords().iterator();
it.hasNext();) {
            RecordBase rec = it.next();
            if (rec instanceof NoteRecord) {
                NoteRecord note = (NoteRecord) rec;
                if (note.getRow() == row && note.getColumn() == column) {
                    if(i < noteTxo.size()) {
                        TextObjectRecord txo = noteTxo.get(note.getShapeId());

                        //NEXT LINE IS ADDED
                        if(txo != null) { // <-- MUST CHECK txo IS NOT NULL.
                            comment = new HSSFComment(note, txo);
                            comment.setRow(note.getRow());
                            comment.setColumn((short) note.getColumn());
                            comment.setAuthor(note.getAuthor());
                            comment.setVisible(note.getFlags() ==
NoteRecord.NOTE_VISIBLE);
                            comment.setString(txo.getStr());
                        } else {
                            log.log(POILogger.WARN, "Failed to match NoteRecord
and TextObjectRecord, row: " + row + ", column: " + column);
                        }

                    } else {
                        log.log(POILogger.WARN, "Failed to match NoteRecord and
TextObjectRecord, row: " + row + ", column: " + column);
                    }
                    break;
                }
                i++;
            } else if (rec instanceof ObjRecord) {
--- end ------------

Thanks.

-- 
Configure bugmail: https://issues.apache.org/bugzilla/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the assignee for the bug.

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@poi.apache.org
For additional commands, e-mail: dev-help@poi.apache.org


Mime
View raw message