user3534834
user3534834

Reputation: 85

why my app crash when i try to post some data into web service?

what i am doing is that i am trying to send some strings from my activity and post to a web service .. my code is like this :

approve.setOnClickListener(new OnClickListener() {

            @SuppressWarnings("unchecked")
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
//              Intent intent=new Intent(Info.this,MainActivity.class);

                 status="1";
                 ordernumber="25";
                 userid="11";
                 //Create instance for AsyncCallWS
                AsyncCallWS task = new AsyncCallWS();
                //Call execute 
                task.execute();
            }   
        });

     private class AsyncCallWS extends AsyncTask {

        @Override
        protected Object doInBackground(Object... params) {
            // TODO Auto-generated method stub

                loginStatus = WebService.invokeLoginWS(status,ordernumber,userid,"GetOrderData");
                return null;
            }

            protected void onPostExecute(Void result) {
                //Make Progress Bar invisible
                webservicePG.setVisibility(View.INVISIBLE);
                Intent intObj = new Intent(Info.this,PenddingOrders.class);
                //Error status is false
            }


            //Make Progress Bar visible
            protected void onPreExecute() {
                webservicePG.setVisibility(View.VISIBLE);
            }


        } 

and here is my web service class like this :

package post;


import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

public class WebService {
    //Namespace of the Webservice - can be found in WSDL
    private static String NAMESPACE = "http://service.programmerguru.com/";
    //Webservice URL - WSDL File location    
    private static String URL = "http://192.168.1.113/testOrder/OrderAndroid.asmx";//Make sure you changed IP address
    //SOAP Action URI again Namespace + Web method name
    private static String SOAP_ACTION = "http://service.programmerguru.com/";

    public static boolean invokeLoginWS(String status,String orderid,String username, String webMethName) {
        boolean loginStatus = false;
        // Create request
        SoapObject request = new SoapObject(NAMESPACE, webMethName);
        // Property which holds input parameters
        PropertyInfo statusPI = new PropertyInfo();
        PropertyInfo orderidPI = new PropertyInfo();
        PropertyInfo usernamePI = new PropertyInfo();
        // Set Username
        statusPI.setName("status");
        // Set Value
        statusPI.setValue(status);
        // Set dataType
        statusPI.setType(String.class);
        // Add the property to request object
        request.addProperty(statusPI);
        //Set Password
        orderidPI.setName("orderid");
        //Set dataType
        orderidPI.setValue(orderid);
        //Set dataType
        orderidPI.setType(String.class);
        //Add the property to request object
        request.addProperty(orderidPI);
        usernamePI.setName("username");
        //Set dataType
        usernamePI.setValue(username);
        //Set dataType
        usernamePI.setType(String.class);
        //Add the property to request object
        request.addProperty(usernamePI);


        // Create envelope
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                SoapEnvelope.VER11);
        // Set output SOAP object
        envelope.setOutputSoapObject(request);
        // Create HTTP call object
        HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);


        try {
            // Invoke web service
            androidHttpTransport.call(SOAP_ACTION+webMethName, envelope);
            // Get the response
            SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
            // Assign it to  boolean variable variable
            loginStatus = Boolean.parseBoolean(response.toString());

        } catch (Exception e) {
            //Assign Error Status true in static variable 'errored'

            e.printStackTrace();
        } 
        //Return booleam to calling object
        return loginStatus;
    }
}

and my activity crash when i press the approve button and here are some of what i see in the logcat:

FATAL EXCEPTION: main
java.lang.NullPointerException
at com.example.newmaamoontest.Info$AsyncCallWS.onPreExecute(Info.java:151)
at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586)
at android.os.AsyncTask.execute(AsyncTask.java:534)

Upvotes: 0

Views: 236

Answers (3)

kumar
kumar

Reputation: 691

A better solution is you move

         status="1";
         ordernumber="25";
         userid="11";
         //Create instance for AsyncCallWS
        AsyncCallWS task = new AsyncCallWS();
        //Call execute 
        task.execute();

To a method. In that method you launch progressdialog and execute the task. For Android you will have problem by doing the way you are doing.

Upvotes: 0

Suhas K
Suhas K

Reputation: 74

Your are not initializing webservicePG view variable. You are trying to call method without instantiating object. try initializing and instantiate webservicePG in onCreate method using webservicePG = findViewById(R.id.your_id); or if you are creating dynamically webservicePG = new View(this);

Upvotes: 1

Smokez
Smokez

Reputation: 382

It seems to me that you have not initialized your webservicePG variable at the time you attempt to set it's visibility. It's a view, so have you called webservicePG = findViewById(R.id.your_webservicePG_id); in your onCreate() or onCreateView()?

Upvotes: 0

Related Questions