在eclipse

时间:2017-07-04 11:03:51

标签: codenameone

尝试在eclipse中运行餐厅应用程序时出现以下错误:

显示java.lang.NullPointerException     在com.myrestaurant.app.ui.MainMenuForm.lambda $ 0(MainMenuForm.java:73)     在com.codename1.ui.util.EventDispatcher.fireSelectionSync(EventDispatcher.java:321)     在com.codename1.ui.util.EventDispatcher.fireSelectionEvent(EventDispatcher.java:402)     at com.codename1.ui.list.DefaultListModel.setSelectedIndex(DefaultListModel.java:181)     在com.codename1.ui.List.initComponentImpl(List.java:329)     在com.codename1.ui.Container.initComponentImpl(Container.java:979)     在com.codename1.ui.Container.insertComponentAtImpl(Container.java:715)     at com.codename1.ui.Container.insertComponentAt(Container.java:687)     在com.codename1.ui.Container.addComponent(Container.java:641)     在com.codename1.ui.Form.addComponent(Form.java:1207)     在com.codename1.ui.Container.add(Container.java:281)     在com.myrestaurant.app.ui.BaseForm.init(BaseForm.java:102)     在com.myrestaurant.app.ui.BaseForm。(BaseForm.java:34)     在com.myrestaurant.app.ui.MainMenuForm。(MainMenuForm.java:43)     在com.myrestaurant.app.ui.BaseForm.showMainMenuForm(BaseForm.java:156)     在com.myrestaurant.app.MyRestaurant.start(MyRestaurant.java:48)     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)     at java.lang.reflect.Method.invoke(Method.java:498)     在com.codename1.impl.javase.Executor $ 1 $ 1.run(Executor.java:123)     在com.codename1.ui.Display.processSerialCalls(Display.java:1154)     在com.codename1.ui.Display.mainEDTLoop(Display.java:971)     在com.codename1.ui.RunnableWrapper.run(RunnableWrapper.java:120)     在com.codename1.impl.CodenameOneThread.run(CodenameOneThread.java:176)

请帮忙吗?

方法如下:

@Override
protected List<String> createCategoryList() {
    if(Restaurant.getInstance().
                menu.get().
                categories.size() == 0) {
        categoryModel = new DefaultListModel<>("Loading...");
    } else {
        categoryModel = new DefaultListModel<>(
                Restaurant.getInstance().
                    menu.get().
                    categories.asList());
    }
    List<String> l = new List<String>(categoryModel) {
        @Override
        protected boolean shouldRenderSelection() {
            return true;
        }
    };
    ((DefaultListCellRenderer<String>)l.getRenderer()).setAlwaysRenderSelection(true);
    l.setIgnoreFocusComponentWhenUnfocused(false);
    l.setOrientation(List.HORIZONTAL);
    l.setFixedSelection(List.FIXED_CENTER);
    l.addSelectionListener((i, ii) -> {
        if(currentCategory != l.getSelectedItem()) {
            currentCategory = l.getSelectedItem();
            for(Component c : dishesContainer) {
                Dish d = (Dish)c.getClientProperty("dish");
                ***boolean hidden = d.category.get().equals(currentCategory);***
                c.setHidden(hidden);
                c.setVisible(!hidden);
            }
            dishesContainer.animateLayout(150);
        }
    });
    return l;
}

2 个答案:

答案 0 :(得分:1)

删除列表侦听器中的“***”字符... 似乎这个变量之一是null:

  • currentCategory
  • dishesContainer
  • d
  • d.category

答案 1 :(得分:0)

随着课程的进展,这在应用程序的后续迭代中得到修复。这是该块的当前代码:

for(Component c : dishesContainer) {
    Dish d = (Dish)c.getClientProperty("dish");
    if(d != null) {
        String cat = d.category.get();
        if(cat != null) {
            boolean hidden = cat.equals(currentCategory);
            c.setHidden(hidden);
            c.setVisible(!hidden);
        }
    }
}

此时我还没有上传所有内容,但我正在努力。