0

I am creating a restaurant app that requires an expandable listview to show a menu for the waiter to take a customer's menu. The app is crashing and I think it has something to do with the code below:

public class Menu extends AppCompatActivity {

     HashMap < String, List < String >> Food_menu;
     List < String > Food_list;
     ExpandableListView Exp_list;
     FoodMenuAdapter adapter;

     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);

         Exp_list = (ExpandableListView) findViewById(R.id.exp_list);
         Food_menu = DataProvider.getInfo();
         Food_list = new ArrayList < String > (Food_menu.keySet());
         adapter = new FoodMenuAdapter(this, Food_menu, Food_list);
         Exp_list.setAdapter(adapter);


     }
 }

When I debugged the app, it said that:

Exp_list
Food_menu
Food_list 

were all coming back as null. I'm new to android studios and I'm not sure how to remedy the problem. Any help would be greatly appreciated, thanks.

Logcat:

04-11 22:05:53.841 1960-1960/com.example.ben.restaurantapp E/AndroidRuntime: FATAL EXCEPTION: main
                                                                         Process: com.example.ben.restaurantapp, PID: 1960
                                                                         java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.ben.restaurantapp/com.example.ben.restaurantapp.Menu}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ExpandableListView.setAdapter(android.widget.ExpandableListAdapter)' on a null object reference
                                                                             at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
                                                                             at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
                                                                             at android.app.ActivityThread.access$800(ActivityThread.java:144)
                                                                             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
                                                                             at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                             at android.os.Looper.loop(Looper.java:135)
                                                                             at android.app.ActivityThread.main(ActivityThread.java:5221)
                                                                             at java.lang.reflect.Method.invoke(Native Method)
                                                                             at java.lang.reflect.Method.invoke(Method.java:372)
                                                                             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
                                                                             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
                                                                          Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ExpandableListView.setAdapter(android.widget.ExpandableListAdapter)' on a null object reference
                                                                             at com.example.ben.restaurantapp.Menu.onCreate(Menu.java:38)
                                                                             at android.app.Activity.performCreate(Activity.java:5937)
                                                                             at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
                                                                             at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
                                                                             at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360) 
                                                                             at android.app.ActivityThread.access$800(ActivityThread.java:144) 
                                                                             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278) 
                                                                             at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                             at android.os.Looper.loop(Looper.java:135) 
                                                                             at android.app.ActivityThread.main(ActivityThread.java:5221) 
                                                                             at java.lang.reflect.Method.invoke(Native Method) 
                                                                             at java.lang.reflect.Method.invoke(Method.java:372) 
                                                                             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899) 
                                                                             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) 
IonaMc
  • 45
  • 7
  • Are you getting an error or something? How were you debugging? If it was through the stack trace could you post your LogCat? – zgc7009 Apr 11 '16 at 21:56
  • The app crashes when i try to open the activity the expandable list view is on – IonaMc Apr 11 '16 at 22:03
  • Edit: logcat has been added – IonaMc Apr 11 '16 at 22:05
  • Exact duplicate of [NPE while using HashMap to populate ListView](http://stackoverflow.com/questions/36571025/npe-while-using-hashmap-to-populate-listview) – Sufian Apr 12 '16 at 12:25

2 Answers2

1

I think the second parameter of your FoodMenuAdapter has to be a resource layout file.

For example:

    Food Food_menu = DataProvider.getInfo();
    adapter = new FoodMenuAdapter(this, R.layout.your_xml_layout_here, Food_list);
    Exp_list = (ExapandableListView) findViewById(R.id.exp_list);
    Exp_list.setAdapter(adapter);

Where your_xml_layout_here is the layout that all the information will be displayed on. Keep in mind that an adapter is the middleman of information and what you see on your application screen.

Dane Lowrey
  • 170
  • 1
  • 10
1

From your logcat you can see the exact problem:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ExpandableListView.setAdapter(android.widget.ExpandableListAdapter)' on a null object reference

which means that at the point you try to use setAdapter method on the Exp_List, the said variable is null.

What you can do from here on is to check if your layout activity_main actually contains an ExpandableListView with an id of exp_list (maybe post your layout?).

Your question should not be android studio null error as the null has nothing to do with Android Studio - it comes from the programming language, in this specific case - Java.

You can learn how to fix NullPointerExceptions here.

Nikola
  • 2,093
  • 3
  • 22
  • 43
  • When you say my activity_main, do you mean the activity that I have posted? – IonaMc Apr 11 '16 at 22:16
  • When I say `check your layout activity_main` I mean your layout (xml) file, which you use here: `setContentView(R.layout.activity_main);` – Nikola Apr 11 '16 at 22:18
  • I'm not sure what the setContentView(R.layout.activity_main); means on that activity as the activity im using is the menu activity. Im new to android studios and im still getting used to it and finding out new things. How would I go about fixing the null error? Im really lost – IonaMc Apr 11 '16 at 22:28