Aman Parmar Aman Parmar - 2 months ago 25
Java Question

Javafx changes values after launching launch(args)

OK I have two arrays one with string and one with int. Problem is I'm using barchart in javafx. Now none of array is empty. But after the statement Launch(args) runs in main methods it turns them into null and zero values.

public static void main(String[] args) {
HistoGui BH = new HistoGui();
BH.dataProcessor();
BH.test();
launch(args);
}


here when i run test() it shows value of my both array. I have put another test() just before adding those values in barchart. But suddenly they converts into null and zeros

char a = 'A';
test();
for(int i=0; i<26; i++){
series1.getData().add(new XYChart.Data(Character.toString(a), 5));
a++;
}


here's the output

Total Characters = 65872
A 7
B 3
C 5
D 2
E 4
F 3
G 2
H 4
I 12
J 1
K 0
L 2
M 3
N 4
O 3
P 3
Q 0
R 3
S 7
T 11
U 1
V 1
W 4
X 0
Y 1
Z 0
null 0
null 0
null 0
null 0
null 0
null 0
null 0
null 0
null 0
null 0
null 0
null 0
null 0
null 0
null 0
null 0
null 0
null 0
null 0
null 0
null 0
null 0
null 0
null 0
null 0
null 0

Answer

You are initializing your items in the wrong place. You should put those calls in an init method or in the start method, depending on your uses for them. Reference https://docs.oracle.com/javase/8/javafx/api/javafx/application/Application.html for info on the Life-cycle of the Javafx Application.

EDIT 1:

Try

public void init(){
    HistoGui BH = new HistoGui();
    BH.dataProcessor();
    BH.test();
}