线 - > JSONArray jArray = new JSONArray(result);给出nullpointer异常 [英] Line -> JSONArray jArray=new JSONArray(result); giving nullpointer exception

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

问题描述

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。转换结果变量时出错。
致命主要错误

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");

如果没有数组键识别器,可以尝试迭代或获取第一个元素。不存在任何键时,请参阅此帖子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 = new JSONArray(result);给出nullpointer异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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