I'm trying to create a LineChart in JavaFX and everything works except adding data to the chart. It works when there is no data added to the chart but as soon as I try to add data it gives me a long error basically saying that it can't find the flatMap method.
Caused by: java.lang.NoSuchMethodError: 'javafx.beans.value.ObservableValue javafx.beans.property.ObjectProperty.flatMap(java.util.function.Function)'
I've read somewhere that it can be because of my JavaFX version but I can't seem to figure it out.
Java code:
package GUI;
import javafx.fxml.FXML;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;
public class GUIStockLinegraphController {
@FXML
private LineChart<Number, Number> chart;
@FXML
private NumberAxis xAxis;
@FXML
private NumberAxis yAxis;
public void initialize(){
XYChart.Series<Number, Number> series = new XYChart.Series<>();
series.setName("Test Chart");
series.getData().add(new XYChart.Data<>(1, 2)); //The error comes up when this line is added
chart.getData().add(series);
}
}
FXML code:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.chart.LineChart?>
<?import javafx.scene.chart.NumberAxis?>
<LineChart fx:id="chart" xmlns="http://javafx.com/fxml/11"
xmlns:fx="http://javafx.com/fxml/1"
fx:controller="GUI.GUIStockLinegraphController"
layoutX="5.0" layoutY="10.0"
prefHeight="233.0" prefWidth="870.0" >
<xAxis>
<NumberAxis side="BOTTOM" fx:id="xAxis"/>
</xAxis>
<yAxis>
<NumberAxis side="LEFT" fx:id="yAxis"/>
</yAxis>
</LineChart>
Full stacktrace:
Exception in Application start method
java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:118)
at java.base/java.lang.reflect.Method.invoke(Method.java:580)
at javafx.graphics@22/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:464)
at javafx.graphics@22/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:364)
at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:103)
at java.base/java.lang.reflect.Method.invoke(Method.java:580)
at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1135)
Caused by: java.lang.RuntimeException: Exception in Application start method
at javafx.graphics@22/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:893)
at javafx.graphics@22/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:196)
at java.base/java.lang.Thread.run(Thread.java:1583)
Caused by: javafx.fxml.LoadException:
/F:/chartsDemo/target/classes/GUI/Stock-linegraph.fxml
/F:/chartsDemo/target/classes/GUI/Stock-view.fxml:59
at javafx.fxml@22/javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2727)
at javafx.fxml@22/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2697)
at javafx.fxml@22/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2568)
at javafx.fxml@22/javafx.fxml.FXMLLoader$IncludeElement.constructValue(FXMLLoader.java:1160)
at javafx.fxml@22/javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:759)
at javafx.fxml@22/javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2858)
at javafx.fxml@22/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2654)
at javafx.fxml@22/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2568)
at javafx.fxml@22/javafx.fxml.FXMLLoader.load(FXMLLoader.java:2536)
at com.example.demo/GUI.GUIMainApplication.start(GUIMainApplication.java:41)
at javafx.graphics@22/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:839)
at javafx.graphics@22/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:483)
at javafx.graphics@22/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:456)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:400)
at javafx.graphics@22/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:455)
at javafx.graphics@22/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at javafx.graphics@22/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics@22/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:184)
... 1 more
Caused by: java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:118)
at java.base/java.lang.reflect.Method.invoke(Method.java:580)
at com.sun.javafx.reflect.Trampoline.invoke(MethodUtil.java:76)
at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:103)
at java.base/java.lang.reflect.Method.invoke(Method.java:580)
at javafx.base/com.sun.javafx.reflect.MethodUtil.invoke(MethodUtil.java:273)
at javafx.fxml@22/com.sun.javafx.fxml.MethodHelper.invoke(MethodHelper.java:84)
at javafx.fxml@22/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2693)
... 17 more
Caused by: java.lang.NoSuchMethodError: 'javafx.beans.value.ObservableValue javafx.beans.property.ObjectProperty.flatMap(java.util.function.Function)'
at javafx.controls@22/javafx.scene.chart.XYChart$Data$4.invalidated(XYChart.java:1371)
at javafx.base/javafx.beans.property.ObjectPropertyBase.markInvalid(ObjectPropertyBase.java:112)
at javafx.base/javafx.beans.property.ObjectPropertyBase.set(ObjectPropertyBase.java:147)
at javafx.controls@22/javafx.scene.chart.XYChart$Data.setNode(XYChart.java:1406)
at javafx.controls@22/javafx.scene.chart.LineChart.createSymbol(LineChart.java:531)
at javafx.controls@22/javafx.scene.chart.LineChart.seriesAdded(LineChart.java:424)
at javafx.controls@22/javafx.scene.chart.XYChart.lambda$new$1(XYChart.java:164)
at javafx.base/com.sun.javafx.collections.ListListenerHelper$SingleChange.fireValueChangedEvent(ListListenerHelper.java:164)
at javafx.base/com.sun.javafx.collections.ListListenerHelper.fireValueChangedEvent(ListListenerHelper.java:73)
at javafx.base/javafx.collections.ObservableListBase.fireChange(ObservableListBase.java:233)
at javafx.base/javafx.collections.ListChangeBuilder.commit(ListChangeBuilder.java:482)
at javafx.base/javafx.collections.ListChangeBuilder.endChange(ListChangeBuilder.java:541)
at javafx.base/javafx.collections.ObservableListBase.endChange(ObservableListBase.java:205)
at javafx.base/javafx.collections.ModifiableObservableListBase.add(ModifiableObservableListBase.java:155)
at java.base/java.util.AbstractList.add(AbstractList.java:113)
at com.example.demo/GUI.GUIStockLinegraphController.initialize(GUIStockLinegraphController.java:24)
at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:103)
... 24 more
Exception running application GUI.GUIMainApplication
I tried changing around the Maven dependencies but that didn't fix anything.
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-controls</artifactId>
<version>22</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-fxml</artifactId>
<version>22</version>
</dependency>
<dependency>
<groupId>org.controlsfx</groupId>
<artifactId>controlsfx</artifactId>
<version>11.1.2</version>
</dependency>