Bug 19335 - NPE in extract method
Summary: NPE in extract method
Status: RESOLVED DUPLICATE of bug 18997
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 2.0   Edit
Hardware: PC Windows 2000
: P3 normal (vote)
Target Milestone: 2.0 F3   Edit
Assignee: Olivier Thomann CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2002-06-05 10:55 EDT by Till Bay CLA
Modified: 2002-06-07 15:31 EDT (History)
3 users (show)

See Also:


Attachments
code.txt (1.09 KB, text/plain)
2002-06-05 10:56 EDT, Till Bay CLA
no flags Details

Note You need to log in before you can comment on or make changes to this bug.
Description Till Bay CLA 2002-06-05 10:55:51 EDT
While trying to extract the code shown in code.txt attachment to a method:


!ENTRY org.eclipse.ui 4 0 Jun 05, 2002 16:41:53.336
!MESSAGE java.lang.NullPointerException
!STACK 0
java.lang.NullPointerException
	at org.eclipse.jdt.internal.corext.util.Strings.computeIndent
(Strings.java:128)
	at org.eclipse.jdt.internal.corext.util.CodeFormatterUtil.computeIndent
(CodeFormatterUtil.java:43)
	at 
org.eclipse.jdt.internal.corext.util.CodeFormatterUtil.createIndentString
(CodeFormatterUtil.java:66)
	at org.eclipse.jdt.internal.corext.codemanipulation.MethodBlock.fill
(MethodBlock.java:52)
	at 
org.eclipse.jdt.internal.corext.codemanipulation.CodeBlockEdit.createText
(CodeBlockEdit.java:123)
	at 
org.eclipse.jdt.internal.corext.codemanipulation.CodeBlockEdit.perform
(CodeBlockEdit.java:94)
	at 
org.eclipse.jdt.internal.corext.textmanipulation.TextEditNode.performDo
(TextEditNode.java:444)
	at 
org.eclipse.jdt.internal.corext.textmanipulation.TextEditNode.performDo
(TextEditNode.java:438)
	at 
org.eclipse.jdt.internal.corext.textmanipulation.TextEditNode$RootNode.performDo
(TextEditNode.java:49)
	at 
org.eclipse.jdt.internal.corext.textmanipulation.TextBufferEditor.performEdits
(TextBufferEditor.java:173)
	at 
org.eclipse.jdt.internal.corext.refactoring.changes.TextChange.getPreviewTextBuf
fer(TextChange.java:187)
	at 
org.eclipse.jdt.internal.corext.refactoring.changes.TextChange.getPreviewContent
(TextChange.java:177)
	at 
org.eclipse.jdt.internal.ui.refactoring.PreviewWizardPage.getCompareInput
(PreviewWizardPage.java:236)
	at 
org.eclipse.jdt.internal.ui.refactoring.PreviewWizardPage.getPreviewer
(PreviewWizardPage.java:298)
	at org.eclipse.jdt.internal.ui.refactoring.PreviewWizardPage.showPreview
(PreviewWizardPage.java:429)
	at org.eclipse.jdt.internal.ui.refactoring.PreviewWizardPage.access$2
(PreviewWizardPage.java:427)
	at 
org.eclipse.jdt.internal.ui.refactoring.PreviewWizardPage$2.selectionChanged
(PreviewWizardPage.java:418)
	at org.eclipse.jface.viewers.Viewer.fireSelectionChanged
(Viewer.java:147)
	at org.eclipse.jface.viewers.StructuredViewer.updateSelection
(StructuredViewer.java:1153)
	at org.eclipse.jface.viewers.StructuredViewer.setSelection
(StructuredViewer.java:903)
	at org.eclipse.jface.viewers.Viewer.setSelection(Viewer.java:342)
	at org.eclipse.jdt.internal.ui.refactoring.PreviewWizardPage.setVisible
(PreviewWizardPage.java:383)
	at org.eclipse.jface.wizard.WizardDialog.showPage(WizardDialog.java:888)
	at org.eclipse.jface.wizard.WizardDialog.nextPressed
(WizardDialog.java:653)
	at org.eclipse.jface.wizard.WizardDialog.buttonPressed
(WizardDialog.java:308)
	at org.eclipse.jface.dialogs.Dialog$1.widgetSelected(Dialog.java:398)
	at org.eclipse.swt.widgets.TypedListener.handleEvent
