Bug 144225 [FieldAssist] NullPointerException in ContentProposalAdapter$ContentProposalPopup.open()
I am using Eclipse 3.2RC6 and was playing around with the org.eclipse.jface.fieldassist package. I wanted to implement a simple auto-complete textfield with the ContentProposalAdapter class. But if the first letter which is entered in the textfield does not yield any proposals, the call to getSelectedProposal() in line 859 of ContentProposalAdapter.java returns null and therefore a NPE is thrown, rather than just not showing the popup at all. The following snippet shows this behaviour (just type any letter except "p"): import org.eclipse.jface.fieldassist.ContentProposalAdapter; import org.eclipse.jface.fieldassist.IContentProposal; import org.eclipse.jface.fieldassist.IContentProposalProvider; import org.eclipse.jface.fieldassist.TextContentAdapter; import org.eclipse.swt.SWT; import org.eclipse.swt.layout.FillLayout; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; import org.eclipse.swt.widgets.Text; public class ExampleSnippet { public static void main(String[] args) { Display display = new Display(); Shell shell = new Shell(display); shell.setLayout(new FillLayout()); Text text = new Text(shell, SWT.NONE); ContentProposalAdapter adapter = new ContentProposalAdapter(text, new TextContentAdapter(), new TextProposalProvider(), null, null); adapter.setProposalAcceptanceStyle(ContentProposalAdapter.PROPOSAL_REPLACE); shell.setSize(200, text.getSize().y); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); } } class TextProposalProvider implements IContentProposalProvider { private String str = "Proposal"; public IContentProposal[] getProposals(String contents, int position) { IContentProposal[] proposals; if (str.toLowerCase().startsWith(contents.toLowerCase())) { proposals = new IContentProposal[1]; proposals[0] = new IContentProposal() { public String getContent() { return str; } public int getCursorPosition() { return str.length(); } public String getDescription() { return null; } public String getLabel() { return null; } }; } else { proposals = new IContentProposal[0]; } return proposals; } }
bundles/org.eclipse.jface/src/org/eclipse/jface/fieldassist/ContentProposalAdapter.java
*         filtering of the proposals shown. <code>FILTER_NONE</code>
	 *         specifies that no filtering will occur in the content proposal
	 *         list as keys are typed. <code>FILTER_CUMULATIVE</code>
	 *         specifies that the content of the popup will be filtered by a
	 *         string containing all the characters typed since the popup has
	 *         been open. <code>FILTER_CHARACTER</code> specifies the content
	 *         of the popup will be filtered by the most recently typed
	 *         character. The default is <code>FILTER_NONE</code>.
	 */
	public int getFilterStyle() {
		return filterStyle;
	}
	/**
	 * Set the integer style that indicates how keystrokes affect the content of
	 * the proposal popup while it is open.
	 * 
	 * @param filterStyle
	 *            a constant indicating how keystrokes in the proposal popup
	 *            affect filtering of the proposals shown.
	 *            <code>FILTER_NONE</code> specifies that no filtering will
	 *            occur in the content proposal list as keys are typed.
	 *            <code>FILTER_CUMULATIVE</code> specifies that the content of
	 *            the popup will be filtered by a string containing all the
	 *            characters typed since the popup has been open.
	 *            <code>FILTER_CHARACTER</code> specifies the content of the
	 *            popup will be filtered by the most recently typed character.
	 */
	public void setFilterStyle(int filterStyle) {
		this.filterStyle = filterStyle;
	}