2013-04-12 82 views
0

在我的應用程序中,我需要將字符串值添加到文件(.property文件,如果它很重要)。並且用戶在gwt GUI中輸入這個值。下面是它的重要組成部分:asynccallback failures

final Button submit = new Button("Submit"); 
     addButton(submit); 
     submit.addSelectionListener(new SelectionListener<ButtonEvent>() { 
      @Override 
      public void componentSelected(ButtonEvent ce) { 
       keyWord.selectAll(); 
       regexp.selectAll(); 
       if (keyWord.getValue() != null){ 
        setKeyWord(customerId, keyWord.getValue()); 
        keyWord.setValue(""); 
       } 
       if (regexp.getValue() != null){ 
        setRegExp(customerId, regexp.getValue()); 
        regexp.setValue(""); 
       } 

      } 
     }); 
    } 

    private void setKeyWord(final String customerId, final String keyword){ 

     final AsyncCallback<String> callbackItems = new AsyncCallback<String>() { 
      public void onFailure(final Throwable caught) { 
       Window.alert("unable to add " + caught.toString()); 
      } 

      public void onSuccess(final String x) { 
       Window.alert(x); 
      } 
     }; 
     serverManagementSvc.setKeyWords(customerId, keyword, callbackItems); 
    } 

    private void setRegExp(final String customerId, final String regexp){ 

     final AsyncCallback<String> calbackItems = new AsyncCallback<String>() { 
      @Override 
      public void onFailure(Throwable throwable) { 
       Window.alert("unable to add " + throwable.toString()); 
      } 

      @Override 
      public void onSuccess(String s) { 
       Window.alert(s); 
      } 
     }; 
     serverManagementSvc.setRegExp(customerId, regexp, calbackItems); 
    } 

所以我需要使用Asunccallback調用它們是在「服務器的一部分」的方法。 這裏是這些方法:

//adds a new keyword to customers properties 
    public String setKeyWords(String customer, String word){ 
     try{ 
       PropertiesConfiguration props = new PropertiesConfiguration("/home/mikhail/bzrrep/DLP/DLPServer/src/main/resources/rules.properties"); 
       String newKeyWord = new String(props.getString("users." + customer + ".keywords" + "," + word)); 
       props.setProperty("users." + customer + ".keywords", newKeyWord); 
       props.save(); 
     }catch (ConfigurationException e){ 
      e.printStackTrace(); 
     } 
     return "keyword " + word + " added"; 
    } 

    // adds a new regexp to customer properties 
    public String setRegExp(String customer, String regexp){ 
     try { 
       PropertiesConfiguration props = new PropertiesConfiguration("/home/mikhail/bzrrep/DLP/DLPServer/src/main/resources/rules.properties"); 
       String newRegValue = new String(props.getString("users." + customer + ".regexps" + "," + regexp)); 
       props.setProperty("users." + customer + ".regexps", newRegValue); 
       props.save(); 
     } catch (ConfigurationException e){ 
      e.printStackTrace(); 
     } 
     return "regexp " + regexp + " added to " + customer + "'s config"; 
    } 

所有接口都存在。 當我運行我的代碼並按gui中的「提交」按鈕我看到,兩個asynccallback失敗(Window.alert,如你所見,顯示「空指針異常」,儘管我發送給方法的值不爲null )。爲什麼會這樣?你能告訴我什麼嗎?

UPD這裏是錯誤,是由螢火蟲顯示:

uncaught exception: java.lang.ClassCastException  
function W8(){try{null.a()}catch(a){return a}} 
+0

服務器是否收到請求?服務器日誌怎麼樣? – home

+0

實際上,我是編程中的一名新手,不能說這是因爲不知道如何。 firefox中只有firebug表明Post有適當的值和一個錯誤,我不明白是什麼。我現在更新我的帖子 –

回答

0

解決問題:有在代碼的簡單錯誤。我在錯誤的地方關閉括號:

//adds a new keyword to customers properties 
    public String setKeyWords(String customer, String word){ 
     try{ 
       PropertiesConfiguration props = new PropertiesConfiguration("/home/mikhail/bzrrep/DLP/DLPServer/src/main/resources/rules.properties"); 
       String newKeyWord = new String(props.getString("users." + customer + ".keywords") + "," + word); 
       props.setProperty("users." + customer + ".keywords", newKeyWord); 
       props.save(); 
     }catch (ConfigurationException e){ 
      e.printStackTrace(); 
     } 
     return "keyword " + word + " added"; 
    } 

    // adds a new regexp to customer properties 
    public String setRegExp(String customer, String regexp){ 
     try { 
       PropertiesConfiguration props = new PropertiesConfiguration("/home/mikhail/bzrrep/DLP/DLPServer/src/main/resources/rules.properties"); 
       String newRegValue = new String(props.getString("users." + customer + ".regexps") + "," + regexp); 
       props.setProperty("users." + customer + ".regexps", newRegValue); 
       props.save(); 
     } catch (ConfigurationException e){ 
      e.printStackTrace(); 
     } 
     return "regexp " + regexp + " added to " + customer + "'s config"; 
    } 
0

我建議你重新編譯使用 GWT的代碼風格PRETTY ,然後再次檢查螢火蟲輸出;與更新的未捕獲異常相比,它可能會給您一個更好的線索。接下來,我建議您在eclipse調試器中運行它,並在客戶端和服務器代碼中設置斷點,然後您可以檢查變量並逐步執行代碼。