我第一次尝试使用BoundTableModel
,当我尝试设置属性标签时,我得到了NullPointerException
。
[EDT] 0:0:8,239 - Exception: java.lang.NullPointerException - null
java.lang.NullPointerException
at com.codename1.properties.PropertyBase.putClientProperty(PropertyBase.java:131)
at com.codename1.properties.PropertyBase.setLabel(PropertyBase.java:192)
at our.app.forms.UnitsForm.<init>(UnitsForm.java:54)
NPE抛出在此代码块中,但我不明白为什么。 properties
是public
final
,并且在UserHistory
中实例化,但是没有在其中设置标签。最好稍后设置它们,因为它们需要由我们的TranslationManager
翻译。
UiBinding ui = new UiBinding();
List<UserHistory> histories = RestManager.getHistory();
UserHistory prototype = new UserHistory();
prototype.dateCreate.setLabel("Date");
prototype.balanceUpdate.setLabel("Variation");
prototype.action.setLabel("Action");
UiBinding.BoundTableModel tb = ui.createTableModel(histories, prototype);
tb.setColumnOrder(prototype.dateCreate, prototype.balanceUpdate, prototype.action);
Table table = new Table(tb);
我不知道setLabel(String)
中的Property
如何抛出NullPointerException
,知道吗?
编辑:这是失败的类,第一个是AbstractEntity
:
public abstract class AbstractEntity implements Serializable, PropertyBusinessObject
{
public final LongProperty<AbstractEntity> id = new LongProperty<>("id");
public final IntProperty<AbstractEntity> version = new IntProperty<>("version");
public final Property<Date, AbstractEntity> dateCreate = new Property<>("dateCreate", Date.class);
public final Property<Date, AbstractEntity> dateUpdate = new Property<>("dateUpdate", Date.class);
protected List<PropertyBase> getPropertyList()
{
List<PropertyBase> list = new ArrayList<>();
list.add(id);
list.add(version);
list.add(dateCreate);
list.add(dateUpdate);
return list;
}
protected List<PropertyBase> getExcludePropertyList()
{
List<PropertyBase> list = new ArrayList<>();
return list;
}
@Override
public PropertyIndex getPropertyIndex()
{
PropertyBase[] properties = getPropertyList().toArray(new PropertyBase[getPropertyList().size()]);
PropertyIndex index = new PropertyIndex(this, getName(), properties);
for(PropertyBase excluded : getExcludePropertyList())
{
index.setExcludeFromJSON(excluded, true);
index.setExcludeFromMap(excluded, true);
}
return index;
}
}
这是我尝试在表格中显示的子类:
public class UserHistory extends AbstractEntity
{
public final Property<User, UserHistory> user = new Property<>("user", User.class);
public final DoubleProperty<UserHistory> balanceUpdate = new DoubleProperty<>("balanceUpdate");
public final Property<String, UserHistory> action = new Property<>("action");
public UserHistory() {}
@SuppressWarnings("rawtypes")
protected List<PropertyBase> getPropertyList()
{
List<PropertyBase> list = super.getPropertyList();
list.add(user);
list.add(balanceUpdate);
list.add(action);
return list;
}
}
答案 0 :(得分:1)
这行不通。
您的方法很有趣,但是它的行为与常规实现有一个显着的不同:索引是惰性创建的。因此,并非所有情况下实际上都不会执行索引完成的初始化工作。
更糟糕的是,每次调用get方法时都会创建多个索引,这也是有问题的。
“可能”起作用的东西是这样的:
public abstract class AbstractEntity implements Serializable, PropertyBusinessObject
{
public final LongProperty<AbstractEntity> id = new LongProperty<>("id");
public final IntProperty<AbstractEntity> version = new IntProperty<>("version");
public final Property<Date, AbstractEntity> dateCreate = new Property<>("dateCreate", Date.class);
public final Property<Date, AbstractEntity> dateUpdate = new Property<>("dateUpdate", Date.class);
private PropertyIndex index;
protected AbstractEntity() {
getPropertyIndex();
}
protected List<PropertyBase> getPropertyList()
{
List<PropertyBase> list = new ArrayList<>();
list.add(id);
list.add(version);
list.add(dateCreate);
list.add(dateUpdate);
return list;
}
protected List<PropertyBase> getExcludePropertyList()
{
List<PropertyBase> list = new ArrayList<>();
return list;
}
@Override
public PropertyIndex getPropertyIndex()
{
if(idx == null) {
PropertyBase[] properties = getPropertyList().toArray(new PropertyBase[getPropertyList().size()]);
index = new PropertyIndex(this, getName(), properties);
for(PropertyBase excluded : getExcludePropertyList())
{
index.setExcludeFromJSON(excluded, true);
index.setExcludeFromMap(excluded, true);
}
}
return index;
}
}
下面的原始答案:
如果您忘记将属性添加到索引对象,则会发生这种情况。在这种情况下,父对象为null,则无法设置元数据。