java.lang.NullPointerException Erro ao usar Buffered

时间:2013-11-11 11:25:39

标签: android json http buffer

Ao tentar执行classe ConectPhp

Meu LogCat traz o seguinte erro:

Erro ao converter resultado java.lang.NullPointerException

public class ConectPhp extends ListActivity {

public static InputStream inpStrin = null;
String result = "";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);




    ArrayList<NameValuePair> parametros = new ArrayList<NameValuePair>();
    parametros.add(new BasicNameValuePair("DATA", "2011-11-05"));



    try{


        HttpClient httpcliente = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://192.168.254.15/mobile/ConexaoWcrm.php/");
        httppost.setEntity(new UrlEncodedFormEntity(parametros));
        HttpResponse response = httpcliente.execute(httppost);
        HttpEntity entity = response.getEntity();

    } catch (Exception e){
        Log.e("log_tag", "Erro em conectar a "+e.toString());
    }

    try{
        BufferedReader reader = new BufferedReader(new InputStreamReader(inpStrin, "iso-8859-1"), 8);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while((line = reader.readLine()) != null){
            sb.append(line+"\n");
        }
        inpStrin.close();
        result=sb.toString();
    }catch (Exception e){
        Log.e("log_tag","Erro ao converter resultado"+e.toString());
    }



    try{

        JSONArray jArray = new JSONArray(result);
        String[] clientes = new String[jArray.length()];
        for(int i=0; i<jArray.length();i++){
            JSONObject json_data = jArray.getJSONObject(i);
            clientes[i]=json_data.getString("clientnome");
        }


        this.setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, clientes));
    }catch(Exception e){
        Log.e("log_tag","Erro ao popular os dados "+e.toString());
    }
    setContentView(R.layout.conectphp);


}}

Estrou testandoumaconexãocomum banco de dados externo para listra seus dados na tela do Android。

1 个答案:

答案 0 :(得分:0)

您尚未向inpStrin分配任何内容。另外,您应将HttpClient代码移至AsyncTask,否则可能会NetworkOnMainThreadException