-2

I'm doing a school project to end my course, its a small program that interacts with php and mysql database to retrive data and import it to the android app.

I'm getting a fatal exception / null pointer exception and I don't know why.

If you require me to post more files from the program plz let me know Thanks in advance

Receitas.Java

public class receitas extends ActionBarActivity {

String myJSON;

private static final String TAG_RESULTS="result";
private static final String TAG_ID_RECIPE="id_recipe";
private static final String TAG_TITLE="title";
private static final String TAG_INGREDIENTS="ingredients";
private static final String TAG_STEPS="steps";

JSONArray recipes = null;

ArrayList<HashMap<String, String>> recipelist;

ListView list;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_receitas);
    list = (ListView) findViewById(R.id.ListView);
    recipelist = new ArrayList<HashMap<String, String>>();
    getData();
}

protected void showList(){
    try{
        JSONObject jsonObj = new JSONObject(myJSON);
        recipes = jsonObj.getJSONArray(TAG_RESULTS);

        for(int i=0;i<recipes.length();i++){
            JSONObject c = recipes.getJSONObject(i);
            String id_recipe = c.getString(TAG_ID_RECIPE);
            String title = c.getString(TAG_TITLE);
            String ingredients = c.getString(TAG_INGREDIENTS);
            String steps = c.getString(TAG_STEPS);

            HashMap<String, String> recipes = new HashMap<String, String>();

            recipes.put(TAG_ID_RECIPE,id_recipe);
            recipes.put(TAG_TITLE,title);
            recipes.put(TAG_INGREDIENTS,ingredients);
            recipes.put(TAG_STEPS,steps);

            recipelist.add(recipes);
        }

        ListAdapter adapter = new SimpleAdapter(
                receitas.this, recipelist, R.layout.list_item,
                new String[]{TAG_ID_RECIPE,TAG_TITLE},
                new int[]{R.id.id_recipe, R.id.title}
        );

        list.setAdapter(adapter);
    } catch (JSONException e) {
        e.printStackTrace();
    }
}

public void getData(){
    class GetDataJSON extends AsyncTask<String, Void, String>{

        @Override
        protected String doInBackground(String... params) {
            DefaultHttpClient httpclient = new DefaultHttpClient(new BasicHttpParams());
            HttpPost httppost = new HttpPost("http://localhost:8080/android/get-recipe-all.php");

            // Depends on your web service
            httppost.setHeader("Content-type", "application/json");

            InputStream inputStream = null;
            String result = null;
            try {
                HttpResponse response = httpclient.execute(httppost);
                HttpEntity entity = response.getEntity();

                inputStream = entity.getContent();
                // json is UTF-8 by default
                BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
                StringBuilder sb = new StringBuilder();

                String line = null;
                while ((line = reader.readLine()) != null)
                {
                    sb.append(line + "\n");
                }
                result = sb.toString();
            } catch (Exception e) {
                // Oops
            }
            finally {
                try{if(inputStream != null)inputStream.close();}catch(Exception squish){}
            }
            return result;
        }

        @Override
        protected void onPostExecute(String result){
            myJSON=result;
            showList();
        }
    }
    GetDataJSON g = new GetDataJSON();
    g.execute();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_receitas, 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();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}}

Here is my logcat Logcat

07-16 14:25:39.493  20431-20431/com.example.kuro.cheffridge E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NullPointerException
        at org.json.JSONTokener.nextCleanInternal(JSONTokener.java:116)
        at org.json.JSONTokener.nextValue(JSONTokener.java:94)
        at org.json.JSONObject.<init>(JSONObject.java:154)
        at org.json.JSONObject.<init>(JSONObject.java:171)
        at com.example.kuro.cheffridge.receitas.showList(receitas.java:55)
        at com.example.kuro.cheffridge.receitas$1GetDataJSON.onPostExecute(receitas.java:127)
        at com.example.kuro.cheffridge.receitas$1GetDataJSON.onPostExecute(receitas.java:88)
        at android.os.AsyncTask.finish(AsyncTask.java:631)
        at android.os.AsyncTask.access$600(AsyncTask.java:177)
        at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:4745)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
        at dalvik.system.NativeStart.main(Native Method)
Kuro
  • 3
  • 3
  • 1
    possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – SomeJavaGuy Jul 16 '15 at 14:35
  • `String myJSON;` is declared on line 3, but it is never assigned to. Thus, it is `null` when it is passed to the `JSONObject` constructor. – Chad Nouis Jul 16 '15 at 14:38

1 Answers1

1

Your String myJSON object is created but it is a null object. You never assign an actual value to it. Then when you try to use it to create a JSONObject, the null pointer exception is thrown.

mattfred
  • 2,689
  • 1
  • 22
  • 38