0

There are a lot of demo programs for JavaFX graphics features like the following one for a JavaFX LineChart:

public class LineChartSample extends Application {@Override public void start(Stage stage) {
    stage.setTitle("Line Chart Sample");
    //defining the axes
    final NumberAxis xAxis = new NumberAxis();
    final NumberAxis yAxis = new NumberAxis();
    xAxis.setLabel("Number of Month");
    //creating the chart
    final LineChart<Number,Number> lineChart = 
            new LineChart<Number,Number>(xAxis,yAxis);

    lineChart.setTitle("Stock Monitoring, 2010");
    //defining a series
    XYChart.Series series = new XYChart.Series();
    series.setName("My portfolio");
    //populating the series with data
    series.getData().add(new XYChart.Data(1, 23));
    series.getData().add(new XYChart.Data(2, 14));
    //etc..

    Scene scene  = new Scene(lineChart,800,600);
    lineChart.getData().add(series);

    stage.setScene(scene);
    stage.show();
}public static void main(String[] args) {
    launch(args);
}

The limiting factor for me is the data pairs have to be hard-coded before the application works. I would instead like to populate the line chart with data read from an external file, which in my case is an Excel spreadsheet containing numerical data.

This data is retrieved as an ArrayList of doubles. The method for getting the data is in another package, and works without error in other applications. The method is called like this:

 private ArrayList<Double> teamstats;
 ...
 teamstats = ExcelManager.getTeamMavgStats("D1", "Arsenal");
 //iterate on teamstats to populate the series...

However when I introduce the above line of code to begin to replace the hard coded getData().add calls, I get the following error when trying to run the program from Netbeans 8.0.2:

run:
Exception in Application start method
java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at     sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at     sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.    java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at     com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImp    l.java:389)
    at         com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at     sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at     sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.    java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: 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$156(Launcher    Impl.java:182)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NullPointerException
    at movavg.excel.ExcelManager.getTeamMavgStats(ExcelManager.java:163)
    at movavg.ui.LineChartSample.start(LineChartSample.java:73)
    at     com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$163(Launche    rImpl.java:863)
    at     com.sun.javafx.application.PlatformImpl.lambda$runAndWait$176(PlatformImpl.ja    va:326)
    at     com.sun.javafx.application.PlatformImpl.lambda$null$174(PlatformImpl.java:295    )
    at java.security.AccessController.doPrivileged(Native Method)
    at    com.sun.javafx.application.PlatformImpl.lambda$runLater$175(PlatformImpl.java    :294)
    at     com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:    95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at     com.sun.glass.ui.win.WinApplication.lambda$null$149(WinApplication.java:191)
    ... 1 more
Exception running application movavg.ui.LineChartSample
Java Result: 1

I find it hard to know what to do about this as I have never encountered this kind of exception before.

Rusty
  • 79
  • 1
  • 13
  • Use a for loop and System.out.println() to get an idea of how the data looks that is stored in the ArrayList. Once you see how the data looks you can then determine the right way to handle it. – SedJ601 Jan 16 '17 at 06:46
  • I cant access the ArrayList because it is the statement teamstats = ExcelManager.getTeamMavgStats("D1", "Arsenal"); that throws the error. – Rusty Jan 16 '17 at 06:49
  • I am not understanding? – SedJ601 Jan 16 '17 at 06:50
  • How about you try System.out.println(ExcelManagerger.getTEamMavgSats("your stings inputs"); – SedJ601 Jan 16 '17 at 06:50
  • if teamstats is an arraylist of doubles you should be able to say for(int i = 0; i < teamstats.size(); i++){System.out.println(teamstats.get(i))} – SedJ601 Jan 16 '17 at 06:52
  • You maybe need to look at the ExcelManager class to see how it works and what it returns – SedJ601 Jan 16 '17 at 06:54
  • also this is incorrect: teamstats = ExcelManager.getTeamMavgStats("D1", "Arsenal");. It should be teamstats.add(ExcelManager.getTeamMavgStats("D1", "Arsenal")); – SedJ601 Jan 16 '17 at 06:57
  • System.out.println produces the same run time error. – Rusty Jan 16 '17 at 08:27
  • 1
    The stack trace clearly tells you that you have a null pointer exception on line 163 of `ExcelManager.java`. The linked question explains what a null pointer exception is and how to diagnose it. – James_D Jan 16 '17 at 12:55

0 Answers0