Java 异步回调失败
在我的应用程序中,我需要将字符串值添加到文件(.property文件,如果重要的话)。用户在gwt GUI中输入该值。以下是它的重要部分:Java 异步回调失败,java,gwt,gxt,asynccallback,Java,Gwt,Gxt,Asynccallback,在我的应用程序中,我需要将字符串值添加到文件(.property文件,如果重要的话)。用户在gwt GUI中输入该值。以下是它的重要部分: final Button submit = new Button("Submit"); addButton(submit); submit.addSelectionListener(new SelectionListener<ButtonEvent>() { @Override
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);
}
所有接口都存在。
当我运行代码并按下gui中的“提交”按钮时,我看到两个asynccallback都失败了(如您所见,Window.alert显示“空指针异常”,尽管我发送给方法的值不是空的)。为什么会这样?你能给我提个建议吗
UPD以下是firebug显示的错误:
uncaught exception: java.lang.ClassCastException
function W8(){try{null.a()}catch(a){return a}}
问题解决了:代码中有一个简单的错误。我把括号放错地方了:
//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";
}
我建议您使用 -造型漂亮 然后再次检查firebug输出;与更新的未捕获异常相比,它可能会给您提供更好的线索
接下来,我建议您在eclipse调试器中运行它,并在客户端和服务器代码中设置断点,然后您可以检查变量并逐步执行代码。服务器是否接收到请求?服务器日志呢?事实上,我是个编程新手,不能这么说,因为我不知道怎么做。只有firefox中的firebug显示文章有适当的值和一个错误,我不明白这是什么。我现在就更新我的帖子
//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";
}