0

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 :(

cy8el1us
  • 3
  • 4
  • Surely `settingService` is never initialized? Am I missing something? – James_D Aug 10 '17 at 16:38
  • OK I understand but I don't know where should I initialize settingService. In my understanding, the settingService should map save method through my interface and so on... and it doesn't make any sense if I remove paramters it works! – cy8el1us Aug 10 '17 at 16:49
  • I'm not sure I believe that removing the parameters makes it work: I suspect you changed something else at the same time. If you don't believe that `settingService` is null, or think it shouldn't be null for some reason, you need to post the stack trace in the question, identify which line in the code you posted is the line throwing the exception, and show the code where you initialize `settingService`. – James_D Aug 10 '17 at 16:59

0 Answers0