1- 长 xValue = someValue;
2- 长 yValue = someValue;
3- long otherTolalValue = (long)(xValue - yValue);
那行代码给了我以下异常:
java.lang.ClassCastException: java.lang.Double cannot be cast to java.lang.Long.
- 更新 :
代码段:
StackedBarChart<String,Number> sbc = new StackedBarChart<String,Number>();
XYChart.Series<String, Number> series = new XYChart.Series<String, Long>();
series.getData.add(new XYChart.Data<String, Number>("X1",150));
series.getData.add(new XYChart.Data<String, Number>("X2",50));
sbc.getData.add(series);
long dif = getDif(sbc);
long getDif(XYChart barChart){
XYChart.Series series = (XYChart.Series).getData().get(0);
// X1 at zero position i dont have to use iIterator now.
XYChart.Data<String, Long> seriesX1Data = series.getData().get(0);
XYChart.Data<String, Long> seriesX2Data = series.getData().get(1);
long x1Value = seriesX1Data.getYValue();
long x2Value = seriesX1Data.getYValue();
// line - 3 - exception on the next line
// -4- long value = (x1Value) - (x2Value);
long value = (long)(x1Value) - (long)(x2Value);
return value;
}
- 调试后我发现了。
seriesX1Data,seriesX2Data
包含双精度值as the passed chart has Number type
,但 getYvalue() 返回 long,这就是为什么程序在运行时崩溃并出现该异常,但是当我在线投射时,为什么投射不成功。我认为编译器看到该类型已经很长了!