代码之家  ›  专栏  ›  技术社区  ›  Gauvain Klug

属性的setLabel导致NPE

  •  1
  • Gauvain Klug  · 技术社区  · 6 年前

    我在试这个 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 空指针异常 你知道吗?

    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;
        }
    }
    
    1 回复  |  直到 6 年前
        1
  •  1
  •   Shai Almog    6 年前

    这行不通。

    您的方法很有趣,但它的行为与普通实现有一个显著的不同:索引是惰性创建的。因此,索引所做的初始化工作并不是在所有情况下都会发生。

    更糟糕的是,每次调用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;
         }
    }
    

    原始答复如下: