0

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)
  • What is the problem? What do you expect to happen and what happens instead? – JB Nizet Jun 06 '16 at 07:26
  • I expect my Graph to be created with the sets of data I give it (Right now they're manually inputed), but it never even gets to those lines, the start method is never called and launch() calls my main method instead – Nicolás Videla Jun 06 '16 at 07:28
  • 1
    This should result in a NPE instead of looping forever... – fabian Jun 06 '16 at 07:33
  • I added the log, and yeah. It does say NPE, but when debugging, I can see what happens is the main method gets called over and over everytime launch() runs, and the main method is who calls the method that calls launch() – Nicolás Videla Jun 06 '16 at 07:35
  • launch() is a static method. Read its documentation. It doesn't call your main method. Instead, it does what its documentation explains: create an instance of your Application class (**different** from the one you created yourself, and which thus does not have its `m` set), and starts it. Hence the NullPointerException that you get (and that, for mysterious reasons, you didn't even talk about when I asked what happened instead). Why don't you simply initialize `m` in the GraficaTest constructor, or in its start method, and replace your main method by `Application.launch(GraficaTest.class);`? – JB Nizet Jun 06 '16 at 07:49
  • What I want to do is use the JavaFX application I'm creating to be called from the system to be able to graph my data, not use the application as the main app or a standalone. This data would be stored in my main class and I don't think creating a new instance of it would be the solution – Nicolás Videla Jun 07 '16 at 21:54

0 Answers0