Why a variable inside a private inner class can't get value of a private variable that has value from AsynCallback method in Gwt?

StackOverflow https://stackoverflow.com/questions/20611854

  •  02-09-2022
  •  | 
  •  

Question

I found this very weird behavior in Gwt.

I have a simple TestPresenter.java that have 2 buttons. Button 1 get data from DB and return value via Asyncallback method. Button 2 is to retrieve that value from a private inner class.

private String test1;
private String test2;
private AsyncCallback<GetArticleResult> getArticleCallback=new AsyncCallback<GetArticleResult>(){
@Override
        public void onFailure(Throwable caught) {
            // TODO Auto-generated method stub
            loadingPresenter.hide();
        }

        @Override
        public void onSuccess(GetArticleResult result) {
            test1=result.getVal();
            test2="123";

        }
};
private class  InlineHTMLContextMenuHandler implements ContextMenuHandler {



        @Override
        public void onContextMenu(ContextMenuEvent event) {
            System.out.println(test1);
            System.out.println(test2);
        }
}

Now, I click the Button 1 first & it get Data from DB, then I click the button 2. Then I get the output: test1=Null; test2="123"; when debugging the test1 show real value, not null. For test1 variable, If I access it from a method normally then it will be fine but if accessing it from private inner class then I can't get its value.

What wrong? This so weird?

Was it helpful?

Solution

I did a test with GWT 2.5.1 and the problem did not happen. Most likely your problem is in the class GetArticleResult.

Here is the code I used:

private String test1 = null;
private String test2 = null;

private AsyncCallback<GetArticleResult> callback = new AsyncCallback<GetArticleResult>() {
    @Override
    public void onFailure(Throwable throwable) {
        Window.alert(throwable.getClass().getName() + " - " + throwable.getMessage());
    }

    @Override
    public void onSuccess(GetArticleResult s) {
        test1 = s.msg;
        test2 = "123";
        Window.alert(test1 + " - " + test2);
    }
};

private class MyValueChangeHandler implements ValueChangeHandler<String> {
    @Override
    public void onValueChange(ValueChangeEvent<String> stringValueChangeEvent) {
        Window.alert(test1 + " - " + test2);
    }
}

And my GetArticleResult is very simple:

public class GetArticleResult implements IsSerializable {

    public String msg;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top