我正在开发一个Java项目。该项目正常运行,但是我需要使用图形和UI使其变得很酷,因此我继续使用JavaFx。该计划很简单。我正在使用外部文件来调用数字,并且取决于数字是否可以被2整除,这三个按钮的背景颜色将不同;红色或绿色。我使用setStyle根据输入更改背景颜色,它给出了Null Pointer Exception,我不明白(如果输入是在程序中还是在逻辑上)(输入和输出在Java应用程序中均正常工作)变量k是程序的不同部分,但它正常工作,并且将返回1或0。>
#lang racket
(struct worker (name work study ent))
(define workers(list
(worker '(bill) '(none) '(medium) '(none))
(worker '(jill) '(high) '(low) '(medium))
(worker '(tim) '(vhigh) '(none) '(vhigh))
(worker '(gary) '(medium) '(high) '(medium))
(worker '(samantha) '(vlow) '(vlow) '(medium))
(worker '(holly) '(vlow) '(low) '(low))
(worker '(ryan) '(low) '(low) '(low))
(worker '(quin) '(low) '(medium) '(vlow))
(worker '(lisa) '(medium) '(vlow) '(high))
(worker '(jennifer) '(low) '(low) '(vlow))
(worker '(jeff) '(high) '(low) '(high))
(worker '(george) '(medium) '(vhigh) '(medium))
(worker '(beth) '(none) '(none) '(low))
(worker '(maria) '(vlow) '(medium) '(low))
(worker '(simon) '(medium) '(high) '(high))
))
(define convert
(lambda (input)
(match (input
('none 0)
('vlow 1)
('low 2)
('medium 3)
('high 4)
('vhigh 5)
)
)
)
)
(define (hardworker workers)
(map(lambda(workers)...
////
File file = new File("count.txt");
Scanner scanner = new Scanner(file);
int count =0;
while(scanner.hasNextInt()){
count =scanner.nextInt();
}
if (k == 0){
if ( count % 2 == 1){
waterButton.setStyle("-fx-background-color: #ff0000; ");
waterButton2.setStyle("-fx-background-color: green");
waterButton3.setStyle("-fx-background-color: green");
}
else if (count % 2 ==0) {
waterButton.setStyle("-fx-background-color: green");
waterButton2.setStyle("-fx-background-color: green");
waterButton3.setStyle("-fx-background-color: green");
}
}
else {
waterButton.setStyle("-fx-background-color: #ff0000; ");
waterButton2.setStyle("-fx-background-color: #ff0000; ");
waterButton3.setStyle("-fx-background-color: #ff0000; ");
System.exit(0);
}
}catch (IOException e){
System.out.println(e.getMessage());
}
答案 0 :(得分:2)
在NullPointerException
行上的waterButton.setStyle("-fx-background-color: #ff0000; ");
清楚地表明变量waterButton
是null
(就像已经猜到的那样)。
因为您说您的程序正在运行,所以我假设您发布的代码在该类的构造函数中(或从该构造函数调用),因为在构造函数中,FXML部分未初始化,这意味着所有用@FXML
注释的字段都是null
,直到调用initialize方法(在构造对象之后调用)。
无法更改此行为,因为这就是FXML注入的工作方式,但是如果您将代码放在类的initialize
方法中,它应该可以工作。< / p>