This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

Bug 197021 - NullPointerException at org.netbeans.modules.java.source.save.Reindenter.reindent
Summary: NullPointerException at org.netbeans.modules.java.source.save.Reindenter.rein...
Status: RESOLVED FIXED
Alias: None
Product: editor
Classification: Unclassified
Component: Formatting & Indentation (show other bugs)
Version: 7.0
Hardware: All All
: P3 normal (vote)
Assignee: Dusan Balek
URL:
Keywords:
Depends on:
Blocks: 179047
  Show dependency tree
 
Reported: 2011-03-23 21:18 UTC by err
Modified: 2011-03-30 08:44 UTC (History)
0 users

See Also:
Issue Type: DEFECT
Exception Reporter: 177855


Attachments
stacktrace (3.06 KB, text/plain)
2011-03-23 21:18 UTC, err
Details
proposed fix (2.34 KB, patch)
2011-03-24 05:11 UTC, err
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description err 2011-03-23 21:18:06 UTC
Build: NetBeans IDE 7.0 Beta 2 (Build 201102140001)
VM: Java HotSpot(TM) Client VM, 19.0-b09, Java(TM) SE Runtime Environment, 1.6.0_23-b05
OS: Windows XP

User Comments:
err: Reindent where the last line of the region is only a '\n'




Stacktrace: 
java.lang.NullPointerException
   at org.netbeans.modules.java.source.save.Reindenter.reindent(Reindenter.java:169)
   at org.netbeans.modules.editor.indent.TaskHandler$MimeItem.runTask(TaskHandler.java:546)
   at org.netbeans.modules.editor.indent.TaskHandler.runTasks(TaskHandler.java:316)
   at org.netbeans.modules.editor.indent.IndentImpl.reindent(IndentImpl.java:260)
   at org.netbeans.modules.editor.indent.api.Indent.reindent(Indent.java:171)
   at org.netbeans.modules.jvi.impl.NbBuffer.reindent(NbBuffer.java:214)
Comment 1 err 2011-03-23 21:18:11 UTC
Created attachment 107229 [details]
stacktrace
Comment 2 err 2011-03-24 05:11:06 UTC
Created attachment 107234 [details]
proposed fix

The NPE occurs because at Reindenter:164
            Integer newIndent = newIndents.get(startOffset);
there is no map entry for startOffset of the last line, so newIndent is set to null. This occurs when the last line of the range is a single '\n'. In that case the while loop at Reindenter:139 
            while (endOffset < region.getEndOffset()) {
never executes the loop body for the last line of the range and so the map entry is never created.

For example, my test case has three lines with startOffsets list of {53,70,76} and region.getEndOffset() is 76. The "while(76 < 76)" fails. Changing the condition to "<=" results in an infinite loop.

The attached patch insures that the body is executed for each line; and simplifies the logic at the expense of adding an it.next;it.previous for each loop.

BTW, the bug with the while condition arises because IndentImpl:288
            doc.createPosition(lineElem.getEndOffset() - 1));
subtracts one from the offset. Also see IndentImpl:278.
Comment 3 err 2011-03-28 12:33:26 UTC
Adding "blocks" to jVi umbrella.

The supplied is pretty straightforward. Can it be evaluated?
Comment 4 Dusan Balek 2011-03-29 13:09:04 UTC
Fixed in jet-main. Thanks for the patch.

http://hg.netbeans.org/jet-main/rev/b005ac22e5dd
Comment 5 Quality Engineering 2011-03-30 08:44:19 UTC
Integrated into 'main-golden', will be available in build *201103300400* on http://bits.netbeans.org/dev/nightly/ (upload may still be in progress)
Changeset: http://hg.netbeans.org/main/rev/b005ac22e5dd
User: Dusan Balek <dbalek@netbeans.org>
Log: Issue #197021: NullPointerException at org.netbeans.modules.java.source.save.Reindenter.reindent - fixed.