2010-06-02 132 views
3

我的Web服務應該返回一個整數,但每次運行代碼時都會收到NullPointerException錯誤。 任何意見或幫助將是非常讚賞NullPointerException嘗試使用kSoap方法連接到Web服務時Android

這裏是我的代碼:

public class CGCountTest extends Activity { 

    TextView testTV; 

    private static final String NAMESPACE = "http://passport-america.com/webservices/"; 
    private static final String URL = "http://localhost:11746/Service1.asmx"; 
    private static final String SOAP_ACTION = "http://www.passport-america.com/webservices/getCGCount"; 
    private static final String METHOD_NAME = "getCGCount"; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.soap_test); 

      SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); 
      SoapSerializationEnvelope envelope = 
       new SoapSerializationEnvelope(SoapEnvelope.VER11); 
       envelope.dotNet = true; 
       envelope.setOutputSoapObject(request); 
       HttpTransportSE androidHttpTransport = new HttpTransportSE(URL); 

       try { 
        androidHttpTransport.call(SOAP_ACTION, envelope); 
        java.lang.Integer result = (Integer)envelope.getResponse(); 
        TextView testTV = (TextView)findViewById(R.id.testTV); 
        result.toString(); 
        testTV.setText(result); 
       } 
       catch(Exception e) 
       { 
        testTV.setText(e.getMessage()); 
       } 
    } 

這裏的logcat的

06-02 15:13:36.557: WARN/dalvikvm(326): threadid=3: thread exiting with uncaught exception (group=0x4001aa28) 
06-02 15:13:36.557: ERROR/AndroidRuntime(326): Uncaught handler: thread main exiting due to uncaught exception 
06-02 15:13:36.876: ERROR/AndroidRuntime(326): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.pa.passammain/com.pa.passammain.CGCountTest}: java.lang.NullPointerException 
06-02 15:13:36.876: ERROR/AndroidRuntime(326):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2401) 
06-02 15:13:36.876: ERROR/AndroidRuntime(326):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2417) 
06-02 15:13:36.876: ERROR/AndroidRuntime(326):  at android.app.ActivityThread.access$2100(ActivityThread.java:116) 
06-02 15:13:36.876: ERROR/AndroidRuntime(326):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794) 
06-02 15:13:36.876: ERROR/AndroidRuntime(326):  at android.os.Handler.dispatchMessage(Handler.java:99) 
06-02 15:13:36.876: ERROR/AndroidRuntime(326):  at android.os.Looper.loop(Looper.java:123) 
06-02 15:13:36.876: ERROR/AndroidRuntime(326):  at android.app.ActivityThread.main(ActivityThread.java:4203) 
06-02 15:13:36.876: ERROR/AndroidRuntime(326):  at java.lang.reflect.Method.invokeNative(Native Method) 
06-02 15:13:36.876: ERROR/AndroidRuntime(326):  at java.lang.reflect.Method.invoke(Method.java:521) 
06-02 15:13:36.876: ERROR/AndroidRuntime(326):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791) 
06-02 15:13:36.876: ERROR/AndroidRuntime(326):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549) 
06-02 15:13:36.876: ERROR/AndroidRuntime(326):  at dalvik.system.NativeStart.main(Native Method) 
06-02 15:13:36.876: ERROR/AndroidRuntime(326): Caused by: java.lang.NullPointerException 
06-02 15:13:36.876: ERROR/AndroidRuntime(326):  at com.pa.passammain.CGCountTest.onCreate(CGCountTest.java:46) 
06-02 15:13:36.876: ERROR/AndroidRuntime(326):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123) 
06-02 15:13:36.876: ERROR/AndroidRuntime(326):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2364) 
06-02 15:13:36.876: ERROR/AndroidRuntime(326):  ... 11 more 

我想我的URL字符串可能是問題,但我已經嘗試使用我的IP沒有運氣

回答

0

我曾與類似的情況,下面的代碼工作沒有任何錯誤。

 String NAMESPACE = "http://tempuri.org/"; 
     String METHOD_NAME = "getOutletCount"; 
     String SOAP_ACTION = "http://tempuri.org/getOutletCount"; 
     String URL = "http://10.20.10.80/dinotawebsrv/GetDataWebService.asmx"; 

     SoapObject Request = new SoapObject(NAMESPACE, METHOD_NAME); 

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

     AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(
       URL); 

     androidHttpTransport.call(SOAP_ACTION, envelope); 
     System.out.println("web service call completed......"); 
     SoapPrimitive response = (SoapPrimitive) envelope.getResponse(); 

     return Integer.parseInt(response.toString()); 

嘗試調試它,檢查你是否得到任何結果的迴應,我也認爲你不能直接將響應投給整數。祝你好運..

0

這不是你的URL字符串(它可能不是你的正確但它是一個有效的字符串),其中之一您正在使用的函數調用將null作爲輸入參數之一。很可能,這是因爲你試圖做一些返回null的東西,然後在另一個方法調用中使用該值。

我不知道你在代碼的第326行有什麼,但我打賭其中一個對象是null,所以我會開始檢查第一個。

如果你想粘貼第326行,我將能夠進一步提供幫助。

+0

謝謝你這麼快的迴應! 有趣的是,在那個特定的類文件中,我只有49行代碼。我錯過了什麼嗎?如果可以的話,我對Android開發很陌生。 – bschultz 2010-06-02 16:05:08

+1

326是進程ID,而不是行號。 CGCountTest.java:46是堆棧跟蹤指示的內容。 – 2010-06-05 10:23:18

相關問題