I have a problem with android programming. I am new in this, so don’t be judgemental.
I built a login form and when I try to login, I get in the username textbox java.lang.NullPointerException. Here is the code:
import java.util.ArrayList;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.content.Context;
import android.content.Intent;
public class Login extends Activity {
EditText un,pw;
TextView error;
Button ok;
Button reg;
private static Context context;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
un=(EditText)findViewById(R.id.editText1);
pw=(EditText)findViewById(R.id.editText2);
ok=(Button)findViewById(R.id.button1);
reg=(Button)findViewById(R.id.button2);
ok.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
postParameters.add(new BasicNameValuePair("username", un.getText().toString()));
postParameters.add(new BasicNameValuePair("password", pw.getText().toString()));
String response = null;
try {
response = CustomHttpClient.executeHttpPost("<HTTP Request site>", postParameters);
String res=response.toString();
res= res.replaceAll("\\s+","");
if(res.equals("1")) {
error.setText("Welcome to ...");
Intent i = new Intent(context, Welcome.class);
startActivity(i);
}
else
error.setText("Sorry!! Incorrect Username or Password");
} catch (Exception e) {
un.setText(e.toString());
}
}
});
}
}
Is there any problem or a mistake that I made. Please help me. Or just give me some directions.
Thanks in advance
You’re getting an error because your TextView
erroris never initialized. Add the following line into youronCreate():