I am working on personal application and trying to use javaFX. Actually I am stuck on connecting a controller and a service. the idea is to seperate the code to get a better architecture. I've searched many information but I couldn't find the right answer in my case. If someone has an idea, I'd appreciate.
Controller
public class SettingsController extends AbstractController implements Initializable {
private final static Logger logger = LoggerFactory.getLogger(SettingsController.class);
@FXML
public TextField logFilepath;
@FXML
public TextField outputFilepath;
@FXML
private Button saveSettings;
/**
*/
@Override
public void initialize(URL url, ResourceBundle rb) {
logger.debug("initialize");
//do something
}
/**
*/
public void save() {
logger.debug("save");
final String logPath = logFilepath.getText();
final String outputPath = outputFilepath.getText();
System.out.println(logPath);
settingService.save(logPath, outputPath);
Stage stage = (Stage) saveSettings.getScene().getWindow();
stage.close();
}
settingService Interface
public interface SettingService {
/**
*/
void save(final String logPath, final String outputPath);
}
settingServiceImpl
public class SettingServiceImpl implements SettingService {
/**
*/
private static final Logger logger = LoggerFactory.getLogger(SettingServiceImpl.class);
/**
*/
public SettingServiceImpl() {
super();
}
@Override
public void save(final String logPath, final String outputPath) {
logger.debug("save");
final Properties prop = new Properties();
OutputStream output = null;
try {
output = new FileOutputStream("settings.properties");
prop.setProperty("log_directory", logPath);
prop.setProperty("output_directory", outputPath);
prop.store(output, null);
} catch (IOException io) {
io.getMessage();
} finally {
if(output != null) {
try {
output.close();
} catch (IOException iot) {
iot.getMessage();
}
}
}
}
}
AbstractController
public abstract class AbstractController {
/**
*/
protected SettingService settingService;
public AbstractController() {
super();
}
}
When I run my application, It works until I am calling the method save from my controller -> I am always getting the same error as NPE
Caused by: java.lang.NullPointerException
at com.analyzer.controller.SettingsController.save(SettingsController.java:139)
If I remove the save method's parameter -> settingService.save(str, str) - and replace variables LogPath and outputPath by simple Strings, it works .. So it seems like I don't have the right to calling methods with parameters inside a controller handler.
worth ! I wanted to make a bit complex by declare a ServiceOut class which could help me to construct my In and Out services but it's the same :(