0
04-19 12:39:23.139 2933-2943/? E/art: Failed sending reply to debugger: Broken pipe
04-19 12:39:32.248 2933-2950/net.pushyfurball.currencyconversion E/Surface: getSlotFromBufferLocked: unknown buffer: 0xb4095db0
04-19 12:39:32.254 2933-2933/net.pushyfurball.currencyconversion E/AndroidRuntime: FATAL EXCEPTION: main
    Process: net.pushyfurball.currencyconversion, PID: 2933
    java.lang.NullPointerException: Attempt to invoke virtual method 'int org.json.JSONObject.length()' on a null object reference
    at net.pushyfurball.currencyconversion.MainActivity$JSONParse.onPostExecute(MainActivity.java:89)
    at net.pushyfurball.currencyconversion.MainActivity$JSONParse.onPostExecute(MainActivity.java:59)
    at android.os.AsyncTask.finish(AsyncTask.java:651)
    at android.os.AsyncTask.-wrap1(AsyncTask.java)
    at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:668)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:148)
    at android.app.ActivityThread.main(ActivityThread.java:5417)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

That's the error I get when I am trying to use the code as follows:

package net.pushyfurball.currencyconversion;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;

public class MainActivity extends Activity {
ListView list;
TextView code;
TextView name;
TextView rate;
Button Btngetdata;
ArrayList<HashMap<String, String>> exchangeRate = new ArrayList<HashMap<String, String>>();

//URL to get JSON Array
private static String url = "http://www.floatrates.com/daily/usd.json";

//JSON Node Names
private static final String TAG_CODE = "code";
private static final String TAG_NAME = "name";
private static final String TAG_RATE = "rate";

JSONObject currentRates = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);
    exchangeRate = new ArrayList<HashMap<String, String>>();

    Btngetdata = (Button)findViewById(R.id.getdata);
    Btngetdata.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            new JSONParse().execute();

        }
    });

}

private class JSONParse extends AsyncTask<String, String, JSONObject> {
    private ProgressDialog pDialog;
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        code = (TextView)findViewById(R.id.code);
        name = (TextView)findViewById(R.id.name);
        rate = (TextView)findViewById(R.id.rate);
        pDialog = new ProgressDialog(MainActivity.this);
        pDialog.setMessage("Getting Data ...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(true);
        pDialog.show();

    }

    @Override
    protected JSONObject doInBackground(String... args) {

        JSONParser jParser = new JSONParser();

        // Getting JSON from URL
        JSONObject json = jParser.getJSONFromUrl(url);
        return json;
    }
    @Override
    protected void onPostExecute(JSONObject json) {
        pDialog.dismiss();
        try {
            // Getting JSON Array from URL
            for(int i = 0; i < currentRates.length(); i++){
                JSONObject c = currentRates;

                // Storing  JSON item in a Variable
                String code = c.getString(TAG_CODE);
                String name = c.getString(TAG_NAME);
                String rate = c.getString(TAG_RATE);

                // Adding value HashMap key => value

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

                map.put(TAG_CODE, code);
                map.put(TAG_NAME, name);
                map.put(TAG_RATE, rate);

                exchangeRate.add(map);
                list=(ListView)findViewById(R.id.list);

                ListAdapter adapter = new SimpleAdapter(MainActivity.this, exchangeRate,
                        R.layout.list_v,
                        new String[] { TAG_CODE,TAG_NAME, TAG_RATE }, new int[] {
                        R.id.code,R.id.name, R.id.rate});

                list.setAdapter(adapter);
                list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

                    @Override
                    public void onItemClick(AdapterView<?> parent, View view,
                                            int position, long id) {
                        Toast.makeText(MainActivity.this, "You Clicked at "+exchangeRate.get(+position).get("name"), Toast.LENGTH_SHORT).show();

                    }
                });

            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

    }
}

}

I have the issue when trying to pull the file from http://www.floatrates.com/daily/usd.json (which you can see in the code). The only errors I can semi figure out is the fact that is happens at the currentRate.length() but not sure as to why. When my friend sent me the code after I had given up, he said that I would need to change the JSONArray to JSONObject because of the json file I am trying to pull.

DwB
  • 37,124
  • 11
  • 56
  • 82

2 Answers2

0

your JSONObject currentRates object is null referenced,

JSONObject currentRates = null;

if its value is null and you do this currentRates.length(); you will get a NPE

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
0

check this out:

JSONObject currentRates = null;

sets currentRates to null.

If you never set this reference to a non-null value, this code:

for(int i = 0; i < currentRates.length(); i++)

will always throw a NullPointerException.

Try setting currentRates.

DwB
  • 37,124
  • 11
  • 56
  • 82