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