Note that there are some explanatory texts on larger screens.

plurals
  1. POAccessing a single PHP file from two different activites in android
    text
    copied!<p>I have two activites: <code>Mainpage.java</code> and <code>Secondary.java</code>. And I am trying to access the same php file from both the class. When the second class is called the following <strong><em>error</em></strong> is revoked.</p> <pre><code> java.lang.nullpointerexception. </code></pre> <p>How can i remove this error?</p> <p>This is causing the apllication to force close. Thanks. This the code for the first class: void login(){ try{ </p> <pre><code> httpclient=new DefaultHttpClient(); httppost= new HttpPost("http://10.0.2.2/ABC/login.php"); nameValuePairs = new ArrayList&lt;NameValuePair&gt;(2); nameValuePairs.add(new BasicNameValuePair("username",et.getText().toString().trim())); nameValuePairs.add(new BasicNameValuePair("password",pass.getText().toString().trim())); nameValuePairs.add(new BasicNameValuePair("action","LOGIN")); httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); ResponseHandler&lt;String&gt; responseHandler = new BasicResponseHandler(); response=httpclient.execute(httppost); final String response = httpclient.execute(httppost, responseHandler); System.out.println("Response : " + response); tv.setText("Response from PHP : " + response); if(response.equalsIgnoreCase("User Found")) { Toast.makeText(Axdroid.this,"Login Success",Toast.LENGTH_SHORT).show(); startActivity(new Intent(getApplicationContext(),Secondary.class)); } else { showAlert(); } dialog.dismiss(); }catch(Exception e){ System.out.println("Exception : " + e.getMessage()); } </code></pre> <p>}</p> <p>And this for the secondary class void jsr(){</p> <pre><code> try{ httpclient = new DefaultHttpClient(); httppost= new HttpPost("http://10.0.2.2/AndroidLeave/login.php"); nameValuePairs.add(new BasicNameValuePair("action","SUMMARY")); httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); response = httpclient.execute(httppost); System.out.println(" ++++++++ +++++++++++++"+response); entity = response.getEntity(); is = entity.getContent(); }catch(Exception e){ Log.e("log_tag", "Error in http connection"+e.toString()); } System.out.println(" ++++++++ After getting data from PHP file +++++++++++++"); //convert response to string try{ BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8); sb = new StringBuilder(); sb.append(reader.readLine() + "\n"); String line="0"; 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());} }} </code></pre> <p>LOGCAT:</p> <pre><code> 10-26 18:10:01.313: W/SingleClientConnManager(2296): Make sure to release the connection before allocating another one. 10-26 18:10:01.964: E/log_tag(2296): Error in http connectionjava.lang.NullPointerException 10-26 18:10:01.974: E/log_tag(2296): Error converting result java.lang.NullPointerException </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload