1
votes

I'm getting the following error when trying to run the restaurant app in eclipse:

java.lang.NullPointerException at com.myrestaurant.app.ui.MainMenuForm.lambda$0(MainMenuForm.java:73) at com.codename1.ui.util.EventDispatcher.fireSelectionSync(EventDispatcher.java:321) at com.codename1.ui.util.EventDispatcher.fireSelectionEvent(EventDispatcher.java:402) at com.codename1.ui.list.DefaultListModel.setSelectedIndex(DefaultListModel.java:181) at com.codename1.ui.List.initComponentImpl(List.java:329) at com.codename1.ui.Container.initComponentImpl(Container.java:979) at com.codename1.ui.Container.insertComponentAtImpl(Container.java:715) at com.codename1.ui.Container.insertComponentAt(Container.java:687) at com.codename1.ui.Container.addComponent(Container.java:641) at com.codename1.ui.Form.addComponent(Form.java:1207) at com.codename1.ui.Container.add(Container.java:281) at com.myrestaurant.app.ui.BaseForm.init(BaseForm.java:102) at com.myrestaurant.app.ui.BaseForm.(BaseForm.java:34) at com.myrestaurant.app.ui.MainMenuForm.(MainMenuForm.java:43) at com.myrestaurant.app.ui.BaseForm.showMainMenuForm(BaseForm.java:156) at 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) at com.codename1.impl.javase.Executor$1$1.run(Executor.java:123) at com.codename1.ui.Display.processSerialCalls(Display.java:1154) at com.codename1.ui.Display.mainEDTLoop(Display.java:971) at com.codename1.ui.RunnableWrapper.run(RunnableWrapper.java:120) at com.codename1.impl.CodenameOneThread.run(CodenameOneThread.java:176)

Any help please?

The method is the following:

@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
That is a NullPointerException at MainMenuForm.java:73. We don't have the app, so we can't help you. Provide the code in question. - David G
sorry forgot to add the code. I added the method. Thanks - Spring dev

2 Answers

1
votes

Remove the "***" characters in the list listener ... Seems that one of this variable is null :

  • currentCategory
  • dishesContainer
  • d
  • d.category
0
votes

This was fixed in later iterations of the app as the course progresses. This is the current code for this block:

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);
        }
    }
}

At this time I haven't finished uploading everything but I'm working on that.