线 - > JSONArray jArray =新JSONArray(结果);给空指针异常 [英] Line -> JSONArray jArray=new JSONArray(result); giving nullpointer exception

查看:447
本文介绍了线 - > JSONArray jArray =新JSONArray(结果);给空指针异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

package com.example.fyptrialapp;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.StrictMode;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

public class SearchByName extends Activity{

    HttpPost httppost;
    StringBuffer buffer;
    HttpResponse response;
    HttpClient httpclient;
    InputStream inputStream=null;
    Intent i;

    //public static final String recipes[]=new String[]{"Almond-Sheera","Bhel","Bread-Pizzas","Carrot-Pickle","Carrot-Relish"};
     String recipes[];

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.search_recipe);

        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);

        Button b2=(Button)findViewById(R.id.bSearchByName);

        try{

            httpclient = new DefaultHttpClient();
            httppost = new HttpPost("http://10.0.2.2/fypTrial2/searchByName.php");
            response = httpclient.execute(httppost);    // Execute HTTP Post Request
            inputStream = response.getEntity().getContent();
            String result=null;

            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1"), 8);
            StringBuilder sb = new StringBuilder();
            String line=null;
            while((line=reader.readLine())!= null){
                sb.append(line+"\n");
            }

            inputStream.close();
            result=sb.toString();

            JSONArray jArray=new JSONArray(result);// this statment gives error

            for(int i=0;i<jArray.length();i++){
                JSONObject json=jArray.getJSONObject(i);
                recipes[i]=json.getString("name");
            }
        }
        catch(Exception e){

            Log.e("log_tag3", "Error convering result"+e.toString());

        }

        ArrayAdapter<String> ac=new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line,recipes);
            AutoCompleteTextView tv=(AutoCompleteTextView) findViewById(R.id.autoCompleteTextView2);
            tv.setAdapter(ac);
        b2.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
            Intent i=new Intent("com.example.practice.Search_Result");
            startActivity(i);
            }
        });     


    }   
}

错误

java.nullpointerexception。错误转换结果variable.And的
  致命主要误差

java.nullpointerexception. Error converting result variable.And the fatal main error

如果我想使用ASnk任务,我应该怎么写吧。

If i want to use ASnk task how should i write it.

推荐答案

JSON语法从来没有开始作为一个数组。这可能是因为它{然后按[开始,但从来没有直接到[。这就是为什么尝试转换的结果JSONArray将无法工作。你应该为此首先尝试获取对象,那么数组元素。

JSON syntax never starts as an array. It could be that it starts with { followed by [, but never straight to [. This is why trying to convert your results to JSONArray will not work. You should therefor try to get the object first, then the array elements.

JSONObject jo = new JSONObject(result);
JSONArray jArray = jo.getJSONArray("keyIdentifyer");

如果你没有数组键identifyer,你可以尝试迭代或获得的第一个元素。当按键没有present见上迭代这个帖子answear

If you dont have the array key identifyer, you could try to iterate or get the first element. See this post answear on iterations when no keys are present.

这篇关于线 - &GT; JSONArray jArray =新JSONArray(结果);给空指针异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