I am using GWT and overlays, JavaScript overlay types - not UI, to read an array of doubles in a JSON input and send it to Highcharts. As long as no data points are null the below works fine.

public final native JsArrayNumber getPoints() /*-{
    return this.points;
}-*/;

However, null represents a missing value in Highcharts. Unfortunately there is no way to know which index is null, the get() method will throw HostedModeException stating that

Something other than a double was returned from JSNI method '@com.google.gwt.core.client.JsArrayNumber::get(I)': JS value of type undefined, expected double

Interestingly, I may myself produce nulls and the toString() method works just fine.

JsArrayNumber test = jsSeries.getPoints();
test.set(test.length()+2, 24.2);
System.out.println(test.toString());
System.out.println(test.length());
for (int i = 0; i < test.length(); i++) {
    System.out.println(test.get(i));            
}

Output:
163,1,276,0,547,0,628,,,24.2
10
163.0
1.0
276.0
0.0
547.0
0.0
628.0
(and here comes the exception at the first null entry).

It strikes me as odd that I am not able to query the null values but I am able to produce them. I am sure there are valid reasons for this and I am very new to GWT.

What would be the appropriate method to use overlays to read in an array of double-values when some of the entries may be null?

(For now I just added another function isNull(index) so that I can check this myself).

1

There are 1 answers

2
Colin Alworth On BEST ANSWER

In Java, double is a value type, and cannot contain a null, whereas in JavaScript types are managed a little more flexibly, since you refer to everything as a var. Event something like null + null is legal, as JavaScript will coerce the types to have that make sense.

The object type java.lang.Double can be assigned a null value, but JSNI methods cannot return Double. Your isNull check is probably the best way forward, but you might consider adding a Double method that checks for null, otherwise returns the real value:

public Double getWithNull(int i) {
  if (isNull(i)) {
    return null;
  } else {
    return get(i);
  }
}

https://developers.google.com/web-toolkit/doc/latest/DevGuideCodingBasicsJSNI#passing-javascript discusses the types that can be returned from JSNI.