JSONArray& JSONObject错误

时间:2016-04-19 17:47:53

标签: java json

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)

这是我在尝试使用代码时遇到的错误:

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

    }
}

}

我在尝试从http://www.floatrates.com/daily/usd.json(您可以在代码中看到)中提取文件时遇到问题。 我可以解决的唯一错误是在currentRate.length()发生的事实,但不确定为什么。 在我放弃之后,当我的朋友把代码发给我时, 他说我需要将JSONArray更改为JSONObject,因为我试图提取json文件。

2 个答案:

答案 0 :(得分:0)

您的JSONObject currentRates对象为空引用,

JSONObject currentRates = null;

如果其值为null并且您执行此操作currentRates.length();,则会获得NPE

答案 1 :(得分:0)

检查一下:

JSONObject currentRates = null;

将currentRates设置为null。

如果您从未将此引用设置为非null值,则此代码为:

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

将始终抛出NullPointerException。

尝试设置currentRates。