NullPointerException error

Put your problem here if it does not fit any of the other categories.

NullPointerException error

Postby flowreen » Wed Mar 06, 2013 11:11 am

Hello guys! I want to make suggestions(cities) from a column from a table from a DB to appear in my **AutoCompleteTextView** control from my Android device but I get
error in http connection java.lang.NullPointerException ,
error converting result java.lang.NullPointerException ,
error converting result org.json.JSONException: End of input at character 0 of

exactly when starting to type something. What am I doing wrong ?

I've noticed that while debugging the errors occur in line "nameValuePairs.add(new BasicNameValuePair("st",CompleteLocalitate.getText().toString())); "

Thanks very much for your help !

My Java code:

package com.example.fishing2;


import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import org.apache.http.HttpEntity;
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.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;


public class PartidaActivity extends Activity {


private ArrayAdapter<String> adapterlocalitate;
private AutoCompleteTextView CompleteLocalitate;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_partida);



//AUTOCOMPLETE LOCALITATE
adapterlocalitate = new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line);
AutoCompleteTextView CompleteLocalitate = (AutoCompleteTextView) findViewById(R.id.nume_localitate);
CompleteLocalitate.setAdapter(adapterlocalitate);
adapterlocalitate.setNotifyOnChange(true);
CompleteLocalitate.setThreshold(1);
CompleteLocalitate.addTextChangedListener(textChecker);
}

final TextWatcher textChecker = new TextWatcher() {
public void afterTextChanged(Editable s) {}

public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

public void onTextChanged(CharSequence s, int start, int before, int count)
{

adapterlocalitate.clear();

callPHP();


}
};

private void callPHP(){
String result = "";
InputStream is=null; {


try{
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("st",CompleteLocalitate.getText().toString()));

HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://192.168.3.159/wtf/test.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();


}catch(Exception e){
Log.e("log_tag", "Error in http connection "+e.toString());
}

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

result=sb.toString();
}catch(Exception e){
Log.e("log_tag", "Error converting result "+e.toString());


try{
JSONArray jArray = new JSONArray(result);
// JSONParser json_data = new JSONParser();

for (int i=0;i<jArray.length(); i++)
{
JSONObject json_data = jArray.getJSONObject(i);
adapterlocalitate.add(json_data.getString("nume_localitate"));
}

} catch(Exception e1){
Log.e("log_tag", "Error converting result "+e1.toString());

}
}
//<NameValuePair></NameValuePair>


}



}


}


My PHP file:
<?php
mysql_connect("192.168.3.159","florin","florin");
mysql_select_db("wtf");

$st = $_REQUEST['st'];

$q= mysql_query("SELECT * FROM localitati WHERE nume_localitate LIKE '%".$st."%' and localitateID<100");

while($e = mysql_fetch_assoc($q))
$output[]=$e;

print(json_encode($output));

mysql_close();
?>

And my manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.fishing2"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk android:minSdkVersion="3"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<activity
android:name="LoginActivity"
android:label="@string/app_name">
</activity>

<activity
android:name="IntroActivity"
android:label="Introducere">
</activity>

<activity
android:name="PartidaActivity"
android:label="Adauga o partida de pescuit">
</activity>

<activity
android:name="expandableFishesList"
android:label="@string/app_name">
</activity>

<activity android:name=".CustomTitleBar"
android:theme="@style/customTheme" >
</activity>

</application>

</manifest>
flowreen
Once Poster
Once Poster
 
Posts: 1
Joined: Wed Mar 06, 2013 10:55 am

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: Yahoo [Bot] and 23 guests