Question

I am trying to run my main menu which creates a SharedPreference file which includes an ArrayList with the object Food which contains strings and doubles. while trying to run it the Application falls apart, this is my code:

package com.example.davidson;

import java.util.ArrayList;

import com.google.gson.Gson;

import android.support.v7.app.ActionBarActivity;
import android.support.v4.app.Fragment;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;

public class MainMenu extends ActionBarActivity implements OnClickListener {
Button btn1,btn2,btn3,btn4;
public ArrayList<Food>L=new ArrayList<Food>();
public static String filename="mySharedList";
SharedPreferences myFoodList;
public Editor editor=myFoodList.edit();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main_menu);    
    myFoodList=getSharedPreferences(filename,0);
    btn1=(Button)findViewById(R.id.button1);
    btn2=(Button)findViewById(R.id.button2);
    btn3=(Button)findViewById(R.id.button3);
    btn4=(Button)findViewById(R.id.button4);
    btn1.setOnClickListener(this);
    btn2.setOnClickListener(this);
    btn3.setOnClickListener(this);
    btn4.setOnClickListener(this);
    Createfoods();



}

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main_menu, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

/**
 * A placeholder fragment containing a simple view.
 */
public static class PlaceholderFragment extends Fragment {

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main_menu,
                container, false);
        return rootView;
    }
}

@Override
public void onClick(View v) {
    if(v.getId()==R.id.button1)
    {
        Intent openFoodSearch=new      Intent("com.example.davidson.FOODSEARCH");
        startActivity(openFoodSearch);
    }
    if(v.getId()==R.id.button2)
    {
        Intent openMyList=new Intent("com.example.davidson.MY_LIST");
        startActivity(openMyList);
    }
    if(v.getId()==R.id.button3)
    {
        Intent openRecommended=new     Intent("com.example.davidson.RECOMMENDED");
        startActivity(openRecommended);
    }
    if(v.getId()==R.id.button4)
    {
        Intent openAddItem=new Intent("com.example.davidson.ADDITEM");
        startActivity(openAddItem);
    }

    }
public void Createfoods()
{
Food f1=new Food("cucumber",50,5,4,3);
Food f2=new Food("tomato",50,3,2,1);
L.add(f1);
L.add(f2);
Gson gson=new Gson();
String json=gson.toJson(L);
editor.putString("myShardList",json);
editor.commit();

}
}

Below is the logcat

Shutting down VM
   threadid=1: thread exiting with uncaught exception (group=0xb3a87ba8)
    FATAL EXCEPTION: main
   Process: com.example.davidson, PID: 1157
    java.lang.RuntimeException: Unable to instantiate activity           ComponentInfo{com.example.davidson/com.example.davidson.MainMenu}:     java.lang.NullPointerException
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2121)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
    at android.app.ActivityThread.access$800(ActivityThread.java:135)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:136)
    at android.app.ActivityThread.main(ActivityThread.java:5017)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
    at dalvik.system.NativeStart.main(Native Method)
       Caused by: java.lang.NullPointerException
    at com.example.davidson.MainMenu.<init>(MainMenu.java:26)
    at java.lang.Class.newInstanceImpl(Native Method)
    at java.lang.Class.newInstance(Class.java:1208)
    at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2112)
    ... 11 more
       Sending signal. PID: 1157 SIG: 9
Was it helpful?

Solution

You're getting a NullPointerException because of these lines:

SharedPreferences myFoodList;
public Editor editor=myFoodList.edit();

You cannot call the edit() function of myFoodList yet because it has not been initialized and is null. You need to initialize it before you can call it, ideally in your onCreate() function.

You can figure this out by looking at your Logcat output, specifically these lines:

Caused by: java.lang.NullPointerException
  at com.example.davidson.MainMenu.<init>(MainMenu.java:26)

The logcat is telling you that your problem is on line 26 in MainMenu.java

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top