0

Edit :
I know it is returning null pointer , but what is cause of it , is it some web service or code issue ?

I am calling a external WebService (celcius to Farenheit Demo) from Android ..

it gives me following error

Attempt to invoke virtual method 'java.lang.String org.ksoap2.serialization.SoapPrimitive.toString()' on a null object reference

I dont have too much Code , Just the MainActivity Class and Async Class

public class MainActivity extends AppCompatActivity {

String TAG = "Response";
Button bt;
EditText celcius;
String getCel;
SoapPrimitive resultString;
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        bt = (Button) findViewById(R.id.bt);
        celcius = (EditText) findViewById(R.id.cel);


        bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                getCel = celcius.getText().toString();
                AsyncCallWS task = new AsyncCallWS();
                task.execute();
            }
        });
    }

    private class AsyncCallWS extends AsyncTask<Void, Void, Void> {

        @Override
        protected void onPreExecute() {
            Log.i(TAG, "onPreExecute");
        }

        @Override
        protected Void doInBackground(Void... params) {
                Log.i(TAG, "doInBackground");
            calculate();
            return null;
        }

        public void calculate() {
            String SOAP_ACTION = "http://www.w3schools.com/webservices/CelsiusToFahrenheit";
            String METHOD_NAME = "CelsiusToFahrenheit";
            String NAMESPACE = "http://www.w3schools.com/webservices/";
            String URL = "http://www.w3schools.com/webservices/tempconvert.asmx";

            try {
                SoapObject Request = new SoapObject(NAMESPACE, METHOD_NAME);
                Request.addProperty("Celsius", getCel);

                SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
                soapEnvelope.dotNet = true;
                soapEnvelope.setOutputSoapObject(Request);

                HttpTransportSE transport = new HttpTransportSE(URL);

                transport.call(SOAP_ACTION, soapEnvelope);
                resultString = (SoapPrimitive) soapEnvelope.getResponse();

                Log.i(TAG, "Result Celsius: " + resultString);
            } catch (Exception ex) {
                Log.e(TAG, "Error: " + ex.getMessage());
            }
        }

        @Override
        protected void onPostExecute(Void result) {
            Log.i(TAG, "onPostExecute");
            Toast.makeText(MainActivity.this, "Response" + resultString.toString(), Toast.LENGTH_LONG).show();
        }

    }

Logcat Shows

Attempt to invoke virtual method 'java.lang.String     
org.ksoap2.serialization.SoapPrimitive.toString()' on a null object     
reference at testproject.example.com.sensor_new.MainActivity$AsyncCallWS.onPostExecute(MainActivity.java:89)

Some solution i saw like to apply Internet setting in manifest and do the calculation in DoBackGround . But that is not solving any issue ,

Any Help would be appreciated

Neeraj Verma
  • 2,174
  • 6
  • 30
  • 51
  • I know it is returning null pointer , but what is cause of it , is it some web service or code issue ? – Neeraj Verma Jul 05 '16 at 09:47
  • "Log.i(TAG, "Result Celsius: " + resultString);" Is this line working ..Are you able to see the log ? – Sunil Sunny Jul 05 '16 at 09:55
  • no , before this line , it an inside code and show thread is already in running code – Neeraj Verma Jul 05 '16 at 10:31
  • Which means you are not getting the response right ? Try debugging this line " resultString = (SoapPrimitive) soapEnvelope.getResponse(); " and see what the response is. " Log.e(TAG, "Error: " + ex.getMessage());" Are you getting this log. – Sunil Sunny Jul 05 '16 at 10:36

0 Answers0