why i get the exception in android
I have written an Android application which will call a REST Web Service. Firstly, I tried an Android application with variable is fixed. I can successfully call the Web Service .
But when I integrated a GUI in the Android application and assigned value in variable, at runtime I get the following exception:
java.lang.reflect.InvocationTargetException
java.lang.NullPointerException
Why do I get the exception?
My program code is :
public class testing extends Activity {
private EditText txturl,foldername,metadata,Username,Password;
private TextView textview;
private Button btnok,btncancel;
private String url,foldname,Metadata;
private String username,password;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnok = (Button)findViewById(R.id.btncrtfold);
btncancel=(Button)findViewById(R.id.btnreset);
btnok.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
txturl=(EditText)findViewById(R.id.txturl);
Username=(EditText)findViewById(R.id.txtloginname);
Password=(EditText)findViewById(R.id.txtpassword);
foldername = (EditText)findViewById(R.id.txtfoldername);
metadata=(EditText)findViewById(R.id.txtmetadata);
foldname=foldername.getText().toString();
Metadata=metadata.getText().toString();
username=Username.getText().toString();
url=txturl.getText().toString();
password=Password.getText().toString();
//HttpRequestInterceptor preemptiveAuth=login();
String result=doprocess(url,username,password,Metadata,foldname);
textview.setText(result);
}});
}
public String doprocess(String url,String username,String password,String metadata,String fold){
HttpResponse response=null;
String content=null;
//public static void login() {
HttpRequestInterceptor preemptiveAuth = new HttpRequestInterceptor() {
@Override
public void process(HttpRequest request,
HttpContext context) throws HttpException,
IOException {
AuthState authState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE);
CredentialsProvider credsProvider = (CredentialsProvider) context.getAttribute(
ClientContext.CREDS_PROVIDER);
HttpHost targetHost = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
if (authState.getAuthScheme() == null) {
AuthScope authScope = new AuthScope(targetHost.getHostName(), targetHost.getPort());
Credentials creds = credsProvider.getCredentials(authScope);
if (creds != null) {
authState.setAuthScheme(new BasicScheme());
开发者_开发技巧 authState.setCredentials(creds);
}
}
}
};
DefaultHttpClient httpclient = new DefaultHttpClient();
url= url+"/"+username+"/"+foldername;
HttpPut put=new HttpPut(url);
try{
httpclient.getCredentialsProvider().setCredentials(
new AuthScope(null, -1),
new UsernamePasswordCredentials(username, password));
httpclient.addRequestInterceptor(preemptiveAuth, 0);
HttpEntity data=new StringEntity(metadata);
put.setEntity(data);
put.setHeader("Content-Type","application/vnd.org.snia.cdmi.container");
response = httpclient.execute(put);
HttpEntity resEntity=response.getEntity();
if(resEntity!=null)
content = (EntityUtils.toString(resEntity));
}catch (Exception e) {
Log.e("PUT Exception", "java.lang.NullPointerException");
}
String result=response.getStatusLine().toString()+"\n"+content;
return result;
}
Hi I think your doProcess method doesn't return anything so that it will throw
NullPointerException on setText on Textview's method.
Please confirm.
精彩评论