2011-08-03 28 views
0

我正在開發一個小黑莓項目。將backberry文件上傳到Web服務= JVM錯誤104 Uncaught NullPointerException?

這裏是它應該是步驟:

  1. 用戶點擊說話!按鈕。應用程序記錄語音。 [沒問題]

  2. 當用戶完成說話,單擊停止!按鈕。點擊停止按鈕後,語音將作爲AMR文件保存在BB上。然後,該文件將通過ksoap2發送到Web服務。 Web服務會將響應作爲文件名的字符串返回。問題是Web服務任何回報且有錯誤發生:JVM錯誤104:未捕獲的NullPointerException我不知道如果我放在正確的位置代碼,或我做錯了什麼事與ksoap2?

這裏是Web服務

namespace VoiceServer 
{ 
    /// <summary> 
    /// Converting AMR to WAV 
    /// </summary> 
    [WebService(Namespace = "http://tempuri.org/")] 
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] 
    [System.ComponentModel.ToolboxItem(false)] 

    public class Service1 : System.Web.Services.WebService 
    { 

     public string UploadFile(String receivedByte, String location, String fileName) 
{ 
    String filepath = fileName; 

/*don't worry about receivedByte and location, I will work on them after the problem is  solved :) */ 

    return "Success"+filepath; 
} 

     private void InitializeComponent() 
     { 

     } 
    } 
} 

下面是在Eclipse上運行代碼的代碼,我不知道如果我把代碼,以便在正確的地方發送文件的Web服務。

public class MyAudio extends MainScreen { 
private ButtonField _startRecordingButton; 
private ButtonField _stopRecordingButton; 
private HorizontalFieldManager _fieldManagerButtons; 
private VoiceNotesRecorderThread _voiceRecorder; 
private LabelField _myAudioTextField; 
private DateField hourMin; 
private long _initTime; 

public MyAudio() { 
    _startRecordingButton = new ButtonField("Speak!", ButtonField.CONSUME_CLICK); 
    _stopRecordingButton = new ButtonField("Stop!", ButtonField.CONSUME_CLICK); 
    _fieldManagerButtons = new HorizontalFieldManager(); 
    _voiceRecorder = new VoiceNotesRecorderThread(500000,"file:///store/home/user/voicefile.amr",this); 
    _voiceRecorder.start(); 

    myButtonFieldChangeListener buttonFieldChangeListener = new myButtonFieldChangeListener(); 
    _startRecordingButton.setChangeListener(buttonFieldChangeListener);  
    _stopRecordingButton.setChangeListener(buttonFieldChangeListener);  

    _fieldManagerButtons.add(_startRecordingButton); 
    _fieldManagerButtons.add(_stopRecordingButton); 

    _myAudioTextField = new LabelField(" Welcome to VoiceSMS!!!"); 
    add(_fieldManagerButtons); 
    add(_myAudioTextField); 

    SimpleDateFormat sdF = new SimpleDateFormat("ss"); 
    hourMin = new DateField("", 0, sdF); 
    hourMin.setEditable(false); 
    hourMin.select(false); 
    _initTime = System.currentTimeMillis(); 
    add(hourMin); 
} 

public void setAudioTextField(String text) { 
    _myAudioTextField.setText(text); 
} 

public void startTime() { 
    _initTime = System.currentTimeMillis(); 
    hourMin.setDate(0); 
} 

public void updateTime() { 
    hourMin.setDate((System.currentTimeMillis()-_initTime)); 
} 

class myButtonFieldChangeListener implements FieldChangeListener{ 
    public void fieldChanged(Field field, int context) { 
     if(field == _startRecordingButton) { 
      try { 
       _voiceRecorder.startRecording(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     }else if(field == _stopRecordingButton) { 
      _voiceRecorder.stopRecording(); 

      //----------Send AMR to Web Service-------------// 

      Object response = null; 
      String URL = "http://http://localhost:portnumber/Service1.asmx"; 
      String method = "UploadFile"; 
      String NameSpace = "http://tempuri.org/"; 
      FileConnection fc = null; 
      byte [] ary = null; 
      try 
      { 
      fc = (FileConnection)Connector.open("file:///store/home/user/voicefile.amr",Connector.READ_WRITE); 
      int size = (int) fc.fileSize(); 
      //String a = Integer.toString(size); 
      //Dialog.alert(a); 
      ary = new byte[size]; 
      fc.openDataInputStream().read(ary); 
      fc.close(); 
      } 
      catch (IOException e1) 
      { 
      e1.printStackTrace(); 
      } 
      SoapObject client = new SoapObject(NameSpace,method); 
      client.addProperty("receivedByte",new SoapPrimitive(SoapEnvelope.ENC,"base64",Base64.encode(ary))); 
      client.addProperty("location","Test/"); 
      client.addProperty("fileName","file:///store/home/user/voicefile.amr"); 
      SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); 
      envelope.bodyOut = client; 
      HttpTransport http = new HttpTransport(URL); 

      try 
      { 
      http.call(method,envelope); 
      } 
      catch(InterruptedIOException io) 
      { 
      io.printStackTrace(); 
      } 
      catch (IOException e) 
      { 
      System.err.println(e); 
      } 
      catch (XmlPullParserException e) 
      { 
      System.err.println(e); 

      } 
      catch(OutOfMemoryError e) 
      { 
      System.out.println(e.getMessage()); 
      } 
      catch(Exception e) 
      { 
      e.printStackTrace(); 
      } 

      try 
      { 
      response = envelope.getResponse(); 
      Dialog.alert(response.toString()); 
      } 
      catch (SoapFault e) 
      { 
      System.err.println(e); 
      System.out.println("Soap Fault"); 
      } 
      catch(NullPointerException ne) 
      { 
      System.err.println(ne); 
      } 
      Dialog.alert(response.toString()); 
      //Dialog.alert("Send Success"); 

      //----------End of Upload-to-Web-Service--------// 
     }   
    } 
} 
} 

我不知道,如果該文件不會被髮送到Web服務或Web服務已經拿到了文件,併產生沒有任何反應???我是BB編程的真正新手。如果我做錯了,請告訴我。

在此先感謝!

回答

2

。在你的URL變量值一個錯字。

「HTTP://」 鍵入兩次

字符串URL = 「HTTP:// HTTP://本地主機:端口號/ Service1.asmx的」;

+0

感謝拉斐爾上述「公共字符串UploadFile補充說:」 [的WebMethod]!我已經糾正了它,但仍然無法解決問題。 – NandNand

0

萬歲!問題解決了!

只是改變URL拉斐爾建議,並在Web服務代碼