(TypedListener.java:85)
	at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:75)
	at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:825)
	at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:1527)
	at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:1289)
	at org.eclipse.jface.window.Window.runEventLoop(Window.java:558)
	at org.eclipse.jface.window.Window.open(Window.java:538)
	at 
org.eclipse.jdt.internal.ui.refactoring.actions.RefactoringStarter.activate
(RefactoringStarter.java:79)
	at org.eclipse.jdt.ui.actions.ExtractMethodAction.run
(ExtractMethodAction.java:47)
	at org.eclipse.jdt.ui.actions.SelectionDispatchAction.dispatchRun
(SelectionDispatchAction.java:180)
	at org.eclipse.jdt.ui.actions.SelectionDispatchAction.run
(SelectionDispatchAction.java:156)
	at org.eclipse.jface.action.Action.runWithEvent(Action.java:749)
	at org.eclipse.jface.action.ActionContributionItem.handleWidgetSelection
(ActionContributionItem.java:407)
	at org.eclipse.jface.action.ActionContributionItem.handleWidgetEvent
(ActionContributionItem.java:361)
	at org.eclipse.jface.action.ActionContributionItem.access$0
(ActionContributionItem.java:352)
	at 
org.eclipse.jface.action.ActionContributionItem$ActionListener.handleEvent
(ActionContributionItem.java:47)
	at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:75)
	at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:825)
	at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:1527)
	at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:1289)
	at org.eclipse.ui.internal.Workbench.runEventLoop(Workbench.java:1177)
	at org.eclipse.ui.internal.Workbench.run(Workbench.java:1160)
	at org.eclipse.core.internal.boot.InternalBootLoader.run
(InternalBootLoader.java:739)
	at org.eclipse.core.boot.BootLoader.run(BootLoader.java:462)
	at java.lang.reflect.Method.invoke(Native Method)
	at org.eclipse.core.launcher.Main.basicRun(Main.java:244)
	at org.eclipse.core.launcher.Main.run(Main.java:693)
	at org.eclipse.core.launcher.Main.main(Main.java:526)
Comment 1 Till Bay CLA 2002-06-05 10:56:15 EDT
Created attachment 1263 [details]
code.txt
Comment 2 Dirk Baeumer CLA 2002-06-05 12:03:10 EDT
Till, what is the original context (file, project) for the code snippet
Comment 3 Dirk Baeumer CLA 2002-06-05 12:03:59 EDT
This should normally not happen. The problem is that the position returned from 
code formatter doesn't map to a valid position in the string anymore.
Comment 4 Adam Kiezun CLA 2002-06-06 04:34:27 EDT
i saw the file too - positions were really out of synch (judging by what 
structured selection said)
Till, you still have the file around?
Comment 5 Till Bay CLA 2002-06-06 04:40:45 EDT
sent the file to Dirk
Comment 6 Dirk Baeumer CLA 2002-06-06 04:50:59 EDT
This is a formatter problem. Here is the scenario I use:

code to format:

private void eee(String string, TextBufferEditor tbe, String lineDelimiter, 
IMember member, String source, IScanner scanner) throws JavaModelException, 
CoreException{ x(); }

Formatter call:

String formattedCode= formatter.format(codeToFormat, 0, positions, 
lineSeparator);

positions is [165, 168]
lineSeparator is ascii 10

The result is a string of length 183 put the returned positions are [184, 187]. 
So they lay outside the string which causes the NPE since I don't handle this 
case.

Comment 7 Dirk Baeumer CLA 2002-06-06 04:51:55 EDT
Note that codeToFormat is one string with line delimiters.
Comment 8 Dirk Baeumer CLA 2002-06-06 05:16:26 EDT
Added guard code and fixup code if the positions seem to be bogus.
Comment 9 Dirk Baeumer CLA 2002-06-06 08:17:36 EDT
Moving to JDT/Core since it is a formatter bug.
Comment 10 Philipe Mulet CLA 2002-06-06 09:36:33 EDT
We did not change this code recently, it should have never work since day one.
Comment 11 Olivier Thomann CLA 2002-06-06 16:04:40 EDT
The problem only occurs if there is a double split of the string. In your example, there is a split on 
the throws keyword and on the commas in the parameters list.
Comment 12 Andreas Krüger CLA 2002-06-07 06:18:24 EDT
Dirk Baeumer says, over at bug 18997, that that bug is related to this one.
Comment 13 Olivier Thomann CLA 2002-06-07 15:31:50 EDT

*** This bug has been marked as a duplicate of 18997 ***