java.lang.NullPointerException 尝试使用异步任务获取 json

java.lang.NullPointerException while trying to get json using Async task

当我单击按钮时,我试图从 Url 获取 json,但出现空指针异常,不幸的是应用程序停止了这是我的 onCreate 方法。

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    autoCompView = (AutoCompleteTextView)findViewById(R.id.autocomplete_current);

 findViewById(R.id.shortestButton).setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            getUrl();//calling getUrl method for getting Url
        }
    });

这是我的 getUrl 方法,用于创建 url 并从 url 获取 json。

private void getUrl() {
    String result = null;
    String dest1 = list.get(0);
    String dest2 = list.get(1);
    String dest3 = list.get(2);
    String start = getAddress();

        String urls = "https://maps.googleapis.com/maps/api/distancematrix/json?";
        try {
            encodedString = urls + "origins="
                    + URLEncoder.encode(start, "utf8")+"|"
                    + URLEncoder.encode(dest1, "utf8")+"|"
                    + URLEncoder.encode(dest2, "utf8")+"|"
                    + URLEncoder.encode(dest3, "utf8")
                    +"&destinations="
                    + URLEncoder.encode(dest1, "utf8")+"|"
                    + URLEncoder.encode(dest2, "utf8")+"|"
                    + URLEncoder.encode(dest3, "utf8")+"|"
                    + URLEncoder.encode(start, "utf8")
                    +"&mode=driving"
                    +"&language = en-En"
                    +"&key="+API_KEY;
            encodedString = encodedString.replaceAll("%2C","+");
            encodedString = encodedString.replaceAll("\++", "+");

        }  

private class ConnectToUrlTask extends AsyncTask<String,Void,String> {

    @Override
    protected String doInBackground(String... uRl) {
        String data = "";

            HttpConnections httpConnections = new HttpConnections();
        try {
            data = httpConnections.readUrl(uRl[0]);
        } catch (IOException e) {
            e.printStackTrace();
        }

        return data;
    }

    @Override
    protected void onPostExecute(String s) {
      new UrlTasks().execute(s);
    }
}
private class UrlTasks extends AsyncTask<String,Void,String> {

    @Override
    protected String doInBackground(String... params) {
        String distance = null;
        JSONObject jobj = null;

        try {
            jobj = new JSONObject(params[0]);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        try {
            JSONArray jsonArray = jobj.getJSONArray("rows");
          // Log.d("jsonArray", String.valueOf(jsonArray));
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject object = jsonArray.getJSONObject(i);
                JSONArray ja2 = object.getJSONArray("elements");
                for (int j = 0; j < ja2.length(); j++) {
                    JSONObject object3 = ja2.getJSONObject(j);
                    distance = object3.getJSONObject("distance").getString("text");
                }
            }
           //Log.d("jsonArray", String.valueOf(jsonArray));
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return distance;
    }

    @Override
    protected void onPostExecute(String s) {
        Log.d("St*",s);
    }
}

这是我的错误日志。

 java.lang.RuntimeException: An error occured while executing doInBackground()
        at android.os.AsyncTask.done(AsyncTask.java:299)
        at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
        at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
        at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
        at java.util.concurrent.FutureTask.run(FutureTask.java:137)
        at android.os.AsyncTask$SerialExecutor.run(AsyncTask.java:230)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
        at java.lang.Thread.run(Thread.java:838)
 Caused by: java.lang.NullPointerException
        at com.example.wm.shortestpathdemo.MainActivity$UrlTasks.doInBackground(MainActivity.java:359)
        at com.example.wm.shortestpathdemo.MainActivity$UrlTasks.doInBackground(MainActivity.java:346)
        at android.os.AsyncTask.call(AsyncTask.java:287)

我在创建 url 时犯了错误。在方法 getUrl():

private void getUrl() {
String result = null;
String dest1 = list.get(0);
String dest2 = list.get(1);
String dest3 = list.get(2);
String start = getAddress();

    String urls = "https://maps.googleapis.com/maps/api/distancematrix/json?";
    try {
        encodedString = urls + "origins="
                + URLEncoder.encode(start, "utf8")+"|"
                + URLEncoder.encode(dest1, "utf8")+"|"
                + URLEncoder.encode(dest2, "utf8")+"|"
                + URLEncoder.encode(dest3, "utf8")
                +"&destinations="
                + URLEncoder.encode(dest1, "utf8")+"|"
                + URLEncoder.encode(dest2, "utf8")+"|"
                + URLEncoder.encode(dest3, "utf8")+"|"
                + URLEncoder.encode(start, "utf8")
                +"&mode=driving"
              // this is the mistake 
                +"&language = en-En"  
              //correct form is
                +"&language = en"
                +"&key="+API_KEY;
        encodedString = encodedString.replaceAll("%2C","+");
        encodedString = encodedString.replaceAll("\++", "+");

    }