I keep getting java.lang.reflect.InvocationTargetException whenever I run my code
package test;
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 Test extends Application {
private static int[] xarray;
private static int[] yarray;
public static void main(String[] args) {
countgradient(3, 1, 1) ;
launch(args);
System.out.println(xarray[0]);
}
public static int[] countgradient( int cx, int cy, int con ){
int x1 = 1;
int y1 = (cx*x1+con)/cy ;
System.out.println(y1);
int x2 = 2;
int y2 = (cx*x2+con)/cy ;
System.out.println(y2);
int gradient = (y2-y1)/(x2-x1);
System.out.println("The gradient is " + gradient);
xarray[0] = x1;
xarray[1] = x2;
yarray[0] = y1;
yarray[1] = y2;
return xarray;
}
@Override
public void start(Stage stage) {
stage.setTitle("Line Chart Sample");
//defining the axes
final NumberAxis xAxis = new NumberAxis();
final NumberAxis yAxis = new NumberAxis();
//creating the chart
final LineChart<Number,Number> lineChart =
new LineChart<>(xAxis,yAxis);
//defining a series
XYChart.Series series = new XYChart.Series();
series.setName("Line");
//populating the series with data
series.getData().add(new XYChart.Data(xarray[0], yarray[0]));
series.getData().add(new XYChart.Data(xarray[1], yarray[1]));
Scene scene = new Scene(lineChart,800,600);
lineChart.getData().add(series);
stage.setScene(scene);
stage.show();
}
}
and I dont really understand the reasons for why I keep getting this problem
The full result that I get is
4
7
The gradient is 3
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:498)
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.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:498)
at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.NullPointerException
at test.Test.countgradient(Test.java:30)
at test.Test.main(Test.java:16)
... 11 more
Exception running application test.Test
C:\Users\Owner\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 3 seconds)
Is it caused by using the array to run the chart or is it something else, should I use a different way to define the array