I am working on an Android project in that i need to send the login data to an online database.
For that asynctask class is used.
but i got innerset exceptions in logcat and a force close.
Here is my code
import java.util.ArrayList;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class LoginActivity extends Activity {
EditText un,pw;
Button ok;
String res;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
un=(EditText)findViewById(R.id.u_name);
pw=(EditText)findViewById(R.id.passwd);
ok=(Button)findViewById(R.id.btnlogin);
ok.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
GetData task = new GetData();
task.execute();
}
});
}
private class GetData extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... urls) {
ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
postParameters.add(new BasicNameValuePair("username", un.getText().toString()));
postParameters.add(new BasicNameValuePair("password", pw.getText().toString()));
//String valid = "1";
String response = null;
try {
response = CustomHttpClient.executeHttpPost("http://127.0.0.1/test_login/check.php", postParameters);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
res=response.toString();
// res = res.trim();
res= res.replaceAll("\\s+","");
//error.setText(res);
return response;
}
protected void onPostExecute(String result)
{
result = res;
if(result.equals("1"))
{
Toast toast = Toast.makeText(LoginActivity.this,
"LoginSuccess", Toast.LENGTH_LONG);
toast.show();
Intent n = new Intent(getApplicationContext(),TestActivity.class);
startActivity(n);
}
else
{
Toast toast = Toast.makeText(LoginActivity.this,
"Login failed", Toast.LENGTH_LONG);
toast.show();
}
}
}
}
I got the following logcat errors
10-11 23:43:10.819: E/AndroidRuntime(1335): FATAL EXCEPTION: AsyncTask #1
10-11 23:43:10.819: E/AndroidRuntime(1335): java.lang.RuntimeException: An error occured while executing doInBackground()
10-11 23:43:10.819: E/AndroidRuntime(1335): at android.os.AsyncTask$3.done(AsyncTask.java:278)
10-11 23:43:10.819: E/AndroidRuntime(1335): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
10-11 23:43:10.819: E/AndroidRuntime(1335): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
10-11 23:43:10.819: E/AndroidRuntime(1335): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
10-11 23:43:10.819: E/AndroidRuntime(1335): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
10-11 23:43:10.819: E/AndroidRuntime(1335): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
10-11 23:43:10.819: E/AndroidRuntime(1335): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
10-11 23:43:10.819: E/AndroidRuntime(1335): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
10-11 23:43:10.819: E/AndroidRuntime(1335): at java.lang.Thread.run(Thread.java:856)
10-11 23:43:10.819: E/AndroidRuntime(1335): Caused by: java.lang.NullPointerException
10-11 23:43:10.819: E/AndroidRuntime(1335): at com.oranz.epark.LoginActivity$GetData.doInBackground(LoginActivity.java:55)
10-11 23:43:10.819: E/AndroidRuntime(1335): at com.oranz.epark.LoginActivity$GetData.doInBackground(LoginActivity.java:1)
10-11 23:43:10.819: E/AndroidRuntime(1335): at android.os.AsyncTask$2.call(AsyncTask.java:264)
10-11 23:43:10.819: E/AndroidRuntime(1335): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
10-11 23:43:10.819: E/AndroidRuntime(1335): ... 5 more
The problem is with your response String being null.
You are forcing the code to be run even after try catch is executed. So if the executeHttpPost method gets into some kind of problem, ur response string will be null and in the next line you are doing,
where repsonse is actually null and hitting the NPE.
EDIT
Change your try catch like this,
And postExecute,