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


    }   
}

ERROR

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

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

dev2d
  • 4,245
  • 3
  • 31
  • 54

2 Answers2

0

You should indeed use AsyncTask for the networking part. Your app probably already crashes with a NetworkOnMainThread error follow this link for the usasge of AsyncTask.

But that besides, you are providing to little code to point to a reason for the NullPointerException. You should always check if an object is not null if it may occur it is.

try{
   //your code
}catch(NullPointerException ex){
   ex.printStackTrace();
}
TmKVU
  • 2,910
  • 2
  • 16
  • 30
0

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

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.

Community
  • 1
  • 1
Olavz
  • 139
  • 1
  • 6