问题
I have a SWT combo which have one empty entry and some values. When I select the empty entry it does not fire any event. But I need to fire an update event. If I do call firePropertyChange() method, I always get the following Exception in the console.
In importMap , it has following values - { ,1,2,3,4} . The first value is an empty string.
I am binding the combo values in the Databindingscontext
IObservableValue observeSelectionCImportObserveWidget = WidgetProperties.selection().observe(cImport);
IObservableValue importModelObserveValue = BeanProperties.value("import").observe(model);
bindingContext.bindValue(observeSelectionCImportObserveWidget, importModelObserveValue, null, new UpdateValueStrategy(UpdateValueStrategy.POLICY_ON_REQUEST));
IObservableValue observeSingleSelectionIndexCImportObserveWidget = WidgetProperties.singleSelectionIndex().observe(cImport);
IObservableValue importIndexModelObserveValue = BeanProperties.value("importIndex").observe(model);
bindingContext.bindValue(observeSingleSelectionIndexCImportObserveWidget, importIndexModelObserveValue, null, null);
IObservableList itemsCImportObserveWidget = WidgetProperties.items().observe(cImport);
IObservableList importComboModelObserveList = BeanProperties.list("importCombo").observe(model);
bindingContext.bindList(itemsCImportObserveWidget, importComboModelObserveList, null, null);
public void setImport(String value){
if(value.equals(""))
return;
long index = 0;
for (Entry<Long, String> entry : importmap.entrySet()) {
if (entry.getValue().equals(value)) {
index = entry.getKey();
break;
}
}
if(ag.getImport() == index) return; // ag is a class which has getters and setters for Import.
ag.setImport(index);
setPriority(); // this method gets the value ag.getImport() and does some operation and updates the values in a text field
IsDirty();
}
public String getImport(){
if(ag.getImport() < 1)
return "";
return importMap.get(ag.getImport());
}
public int getImportIndex(){
if(ag.getImport() < 1)
return -1;
long index = 0;
for (Entry<Long, String> entry : importmap.entrySet()) {
if (entry.getKey() == ag.getImport()) {
return (int) index;
}
index++;
}
return -1;
}
public List<String> getImportCombo(){
List<String> importValues = new ArrayList<String>();
if(importMap != null && importmap.size() > 0)
importtValues.addAll(importmap.values());
return importValues;
}
This is the following exception I get whenever I select an item in combo box
WARN : 16:12:49,983 - TcLogger$IC_PrintStream.log:?
java.lang.NullPointerException
at org.eclipse.core.internal.databinding.beans.BeanPropertyHelper.writeProperty(BeanPropertyHelper.java:51)
at org.eclipse.core.internal.databinding.beans.BeanValueProperty.doSetValue(BeanValueProperty.java:51)
at org.eclipse.core.databinding.property.value.ValueProperty.setValue(ValueProperty.java:67)
at org.eclipse.core.internal.databinding.property.value.SimplePropertyObservableValue.doSetValue(SimplePropertyObservableValue.java:104)
at org.eclipse.core.databinding.observable.value.AbstractObservableValue.setValue(AbstractObservableValue.java:55)
at org.eclipse.core.databinding.observable.value.DecoratingObservableValue.setValue(DecoratingObservableValue.java:103)
at org.eclipse.core.databinding.observable.value.DecoratingObservableValue.setValue(DecoratingObservableValue.java:103)
at org.eclipse.core.databinding.UpdateValueStrategy.doSet(UpdateValueStrategy.java:486)
at org.eclipse.core.databinding.ValueBinding$4.run(ValueBinding.java:197)
at org.eclipse.core.databinding.observable.Realm$1.run(Realm.java:148)
at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:42)
at org.eclipse.core.databinding.observable.Realm.safeRun(Realm.java:152)
at org.eclipse.core.databinding.observable.Realm.exec(Realm.java:170)
at org.eclipse.core.databinding.ValueBinding$3.run(ValueBinding.java:188)
at org.eclipse.core.databinding.observable.Realm$1.run(Realm.java:148)
at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:42)
at org.eclipse.core.databinding.observable.Realm.safeRun(Realm.java:152)
at org.eclipse.core.databinding.observable.Realm.exec(Realm.java:170)
at org.eclipse.core.databinding.ValueBinding.doUpdate(ValueBinding.java:151)
at org.eclipse.core.databinding.ValueBinding.access$4(ValueBinding.java:140)
at org.eclipse.core.databinding.ValueBinding$1.handleValueChange(ValueBinding.java:45)
at org.eclipse.core.databinding.observable.value.ValueChangeEvent.dispatch(ValueChangeEvent.java:62)
at org.eclipse.core.databinding.observable.ChangeManager.fireEvent(ChangeManager.java:119)
at org.eclipse.core.databinding.observable.value.DecoratingObservableValue.fireValueChange(DecoratingObservableValue.java:55)
at org.eclipse.core.databinding.observable.value.DecoratingObservableValue.handleValueChange(DecoratingObservableValue.java:93)
at org.eclipse.core.databinding.observable.value.DecoratingObservableValue$1.handleValueChange(DecoratingObservableValue.java:67)
at org.eclipse.core.databinding.observable.value.ValueChangeEvent.dispatch(ValueChangeEvent.java:62)
at org.eclipse.core.databinding.observable.ChangeManager.fireEvent(ChangeManager.java:119)
at org.eclipse.core.databinding.observable.value.AbstractObservableValue.fireValueChange(AbstractObservableValue.java:71)
at org.eclipse.core.internal.databinding.property.value.SimplePropertyObservableValue.notifyIfChanged(SimplePropertyObservableValue.java:120)
at org.eclipse.core.internal.databinding.property.value.SimplePropertyObservableValue.access$1(SimplePropertyObservableValue.java:112)
at org.eclipse.core.internal.databinding.property.value.SimplePropertyObservableValue$2.run(SimplePropertyObservableValue.java:66)
at org.eclipse.core.databinding.observable.Realm$1.run(Realm.java:148)
at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:42)
at org.eclipse.core.databinding.observable.Realm.safeRun(Realm.java:152)
at org.eclipse.core.databinding.observable.Realm.exec(Realm.java:170)
at org.eclipse.core.internal.databinding.property.value.SimplePropertyObservableValue$1.handleEvent(SimplePropertyObservableValue.java:63)
at org.eclipse.core.databinding.property.NativePropertyListener.fireChange(NativePropertyListener.java:63)
at org.eclipse.jface.internal.databinding.swt.WidgetListener.handleEvent(WidgetListener.java:55)
at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1053)
at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:4066)
at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3657)
at org.eclipse.ui.internal.Workbench.runEventLoop(Workbench.java:2629)
at org.eclipse.ui.internal.Workbench.runUI(Workbench.java:2593)
at org.eclipse.ui.internal.Workbench.access$4(Workbench.java:2427)
at org.eclipse.ui.internal.Workbench$7.run(Workbench.java:670)
at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332)
at org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:663)
at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:149)
at com.teamcenter.rac.aifrcp.Application.runApplication(Unknown Source)
at com.teamcenter.rac.aifrcp.Application.start(Unknown Source)
at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:196)
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:110)
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:79)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:369)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:179)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:619)
at org.eclipse.equinox.launcher.Main.basicRun(Main.java:574)
at org.eclipse.equinox.launcher.Main.run(Main.java:1407)
at org.eclipse.equinox.launcher.Main.main(Main.java:1383)
回答1:
If you are using 'importIndex' you need to have a 'setImportIndex' method so that the changed value can be saved in the bean.
来源:https://stackoverflow.com/questions/34182634/empty-entry-in-the-swt-combo