I have been stuck on this for a while and I am really at my last hope. I really have no idea what the problem can be. I have a GUI project using what I hope is correct MVP form and my problem is I have a presenter object that becomes null in my view class when being used within any method. So far the gui should load and user should hit the button, the method calls the presenter object's method to do something but it is always null. I tried passing into the view class's constructor, making it a object only within the view class, etc and I can't to fix it. Any help is appreciated. I have no idea what is going on and it does not seem to do this to any other object I think
main.java
package main;
import model.Model;
import presenter.Presenter;
import view.View;
public class Main
{
public static void main(String[] args)
{
View view = new View();
Model model = new Model();
Presenter presenter = new Presenter(view, model);
view.setPresenter(presenter);
// this was a way I found on stack overflow to call
// another class that inhierts the application class
// and be able to have args passed if needed
View.launch(View.class, args);
}
}
View.java
package view;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
import presenter.Presenter;
public class View extends Application
{
private Presenter presenter;
private Button test;
private Views views;
public final static String LOGINVIEW = "loginscreen";
public final static String LOGINVIEWFILE = "FXMLViewLogin.fxml";
public final static String CHATVIEW = "chatscreen";
public final static String CHATVIEWFILE = "FXMLViewChat.fxml";
public View()
{
// init Views class
views = new Views();
}
public void setPresenter(Presenter pres)
{
this.presenter = pres;
}
@Override
public void start(Stage primaryStage)
{
loadViews();
primaryStage.setScene(setView(LOGINVIEW));
primaryStage.setTitle("");
primaryStage.show();
}
private void loadViews()
{
views.loadScreen(LOGINVIEW, LOGINVIEWFILE);
views.loadScreen(CHATVIEW, CHATVIEWFILE);
}
private Scene setView(String name)
{
return views.setView(name);
}
@FXML
private void test(ActionEvent event)
{
//***********************************************************
// ALWAYS NULL IN THIS EVENT METHOD METHOD AND ANY OTHER METHOD FOR THAT FACT AFTER setPresenter
if(presenter == null)
System.out.println("Why is this null right here?");
}
}
Presenter.java
package presenter;
import view.View;
import model.Model;
public class Presenter
{
private Model model;
private View view;
public Presenter(View view, Model model)
{
this.model = model;
this.view = view;
}
// any methods start here
}
Views.java
package view;
import java.util.HashMap;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
public class Views
{
private final HashMap<String, Parent> view;
public Views()
{
this.view = new HashMap<>();
}
public void addView(String name, Parent screen)
{
view.put(name, screen);
}
public boolean loadScreen(String name, String file)
{
try
{
FXMLLoader myLoader = new FXMLLoader(getClass().getResource(file));
Parent loadScreen = (Parent) myLoader.load();
addView(name, loadScreen);
return true;
}
catch (Exception e)
{
return false;
}
}
public Scene setView(final String name)
{
Scene scene = null;
if (view.get(name) != null)
scene = new Scene(getParentNode(name));
unloadView(name);
return scene;
}
private boolean unloadView(String name)
{
return view.remove(name) != null;
}
private Parent getParentNode(String name)
{
return view.get(name);
}
}