Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/217.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
连接到Web服务时AndroidClient中的NullPointerException_Android_Jax Ws - Fatal编程技术网

连接到Web服务时AndroidClient中的NullPointerException

连接到Web服务时AndroidClient中的NullPointerException,android,jax-ws,Android,Jax Ws,当连接到Web服务时,我的Android客户端中出现NullPointerException。我真的不知道为什么 日志: 01-12 18:46:55.062: E/AndroidRuntime(831): FATAL EXCEPTION: main 01-12 18:46:55.062: E/AndroidRuntime(831): java.lang.RuntimeException: Unable to start activity ComponentInfo{pl.webcentral.

当连接到Web服务时,我的Android客户端中出现NullPointerException。我真的不知道为什么

日志:

01-12 18:46:55.062: E/AndroidRuntime(831): FATAL EXCEPTION: main
01-12 18:46:55.062: E/AndroidRuntime(831): java.lang.RuntimeException: Unable to start activity ComponentInfo{pl.webcentral.androidclient1/pl.webcentral.androidclient1.MainActivity}: java.lang.NullPointerException
01-12 18:46:55.062: E/AndroidRuntime(831):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
01-12 18:46:55.062: E/AndroidRuntime(831):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
01-12 18:46:55.062: E/AndroidRuntime(831):  at android.app.ActivityThread.access$600(ActivityThread.java:130)
01-12 18:46:55.062: E/AndroidRuntime(831):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
01-12 18:46:55.062: E/AndroidRuntime(831):  at android.os.Handler.dispatchMessage(Handler.java:99)
01-12 18:46:55.062: E/AndroidRuntime(831):  at android.os.Looper.loop(Looper.java:137)
01-12 18:46:55.062: E/AndroidRuntime(831):  at android.app.ActivityThread.main(ActivityThread.java:4745)
01-12 18:46:55.062: E/AndroidRuntime(831):  at java.lang.reflect.Method.invokeNative(Native Method)
01-12 18:46:55.062: E/AndroidRuntime(831):  at java.lang.reflect.Method.invoke(Method.java:511)
01-12 18:46:55.062: E/AndroidRuntime(831):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
01-12 18:46:55.062: E/AndroidRuntime(831):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
01-12 18:46:55.062: E/AndroidRuntime(831):  at dalvik.system.NativeStart.main(Native Method)
01-12 18:46:55.062: E/AndroidRuntime(831): Caused by: java.lang.NullPointerException
01-12 18:46:55.062: E/AndroidRuntime(831):  at pl.webcentral.androidclient1.GameAndroidUtil.callGameStatus(GameAndroidUtil.java:106)
01-12 18:46:55.062: E/AndroidRuntime(831):  at pl.webcentral.androidclient1.GameAndroidUtil.testGameWS(GameAndroidUtil.java:21)
01-12 18:46:55.062: E/AndroidRuntime(831):  at pl.webcentral.androidclient1.MainActivity.onCreate(MainActivity.java:24)
01-12 18:46:55.062: E/AndroidRuntime(831):  at android.app.Activity.performCreate(Activity.java:5008)
01-12 18:46:55.062: E/AndroidRuntime(831):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
01-12 18:46:55.062: E/AndroidRuntime(831):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
01-12 18:46:55.062: E/AndroidRuntime(831):  ... 11 more
GameAndroidUtil:

package pl.webcentral.androidclient1;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.SoapFault;
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 GameAndroidUtil {
    private static final String NAMESPACE = "http://game.webcentral.pl/";
    private static final String SOAP_ACTION = "";
    private static final String WSDL_URL = "http://10.0.2.2:8080/ReversiGameWS/services/GameWS?wsdl";

    public static void testGameWS() throws SoapFault {

        String session1 = callGameLogin("Marcin 1");


        GameStatus gameStatus = callGameStatus(session1);
        System.out.println("Status gracza 1 " + gameStatus.getLastMove() + " " + gameStatus.isYourMove());


        String session2 = callGameLogin("Marcin 2");


        gameStatus = callGameStatus(session2);
        System.out.println("Status gracza 2 " + gameStatus.getLastMove() + " " + gameStatus.isYourMove());

        gameStatus = callGameStatus(session1);
        System.out.println("Status gracza 1 " + gameStatus.getLastMove() + " " + gameStatus.isYourMove());


        try {
            callGameAddMove(session1, 5);
        } catch (Exception e) {
            System.out.println("Złapaliśmy wyjątek zgodnie z założeniem");
        }


        callGameAddMove(session2, 5);


        gameStatus = callGameStatus(session2);
        System.out.println("Status gracza 1 " + gameStatus.getLastMove() + " " + gameStatus.isYourMove());

        gameStatus = callGameStatus(session1);
        System.out.println("Status gracza 2 " + gameStatus.getLastMove() + " " + gameStatus.isYourMove());
    }

    private static String callGameLogin(String userName) {
        String METHOD_NAME = "login";

        SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); 
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); 

        PropertyInfo propInfo=new PropertyInfo();
        propInfo.setName("arg0");
        propInfo.setType(PropertyInfo.STRING_CLASS);
        propInfo.setValue(userName);

        request.addProperty(propInfo);

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

        try {
            androidHttpTransport.call(SOAP_ACTION, envelope);

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

            return resultsRequestSOAP.toString();

        } catch (Exception e) {
            throw new RuntimeException("Unexpected exception", e);
        }
    }
    private static GameStatus callGameStatus(String sessionId) throws SoapFault {
        String METHOD_NAME = "getGameStatus";

        SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); 
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); 

        PropertyInfo propInfo=new PropertyInfo();
        propInfo.setName("arg0");
        propInfo.setType(PropertyInfo.STRING_CLASS);
        propInfo.setValue(sessionId);

        request.addProperty(propInfo);

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

        try {
            androidHttpTransport.call(SOAP_ACTION, envelope);
        } catch (Exception e) {
            throw new RuntimeException("Unexpected exception", e);
        }

        try {
            SoapObject response = (SoapObject)envelope.getResponse();

            GameStatus gameStatus = new GameStatus();
            if (response.hasProperty("lastMove")) {
                gameStatus.setLastMove(Integer.parseInt(response.getProperty("lastMove").toString()));
            }
            gameStatus.setYourMove(Boolean.parseBoolean(response.getProperty("yourMove").toString()));

            return gameStatus;
        } catch (SoapFault e) {
            System.out.println("Error adding move: " + e.faultstring);//można to ładnie jakoś pokazać na ekranie
            throw e;
        }

    }
    private static void callGameAddMove(String sessionId, Integer move) throws SoapFault {
        String METHOD_NAME = "addMove";

        SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); 
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); 

        PropertyInfo propInfo=new PropertyInfo();
        propInfo.setName("arg0");
        propInfo.setType(PropertyInfo.STRING_CLASS);
        propInfo.setValue(sessionId);

        request.addProperty(propInfo);

        propInfo=new PropertyInfo();
        propInfo.setName("arg1");
        propInfo.setType(PropertyInfo.INTEGER_CLASS);
        propInfo.setValue(move);

        request.addProperty(propInfo);

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

        try {
            androidHttpTransport.call(SOAP_ACTION, envelope);
        } catch (Exception e) {
            throw new RuntimeException("Unexpected exception", e);
        }

        try {
            SoapPrimitive resultsRequestSOAP = (SoapPrimitive) envelope.getResponse();
        } catch (SoapFault e) {
            System.out.println("Error adding move: " + e.faultstring);//można to ładnie jakoś pokazać na ekranie
            throw e;
        }
    }

}
问题在于:

            gameStatus.setLastMove(Integer.parseInt(response.getProperty("lastMove").toString());
为什么会这样?仅当响应具有此属性时,才会执行此行。如果有,为什么要使用空指针

            gameStatus.setLastMove(Integer.parseInt(response.getProperty("lastMove").toString()));
下一行为空,这就是空指针异常的原因,请添加日志n检查它
response.getProperty(“lastMove”).toString()

似乎Log.d(“lastMove”、response.getProperty(“lastMove”).toString();正在导致NullPointerException

显然,
response.getProperty(“lastMove”)
是问题所在。这意味着
response
有一个
“lastMove”
键,但值本身是
null
。。。只需使用:

if (response.hasProperty("lastMove") && response.getProperty("lastMove") != null) {
    gameStatus.setLastMove(Integer.parseInt(response.getProperty("lastMove").toString()));
}

(如果您控制SOAPObject的创建方式,首先应该找出保存空值的原因。)

您可以在调用
gameStatus.setLastMove
之前添加
Log.d(“lastMove”,response.getProperty(“lastMove”)+”)
。您知道,只是为了确保。
请求
是否可能具有键
“lastMove”
,但数据为
null
?此Log.d()应该更改什么?添加了它,但没有结果。@Sam对我来说没有意义,但在那一行中没有看到NPE的任何其他原因。@user208030它将把
response.getProperty(“lastMove”)
value记录到LogCat,以检查它是null还是有效值。为什么要用log进行调试?这是发现大多数问题的糟糕方法,包括NPE。只需逐步使用调试器并检查变量。这就是我们应该教新手的东西。我们每个人都有不同的想法来调试解决方案。我更喜欢log,因为log.d(“lastMove”,response.getProperty(“lastMove”).toString();现在导致了NullPointerException…好的,让我们用正确的方法来做。在该行上放置断点并运行应用程序,直到调试器停止在该行。然后将鼠标悬停在每个变量上,查看哪个变量为空。因为您只有一个,所以它必须是
response
,但是使用调试器,您可以快速轻松地确定原因。