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 8570481
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T18:34:05+00:00 2026-06-11T18:34:05+00:00

i am new to android development. i am trying to post data to server

  • 0

i am new to android development. i am trying to post data to server asynchronously. i have a text view message1 whose data gets posted to a web service. My code is as below

public class Main extends Activity {

    ProgressDialog dialog;
    final EditText message1 = (EditText)  findViewById(R.id.editText1) ;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

            //generate GCM id ended
              StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
              StrictMode.setThreadPolicy(policy); 
    }

        private class Submitdata extends AsyncTask<String, Void, String>
        {

            @Override
            protected String doInBackground(String... arg0) {


                HttpClient httpclient = new DefaultHttpClient();
                HttpPost httppost = new HttpPost("http://xyz.com/imran/msg.php");
                try {
                    // Add your data
                    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
                    nameValuePairs.add(new BasicNameValuePair("message", message1.getText().toString()));


                   httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

                    // Execute HTTP Post Request
                   //Toast.makeText(this, resId, duration)
                    HttpResponse response = httpclient.execute(httppost);
//                  Intent intent = new Intent(Register.this,Emergency.class);
//                  Register.this.startActivity(intent);        



                } catch (ClientProtocolException e) {
                    // TODO Auto-generated catch block
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                }

                // TODO Auto-generated method stub
                return null;
            }

            protected void onPreExecute() {
                dialog = ProgressDialog.show(Main.this, null, "Loading", false);



            }
            @Override
            protected void onPostExecute(String result) {
             dialog.dismiss();
            }

        }

    //define function for doing in background like submit data

        public void add_data()
        {
            Submitdata task = new Submitdata();
            task.execute();
        } 

every time i try to run the application, the application gets crashed. i have google the problem and many posts say that it is due to Null pointer exception on onPostExecute() method. can any help me in what i need to change to my code to fix the problem.
Thanks in advance

My Logcat output :

09-24 22:25:32.108: E/AndroidRuntime(18478):    at android.app.ActivityThread.access$600(ActivityThread.java:123)
09-24 22:25:32.108: E/AndroidRuntime(18478):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
09-24 22:25:32.108: E/AndroidRuntime(18478):    at android.os.Handler.dispatchMessage(Handler.java:99)
09-24 22:25:32.108: E/AndroidRuntime(18478):    at android.os.Looper.loop(Looper.java:137)
09-24 22:25:32.108: E/AndroidRuntime(18478):    at android.app.ActivityThread.main(ActivityThread.java:4424)
09-24 22:25:32.108: E/AndroidRuntime(18478):    at java.lang.reflect.Method.invokeNative(Native Method)
09-24 22:25:32.108: E/AndroidRuntime(18478):    at java.lang.reflect.Method.invoke(Method.java:511)
09-24 22:25:32.108: E/AndroidRuntime(18478):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:817)
09-24 22:25:32.108: E/AndroidRuntime(18478):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
09-24 22:25:32.108: E/AndroidRuntime(18478):    at dalvik.system.NativeStart.main(Native Method)
09-24 22:25:32.108: E/AndroidRuntime(18478): Caused by: java.lang.NullPointerException
09-24 22:25:32.108: E/AndroidRuntime(18478):    at android.app.Activity.findViewById(Activity.java:1794)
09-24 22:25:32.108: E/AndroidRuntime(18478):    at com.mpidc2.management.Main.<init>(Main.java:34)
09-24 22:25:32.108: E/AndroidRuntime(18478):    at java.lang.Class.newInstanceImpl(Native Method)
09-24 22:25:32.108: E/AndroidRuntime(18478):    at java.lang.Class.newInstance(Class.java:1319)
09-24 22:25:32.108: E/AndroidRuntime(18478):    at android.app.Instrumentation.newActivity(Instrumentation.java:1023)
09-24 22:25:32.108: E/AndroidRuntime(18478):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1871)
09-24 22:25:32.108: E/AndroidRuntime(18478):    ... 11 more
  • 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-06-11T18:34:06+00:00Added an answer on June 11, 2026 at 6:34 pm

    Move the line EditText message1 = (EditText) findViewById(R.id.editText1); below setContentView(R.layout.activity_main);

    The reason why it’s crashing is that you’re trying to get a reference to your EditText before setting the Activity’s view.

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

Sidebar

Related Questions

I am very new to android development and have been trying to draw a
I am new to Android and trying to setup Android Development Environment. I have
I'm new to Android development and I'm trying to implement a custom view to
i m new to android development. i am trying to insert data into remote
I am new to android development. I'm trying to have my string.xml file add
So I'm pretty new to android development and have been trying to piece together
I'm new in android development. I have been trying to start a new activity
I'm new to both android and game development and have been trying to create
I am new to android development and I've hit a hurdle when trying to
I am new to android development . I'm trying to download and run the

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.