Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7622765
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T04:32:06+00:00 2026-05-31T04:32:06+00:00

class XXX implements Runnable { String lat,lon,str,taluka_name; int name; HttpResponse response; HttpEntity entity; InputStream

  • 0
class XXX implements Runnable
{

String lat,lon,str,taluka_name;
int name;
HttpResponse response;
HttpEntity entity;
InputStream is = null;
Toast s1;
StringBuilder sb=null;
TextView v;
Spinner s;
public String result[];
TextView tv;
LinearLayout ll1;
int i;
ArrayList<Integer> croplist;

public XXX(String t_n,String [] res,LinearLayout ll,TextView tv1)
{
    croplist= new ArrayList<Integer>();
    taluka_name = t_n;
    result = res;
    ll1= ll;
    tv = tv1;
}
@Override
public void run() {
    // TODO Auto-generated method stub
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost request = new HttpPost("http://10.0.2.2/proagri115.php");
    List<NameValuePair>login=new ArrayList<NameValuePair>();
    login.add(new BasicNameValuePair("location", taluka_name));
    try 
    {
        UrlEncodedFormEntity entity=new UrlEncodedFormEntity(login);
        request.setEntity(entity);
    } 
    catch (UnsupportedEncodingException e) 
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    try 
    {
        response = httpclient.execute(request);
        entity = response.getEntity();
        is = entity.getContent();
        System.out.println("Executed the request");
    } 
    catch (ClientProtocolException e) 
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } 
    catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        e.printStackTrace();
        System.out.println("");
    }
    catch(Exception e)
    {

    }
    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();
            str=sb.toString();
            Log.e("log_tag", "Success converting result "+sb.toString());
        }
     catch(Exception e)
        {
            Log.e("log_tag", "Error converting result  "+e.toString());

    System.out.println(str+"I have executed");
    result = str.split(">>");
    System.out.println("length"+result.length);
    for(i=0;i<result.length;i++)
    {
        System.out.println("\n"+i+"=="+result[i]);
    }

    System.out.println("Notified");
   }

   }
   }





public class help extends Activity{
int j;
Intent i;
String s,taluka_name;
EditText edt,edt1,edt2;
Double lat,lon;
Spinner spin;
String [] re;
TextView tv;
Layout lt;
LinearLayout lt1;
XXX runnable;
public void onCreate(Bundle savedinstancestate)
{
    super.onCreate(savedinstancestate);
    setContentView(R.layout.help);
    lt1 = (LinearLayout)findViewById(R.id.ll1);
    s =(String)getIntent().getSerializableExtra("Rainfall");
    taluka_name =(String)getIntent().getSerializableExtra("location");
    System.out.println(s);
    tv = new TextView(this);
    tv.setText("Crops for Your Selected Area are");
    lt1.addView(tv);
    try
    {   
         runnable = new XXX(taluka_name,re,lt1,tv);
             Thread threadX = new Thread(runnable);
             System.out.println("till this");
             threadX.start();  
             System.out.println("In Waited");
             try 
             {
            wait(500);
             }
              catch (IllegalMonitorStateException e)
             {
            System.out.println("IllegalMonitorStateException");
             }
             catch (InterruptedException e)
             {
            System.out.println("InterruptedException");
             }
            System.out.println("Out of Waited");
    }

    catch(Exception e)
    {
        System.out.println("Error again "+e);
    }
    try{
            System.out.println("Final Result will be");
            for(j=0;j<runnable.result.length;j++)
            {
                tv = new TextView(this);
                tv.setText(runnable.result[j]);
                System.out.println(runnable.result[j]);
                lt1.addView(tv);
            }
    }
    catch(Exception e)
    {

    }

}

}

I have main activity and thread XXX. I want to use the result of httprequest in XXX thread to be used in Main activity.But before XXX completes its operation main thread executes and
I get NullpointerException . How should I use network response in main activity . I have tried “synchronized block” . But It works for methods of single class.
How should I solve this problem?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T04:32:08+00:00Added an answer on May 31, 2026 at 4:32 am

    At minimum, you need to use Thread.join to wait for the thread to complete.. But that would block on your UI thread which is really bad. You should really just do this the android way and use an AsyncTask
    Read this:
    http://developer.android.com/resources/articles/painless-threading.html

    Here is what you should do:

    import android.os.AsyncTask;
    
    
    public class XXX extends AsyncTask<Integer, Integer, String[]> {
    
        String lat, lon, str, taluka_name;
        int name;
        HttpResponse response;
        HttpEntity entity;
        InputStream is = null;
        Toast s1;
        StringBuilder sb = null;
        TextView v;
        Spinner s;
        public String result[];
        TextView tv;
        LinearLayout ll1;
        int i;
        ArrayList<Integer> croplist;
    
        public XXX(String t_n, String[] res, LinearLayout ll, TextView tv1) {
            croplist = new ArrayList<Integer>();
            taluka_name = t_n;
            result = res;
            ll1 = ll;
            tv = tv1;
        }
    
        @Override
        protected String[] doInBackground(Integer... params) {
            // TODO Auto-generated method stub
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost request = new HttpPost("http://10.0.2.2/proagri115.php");
            List<NameValuePair> login = new ArrayList<NameValuePair>();
            login.add(new BasicNameValuePair("location", taluka_name));
            try {
                UrlEncodedFormEntity entity = new UrlEncodedFormEntity(login);
                request.setEntity(entity);
            } catch (UnsupportedEncodingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            try {
                response = httpclient.execute(request);
                entity = response.getEntity();
                is = entity.getContent();
                System.out.println("Executed the request");
            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                e.printStackTrace();
                System.out.println("");
            } catch (Exception e) {
    
            }
            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();
                str = sb.toString();
                Log.e("log_tag", "Success converting result " + sb.toString());
            } catch (Exception e) {
                Log.e("log_tag", "Error converting result  " + e.toString());
    
                System.out.println(str + "I have executed");
                result = str.split(">>");
                System.out.println("length" + result.length);
                for (i = 0; i < result.length; i++) {
                    System.out.println("\n" + i + "==" + result[i]);
                }
    
                System.out.println("Notified");
            }
            return result;
        }
    
    
    
    }
    

    Then from your activity call:

    new XXX(taluka_name,re,lt1,tv).execute();
    

    Now the tricky thing is you need to get that result back to your UI thread.. The easiest way is to put the AsyncTask within the activity as an inner class, then in onPostExecute of the asyncTask you just call some function from your activity.
    If you want the AsyncTask in a seperate file then you need to pass a reference of your class to the constructor of the AsyncTask and then you can call any public method of your activity from the asyncTask. Just remember that you can only call the activities methods in onPostExecute and in onProgressUpdate (do not call UI methods in doInBackground)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My custom type is (no default constructor!): package com.XXX.common; public class Email implements Serializable
class Tag(models.Model): name = models.CharField(maxlength=100) class Blog(models.Model): name = models.CharField(maxlength=100) tags = models.ManyToManyField(Tag) Simple
class someclass {}; class base { int a; int *pint; someclass objsomeclass; someclass* psomeclass;
class Score { var $score; var $name; var $dept; var $date; function Score($score, $name,
I'm trying to create an object of a class, using just a name of
We wrote a small Windows class library that implements extension methods for some standard
So, let's say I have this code (VB.Net): Sub Main() dim xxx as string
I have the following code with the corresponding test case: class XXX attr_accessor :source
I have a WCF service that returns a class that implements IExtensibleDataObject. I need
I have implemented a C++ class which behaves very similarly to the standard int

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.