У меня есть приложение javafx + spring.
Приложение прослушивает последовательный порт, читает данные и показывает их в пользовательском интерфейсе. Проблема, вызванная NPE с классом Controller для outputLoggerFile и serialPort для одного класса.
Это мой файл конфигурации с PropertySource, поэтому моя среда должна знать об этих свойствах. SpringConfig
@Configuration
@PropertySource({"classpath:com.properties", "classpath:application.properties"})
@ComponentScan
public class SpringConfig {
@Bean
public SerialPort serialPort(@Value("${serialPort.portName}") String portName){
return new SerialPort(portName);
}
@Bean
public AnnotationMBeanExporter annotationMBeanExporter(){
AnnotationMBeanExporter annotationMBeanExporter = new AnnotationMBeanExporter();
annotationMBeanExporter.addExcludedBean("dataSource");
return annotationMBeanExporter;
}
}
Этот класс класса устанавливает мои свойства для объекта SerialPort, внедряет класс EventListener и открывающее соединение. Работает отлично. ComReader
@Scope("singletone")
@Component
public class ComReader {
@Autowired
private EventListener eventListener;
@Autowired
public SerialPort serialPort;
@Value("${serialPort.baudRate}")
private int baudRate;
@Value("${serialPort.dataBits}")
private int dataBits;
@Value("${serialPort.stopBits}")
private int stopBits;
@Value("${serialPort.parity}")
private int parity;
@PostConstruct
public void init(){
try {
System.out.println("Opening port: " + serialPort.getPortName());
serialPort.openPort();
serialPort.setParams(baudRate,dataBits,stopBits,parity);
serialPort.addEventListener(eventListener, 1);
} catch (SerialPortException e) {
e.printStackTrace();
}
}
}
В классе задачи все отлично работает, кроме любых классов / полей, которые я хочу здесь вставить.
Контроллер
@org.springframework.stereotype.Controller
public class Controller {
@Value("${logger.outputFilePath}")
private String outputLoggerFile;
private SerialPort serialPort;
@Autowired
public void setSerialPort(SerialPort serialPort) {
this.serialPort = serialPort;
}
private static ObservableList<CallDetailRecord> list = FXCollections.observableArrayList();
@FXML
void initialize(){
Timer scheduler = new Timer();
scheduler.schedule(new TimerTask() {
@Override
public void run() {
if (serialPort.isOpened()) circlePortStatus.setFill(Color.GREEN); //(NPE HERE)
else circlePortStatus.setFill(Color.RED);
}
}, 5_000, 60_000);
counterCol.setCellValueFactory(new PropertyValueFactory<>("id"));
startTimeCol.setCellValueFactory(new PropertyValueFactory<>("startTime"));
stopTimeCol.setCellValueFactory(new PropertyValueFactory<>("stopTime"));
numberACol.setCellValueFactory(new PropertyValueFactory<>("numberB"));
numberBCol.setCellValueFactory(new PropertyValueFactory<>("numberA"));
rescodeCol.setCellValueFactory(new PropertyValueFactory<>("resultCode"));
subACol.setCellValueFactory(new PropertyValueFactory<>("subscriberB"));
subBCol.setCellValueFactory(new PropertyValueFactory<>("subscriberA"));
table.setItems(list);
Label webLinkLabel = new Label("Веб ресурс");
AppStart appStart = new AppStart();
webLinkLabel.setOnMouseClicked(event -> appStart.getHostServices().showDocument(getURLPropertie()));
webLink.setGraphic(webLinkLabel);
Label logsLinkLabel = new Label("Логи");
logsLinkLabel.setOnMouseClicked(event -> appStart.getHostServices().showDocument(outputLoggerFile)); //(NPE HERE)
logsLink.setGraphic(logsLinkLabel);
}
public void addCdr(CallDetailRecord cdr){
list.add(cdr);
list.sort(Comparator.comparingInt(CallDetailRecord::getId).reversed());
}
private String getURLPropertie(){
try(InputStream is = new FileInputStream(Objects.requireNonNull(getClass().getClassLoader().getResource("application.properties")).getFile())){
Properties prop = new Properties();
prop.load(is);
return prop.getProperty("url.link");
} catch (IOException e) {
e.printStackTrace();
}
return "https://google.com";
}
}
Это код, который загружает и отображает F XML:
this.primaryStage = primaryStage;
Platform.setImplicitExit(false);
Parent root = FXMLLoader.load(getClass().getResource("/primal.fxml"));
primaryStage.setTitle("NIIAR");
primaryStage.getIcons().add(new Image("/icon.png"));
primaryStage.setScene(new Scene(root, 1400, 900));
createTray();
primaryStage.show();
Если я пытаюсь отладить в другом классе, который, используя Controller, показывает, что переменная outputLoggerFile содержит мое свойство ie , Я понятия не имею, почему.
источников - https://github.com/mindgame73/CDRListener-FX