Access to variables in a method declaration

I am encoding a servlet in GWT, and I really don't understand what happens to a data variable declared as global in a function. Perhaps this is a more general Java problem related to the use of variables in dynamic function declarations, but I can not find the answer to this question.

This is my code:

private AbstractDataTable fetchDataFromServer() { final DataTable data = DataTable.create(); System.out.println("1 Table has " + data.getNumberOfColumns() + " columns"); System.out.println("1 Table has " + data.getNumberOfRows() + " rows"); try { RequestBuilder rb = new RequestBuilder(RequestBuilder.GET, "/mongo.json"); rb.setCallback(new RequestCallback() { @Override public void onResponseReceived(Request request, Response response) { JSONValue value = JSONParser.parse(response.getText()); JSONObject productsObj = value.isObject(); JSONArray radiation = productsObj.get("ShortWave_Rad").isArray(); if (radiation != null) { data.addColumn(ColumnType.NUMBER, "Horizon"); data.addColumn(ColumnType.NUMBER, "Value"); System.out.println("Values " + radiation.size()); for (int i=0; i<=radiation.size()-1; i++) { JSONObject productObj = radiation.get(i).isObject(); data.addRows(radiation.size()); data.setValue(i, 0, productObj.get("horizon").isNumber().doubleValue()); data.setValue(i, 1, productObj.get("value").isNumber().doubleValue()); } System.out.println("2 Table has " + data.getNumberOfColumns() + " columns"); System.out.println("2 Table has " + data.getNumberOfRows() + " rows"); } } @Override public void onError(Request request, Throwable exception) { Window.alert("Error occurred" + exception.getMessage()); } }); rb.send(); } catch (RequestException e) { Window.alert("Error occurred" + e.getMessage()); } System.out.println("3 Table has " + data.getNumberOfColumns() + " columns"); System.out.println("3 Table has " + data.getNumberOfRows() + " rows"); return data; } 

And this is the result that I get:

1 table has 0 colonies

1 table has 0 rows

2 table has 2 columns

2 table has 10 rows

3 table has 0 colonies

3 table has 0 rows

Where did the data content go after using the onResponseReceived declaration?

Many thanks for your help.

+4
source share
1 answer

Your onResponseReceived is called after the asynchronous call is answered . It is very likely that the instructions below will be followed until a response is received.

 System.out.println("3Table has colums " + data.getNumberOfColumns()); System.out.println("3Table has rows " + data.getNumberOfRows()); 

Use some status flag to check the status of the callback before printing the size at the end.

+2
source

Source: https://habr.com/ru/post/1446131/


All Articles