简体   繁体   中英

Calling a web service from android - NullPointerException

I am trying to use the code from here http://www.ibm.com/developerworks/webservices/library/ws-android/index.html?ca=dat- to call a web service. I keep getting the exception thrown at the following line of my code….

androidHttpTransport.call(SOAP_ACTION, envelope);

The class I've written looks as follows;

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;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity {

    private EditText editext1;
    private EditText editext2;
    private EditText editext3;
    private EditText editext4;
    private Button btn1;

    private static final String NAMESPACE = "http://service.hotornot.com/";
    private static String URL = "http://hotornot-hnwebservice.rhcloud.com/TomcatHotOrNot/messages?wsdl";
    private static final String METHOD_NAME = "addMessage";
    private static final String SOAP_ACTION = "http://service.hotornot.com/addMessage";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        editext1 = (EditText) findViewById(R.id.editText1);
        editext2 = (EditText) findViewById(R.id.editText2);
        editext3 = (EditText) findViewById(R.id.editText3);
        editext4 = (EditText) findViewById(R.id.editText4);
        btn1 = (Button) findViewById(R.id.button1);

        btn1.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

                SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

                PropertyInfo propInfo = new PropertyInfo();
                propInfo.name = "arg0";
                propInfo.type = PropertyInfo.STRING_CLASS;
                request.addProperty(propInfo, editext1.getText().toString());

                PropertyInfo propInfo1 = new PropertyInfo();
                propInfo.name = "arg1";
                propInfo.type = PropertyInfo.STRING_CLASS;
                request.addProperty(propInfo1, editext2.getText().toString());

                PropertyInfo propInf2 = new PropertyInfo();
                propInfo.name = "arg2";
                propInfo.type = PropertyInfo.STRING_CLASS;
                request.addProperty(propInf2, editext3.getText().toString());

                PropertyInfo propInfo3 = new PropertyInfo();
                propInfo.name = "arg3";
                propInfo.type = PropertyInfo.STRING_CLASS;
                request.addProperty(propInfo3, editext4.getText().toString());

                SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                        SoapEnvelope.VER11);

                envelope.setOutputSoapObject(request);
                HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

                try {
                    androidHttpTransport.call(SOAP_ACTION, envelope);

                    SoapPrimitive resultsRequestSOAP = (SoapPrimitive) envelope
                            .getResponse();

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        return true;
    }

}

Interestingly - if i debug the code and check the output of the SoapObject it looks as follows which doesn't seem correct - why is the key null? I assume this is what is causing the null pointer exception

addMessage{arg3=this; null=is; null=a; null=test; }

Exception:

12-31 13:23:47.514: W/System.err(770): java.lang.NullPointerException
    12-31 13:23:47.524: W/System.err(770):  at java.io.Writer.write(Writer.java:141)
    12-31 13:23:47.524: W/System.err(770):  at org.kxml2.io.KXmlSerializer.startTag(KXmlSerializer.java:412)
    12-31 13:23:47.524: W/System.err(770):  at org.ksoap2.serialization.SoapSerializationEnvelope.writeObjectBody(SoapSerializationEnvelope.java:581)
    12-31 13:23:47.534: W/System.err(770):  at org.ksoap2.serialization.SoapSerializationEnvelope.writeObjectBody(SoapSerializationEnvelope.java:566)
    12-31 13:23:47.534: W/System.err(770):  at org.ksoap2.serialization.SoapSerializationEnvelope.writeElement(SoapSerializationEnvelope.java:623)
    12-31 13:23:47.544: W/System.err(770):  at org.ksoap2.serialization.SoapSerializationEnvelope.writeBody(SoapSerializationEnvelope.java:547)
    12-31 13:23:47.544: W/System.err(770):  at org.ksoap2.SoapEnvelope.write(SoapEnvelope.java:192)
    12-31 13:23:47.554: W/System.err(770):  at org.ksoap2.transport.Transport.createRequestData(Transport.java:74)
    12-31 13:23:47.554: W/System.err(770):  at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:58)
    12-31 13:23:47.564: W/System.err(770):  at com.example.hotornot.MainActivity$1.onClick(MainActivity.java:74)
    12-31 13:23:47.564: W/System.err(770):  at android.view.View.performClick(View.java:4204)
    12-31 13:23:47.564: W/System.err(770):  at android.view.View$PerformClick.run(View.java:17355)
    12-31 13:23:47.574: W/System.err(770):  at android.os.Handler.handleCallback(Handler.java:725)
    12-31 13:23:47.574: W/System.err(770):  at android.os.Handler.dispatchMessage(Handler.java:92)
    12-31 13:23:47.584: W/System.err(770):  at android.os.Looper.loop(Looper.java:137)
    12-31 13:23:47.584: W/System.err(770):  at android.app.ActivityThread.main(ActivityThread.java:5041)
    12-31 13:23:47.594: W/System.err(770):  at java.lang.reflect.Method.invokeNative(Native Method)
    12-31 13:23:47.594: W/System.err(770):  at java.lang.reflect.Method.invoke(Method.java:511)
    12-31 13:23:47.604: W/System.err(770):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
    12-31 13:23:47.604: W/System.err(770):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
    12-31 13:23:47.604: W/System.err(770):  at dalvik.system.NativeStart.main(Native Method)

This solves your problem:

            PropertyInfo propInfo = new PropertyInfo();
            propInfo.name = "arg0";
            propInfo.type = PropertyInfo.STRING_CLASS;
            request.addProperty(propInfo, editext1.getText().toString());

            PropertyInfo propInfo1 = new PropertyInfo();
            propInfo1.name = "arg1";
            propInfo1.type = PropertyInfo.STRING_CLASS;
            request.addProperty(propInfo1, editext2.getText().toString());

            PropertyInfo propInf2 = new PropertyInfo();
            propInf2.name = "arg2";
            propInf2.type = PropertyInfo.STRING_CLASS;
            request.addProperty(propInf2, editext3.getText().toString());

            PropertyInfo propInfo3 = new PropertyInfo();
            propInfo3.name = "arg3";
            propInfo3.type = PropertyInfo.STRING_CLASS;
            request.addProperty(propInfo3, editext4.getText().toString());

Explanation: the assignments of properties "name" and "type" were done on the wrong variable (always the first one propInfo, but should be propInfo1, ...).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM