I'm trying to send an matrix of data to a JavaFX application so I can input it as data for a graph and show it to the user.
But after adding this method
public void go(int[][] p) {
m = p;
launch();
}
launch ()
will call my main method and eventually loop infinitely
If I do the same thing without sending my data it'll run just fine, and I can't seem to understand what causes this or how to fix it
Here are the full classes I'm using, which are mostly copypasted from the JavaFX documentation, with the minor changes of launching from outside the app class and sending the matrix when I do.
Graficatest.java
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;
import javafx.stage.Stage;
public class GraficaTest extends Application {
/**
* @param args the command line arguments
*/
private int[][] m = null;
public GraficaTest() {
}
public int[][] f(){
return m;
}
public void go(int[][] p) {
m = p;
launch();
}
@Override
public void start(Stage stage) throws Exception {
stage.setTitle("Ejemplo Grafica");
//Defino los ejes
final NumberAxis xAxis = new NumberAxis();
xAxis.setLabel("Mes");
final NumberAxis yAxis = new NumberAxis();
//Creo la grafica
final LineChart<Number, Number> lineChart = new LineChart<Number, Number>(xAxis, yAxis);
lineChart.setTitle("Gastos Mensuales");
//Nueva Serie de Datos
XYChart.Series series = new XYChart.Series();
series.setName("Mis gastos");
int[][] ff = f();
//Ingreso los datos
series.getData().add(new XYChart.Data(ff[0][0], ff[0][1]));
series.getData().add(new XYChart.Data(2, 15));
series.getData().add(new XYChart.Data(3, 23));
series.getData().add(new XYChart.Data(4, 30));
series.getData().add(new XYChart.Data(6, 11));
//Lo meto en la "Escena"
Scene scene = new Scene(lineChart, 800, 600);
lineChart.getData().add(series);
//Lo muestro
stage.setScene(scene);
stage.show();
}
}
Main.java
public class Main{
public static void main(String[] args) throws Exception {
int[][] m = new int[10][2];
for (int i = 0; i < 10; i++) {
m[i][0] = i;
}
m[0][1] = 3;
m[1][1] = 5;
m[2][1] = 10;
m[3][1] = 22;
m[4][1] = 8;
m[5][1] = 5;
m[6][1] = 66;
m[7][1] = 33;
m[8][1] = 1;
m[9][1] = 0;
GraficaTest g = new GraficaTest();
// g.setData(m);
g.go(m);
}
}
Here's the log:
run:
Exception in Application start method
Exception in thread "main" java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$155(LauncherImpl.java:182)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NullPointerException
at graficatest.GraficaTest.start(GraficaTest.java:58)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
at com.sun.glass.ui.gtk.GtkApplication.lambda$null$49(GtkApplication.java:139)
... 1 more
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)