I am building a UI for user input and another UI for displaying histogram. I have developed two UIs and tested them separately. Each works perfectly, but when I try to compile them together, an error occurs.
This is the file for histogramController:
The following file has two scenes where I need to switch between the scenes.
public class HistogramController extends Application {
private Scene scene;
private Parent root;
int histogramSize ;
int bin;
int [] histArr;
int temp;
int[] cutoffs = new int[bin+1];
int[] counts = new int[bin];
public void start(Stage stage) throws IOException {
//the UI to prompt user input
root = FXMLLoader.load(getClass().getResource("hello-view.fxml"));
scene = new Scene(root , 503, 547);
stage.setScene(scene);
stage.show();
}
// the UI for histogram
public void createStage1(){
Stage primaryStage = new Stage();
Calculation();
Label labelInfo = new Label();
final CategoryAxis xAxis = new CategoryAxis();
final NumberAxis yAxis = new NumberAxis();
final BarChart<String,Number> barChart = new BarChart<>(xAxis,yAxis);
barChart.setCategoryGap(0);
barChart.setBarGap(0);
xAxis.setLabel("Range of Number of Passengers at 5min interval");
yAxis.setLabel("Number of Data point(s)")
XYChart.Series series1 = new XYChart.Series();
Scene scene = new Scene(root, 800, 450);
primaryStage.setTitle("WIA1002 HISTOGRAM");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch();
}
//calculation for the histogram
private void Calculation(){
//find required data
int min = histArr[0];
int max = histArr[histogramSize-1];
int increment = (max - min) / bin;
temp = min;
int counter = 0;
//find cutoffs
while (temp <= max) {
cutoffs[counter] = temp;
temp += increment;
counter++;
}
temp = 1;
int nextBin = cutoffs[temp];
counter = 0;
//find counts
for (int j = 0; j < histogramSize; j++) {
if (histArr[j] < nextBin)
counter++;
else {
temp++;
if (temp == cutoffs.length) {
counts[temp-2] = ++counter;
break;
}
counts[temp-2] = counter;
counter = 1;
nextBin = cutoffs[temp];
}
}
}
}
Apparently, I need input from user and store the values in the (histogramSize, bin, histArr ) variables in HistogramController class.
This is my input controller class.
public class InputController {
@FXML
private TextField bin;
@FXML
private TextField hist;
@FXML
private TextField size;
public void submit(javafx.event.ActionEvent actionEvent) throws IOException {
HistogramController input = new HistogramController();
input.histogramSize = Integer.parseInt(size.getText());
input.bin = Integer.parseInt(bin.getText());
String [] hists = hist.getText().split(", ");
input.histArr = new int [hists.length];
for (int y = 0; y< hists.length;y++) {
input.histArr[y] = Integer.parseInt(hists[y]);
}
FXMLLoader loader = new FXMLLoader(getClass().getResource("graph.fxml"));
Parent root = loader.load();
input.createStage1();
}
}
I want my program to display the histogram after user clicked the submit button. However, when I clicked the submit button, it shows me this error.
Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
Caused by: java.lang.reflect.InvocationTargetException
Caused by: java.lang.ArrayIndexOutOfBoundsException: Index 0 out of bounds for length 0
at com.example.histograms.HistogramController.Calculation(HistogramController.java:90)
at com.example.histograms.HistogramController.createStage1(HistogramController.java:41)
at com.example.histograms.InputController.submit(InputController.java:36)
I think this error is caused by I have a null histArr, bin and size, meaning the user input is not able to store in the variables in my HistogramController class. I need my program to receive the input from user and update the array immediately to output the histogram.