Two classes from one package FXMLLoader error - javafx

When I attempt to load an FXML file that is in the same package as the controller that is calling for the loading I get a ClassNotFound exception referring to the controller class of the FXML file that is being loaded. When I attempt to load an FXML file with the same name from a different package than the package containing the calling controller the file loads as expected.
Here is the code from the projectselectorcontroller class. The commented code is the line that causes the exception.
#Override
public void Response(KWMessage Message)
{
if(Message.getType() != -1)
{
switch(Message.getType())
{
case 6000:
Platform.runLater(new Runnable()
{
#Override
public void run()
{
//This file loads as expected
ExtendedNode node = mainController.load("/newproject/newproject.fxml");
//This file does not load
//ExtendedNode node = mainController.load("/projectselector/newproject.fxml");
NewProjectController controller = (NewProjectController)node.getController();
mainController.setCurrentNode(node);
}
});
}
}
}
Here is the load method. I have checked the URL and found that it is correct. Is it possible that two controller classes cannot reside in the same package?
public ExtendedNode load(String FXML)
{
ExtendedNode node;
URL location = getClass().getResource(FXML);
FXMLLoader fxmlLoader = new FXMLLoader();
fxmlLoader.setLocation(location);
fxmlLoader.setBuilderFactory(new JavaFXBuilderFactory());
try
{
node = new ExtendedNode();
node.setNode((Parent)fxmlLoader.load(location.openStream()));
node.setController((iMainController)fxmlLoader.getController());
node.getController().setMainController(this);
}
catch (IOException ex){node = null;}
return node;
}

I am able to load FXML files located in the same package as the controller class file without difficulty. I am similarly able to load FXML files located in another package available relative to the one in which the controller class resides.
A common reason for a ClassNotFound Exception while loading FXML markup is not properly naming the controller class in the root level node of your markup file. You need a fully qualified Java object name in the fx:controller attribute, eg:
<AnchorPane id="AnchorPane" fx:id="ServicesEditor" maxHeight="-Infinity" maxWidth="-Infinity"
minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0"
styleClass="stage-pane" xmlns:fx="http://javafx.com/fxml"
fx:controller="org.kkk.mm.censusassistant.GUIServicesEditorController">
I would first examine your markup files at both locations to assure that both had a fully qualified Java name for your controller class and that both identified the correct class.
If the controller class is properly identified in your project namespace and you are still getting a ClassNotFound exception, then I'll need more context in order to help you. Are you certain that it is the fxmlloader.load() method call that is generating this error? If not...
The logic you show for loading your markup seems convoluted. I'm not sure what the ExtendedNode objects are doing or why you're using them.
You're using:
FXMLLoader fxmlLoader = new FXMLLoader();
fxmlLoader.setLocation(location);
:
:
node.setNode((Parent)fxmlLoader.load(location.openStream()));
Because you've already set the URL for the FXMLLoader with setLocation, you do not then need to call fxmlLoader with an inputStream. You can just simply use fxmlLoader.load();

Related

Griffon "error injecting constructor" while trying to call loadFromFXML

I have a series of popups where I edit or view specific objects. I use these popups for editing various objects that are handled by and ORM (ORMLite), what I am trying to achieve is to have a generic/abstract class that implements similar behaviours through encapsulating methods. As I don't want to have the same FXML for all of the
popup dialogs what I came up with was to create a "template" FXML, load it through loadFXML() function provided by Griffon and store it in a Node object to be the root of the created Scene at the Abstract Class. I am familiar with dependency injection, but I am not aware of the AST of the framework so my Abstract class is able to call loadFromFXML() within the Abstract class I created.
I post my code here:
Concrete class implementing the abstract ViewPopUp class I created:
#ArtifactProviderFor(GriffonView.class)
public class VerConductoresView extends AbstractViewPopUp<ObservablePlanilla> {
private VerConductoresController controller;
private ConductoresModel model;
VerConductoresView() {
super(ObservablePlanilla.class, Conductor.class);
nodeM = new GridPane();
super.setController(controller);
}
#Override
public void initUI() {
Stage stage = (Stage) getApplication()
.createApplicationContainer(Collections.<String,Object>emptyMap());
stage.setTitle(getApplication().getConfiguration().getAsString("application.title"));
stage.setScene(init());
stage.sizeToScene();
getApplication().getWindowManager().attach("ver-conductores", stage);
}
}
Abstract view PopUp I created:
public abstract class AbstractViewPopUp<T> extends AbstractJavaFXGriffonView {
protected Class klazz;
protected Class<T> klazz2;
protected Scene viewScene;
protected ViewControllerPopUp viewController;
protected TableView tableView;
protected GridPane gridPane;
protected String[] ignoredNames;
protected String[] columnNames;
protected IModel<T> viewModel;
protected Node nodeM;
#MVCMember
public void setController(ViewControllerPopUp controller) {
this.viewController = controller;
}
AbstractViewPopUp(Class<T> k1, Class k2, Node node){
klazz = k2;
klazz2 = k1;
nodeM = node;
nodeM = loadFromFXML("com.softgan.viewPopUp");
nodeM = node;
}
AbstractViewPopUp(Class<T> k1, Class k2){
klazz = k2;
klazz2 = k1;
nodeM = loadFromFXML("com.softgan.viewPopUp");
}
protected Scene init() {
Scene scene = new Scene(new Group());
if (nodeM instanceof Parent) {
scene.setRoot((Parent) nodeM);
} else {
((Group) scene.getRoot()).getChildren().addAll(nodeM);
}
connectActions(nodeM, viewController);
connectMessageSource(nodeM);
return scene;
}
}
I want to load the FXML through the Abstract class and then store it so the concrete class can access the loaded FXML so I am able to manipulate its contents, adding labels and textfields dynamically. The problem seems to be that loadFromFXML is throwing a NullPointerException as it is not able to resolve the FXML file from the resources. I already tried to use an AST transformation to make it resources aware, but it seems to not be a valid approach as Guice is not able to resolve, I think, the ResourceHandler.
EDIT
This is the Stacktrace I am getting:
[griffon-pool-1-thread-2] WARN org.codehaus.griffon.runtime.core.controller.AbstractActionManager - An exception occurred when executing com.softgan.ConductoresController.view
griffon.exceptions.InstanceMethodInvocationException: An error occurred while invoking instance method com.softgan.ConductoresController.view()
at griffon.util.GriffonClassUtils.invokeExactInstanceMethod(GriffonClassUtils.java:3186)
Caused by: griffon.exceptions.GriffonException: An error occurred while executing a task inside the UI thread
at com.softgan.ConductoresController.view(ConductoresController.java:122)
at griffon.util.MethodUtils.invokeExactMethod(MethodUtils.java:407)
at griffon.util.MethodUtils.invokeExactMethod(MethodUtils.java:356)
at griffon.util.GriffonClassUtils.invokeExactInstanceMethod(GriffonClassUtils.java:3182)
Caused by: java.util.concurrent.ExecutionException: griffon.exceptions.InstanceNotFoundException: Could not find an instance of type com.softgan.VerConductoresView
... 4 more
Caused by: griffon.exceptions.InstanceNotFoundException: Could not find an instance of type com.softgan.VerConductoresView
Caused by: com.google.inject.ProvisionException: Unable to provision, see the following errors:
1) Error injecting constructor, java.lang.NullPointerException
at com.softgan.VerConductoresView.<init>(VerConductoresView.java:31)
while locating com.softgan.VerConductoresView
1 error
at com.google.inject.internal.InternalProvisionException.toProvisionException(InternalProvisionException.java:226)
at com.google.inject.internal.InjectorImpl$1.get(InjectorImpl.java:1053)
at com.google.inject.internal.InjectorImpl.getInstance(InjectorImpl.java:1086)
Caused by: java.lang.NullPointerException
at com.softgan.AbstractViewPopUp.<init>(AbstractViewPopUp.java:72)
at com.softgan.VerConductoresView.<init>(VerConductoresView.java:31)
at com.softgan.VerConductoresView$$FastClassByGuice$$d0c2bde8.newInstance(<generated>)
at com.google.inject.internal.DefaultConstructionProxyFactory$FastClassProxy.newInstance(DefaultConstructionProxyFactory.java:89)
at com.google.inject.internal.ConstructorInjector.provision(ConstructorInjector.java:114)
at com.google.inject.internal.ConstructorInjector.access$000(ConstructorInjector.java:32)
at com.google.inject.internal.ConstructorInjector$1.call(ConstructorInjector.java:98)
at com.google.inject.internal.ProvisionListenerStackCallback$Provision.provision(ProvisionListenerStackCallback.java:112)
at com.google.inject.internal.ProvisionListenerStackCallback$Provision.provision(ProvisionListenerStackCallback.java:120)
at com.google.inject.internal.ProvisionListenerStackCallback.provision(ProvisionListenerStackCallback.java:66)
at com.google.inject.internal.ConstructorInjector.construct(ConstructorInjector.java:93)
at com.google.inject.internal.ConstructorBindingImpl$Factory.get(ConstructorBindingImpl.java:306)
at com.google.inject.internal.InjectorImpl$1.get(InjectorImpl.java:1050)
... 1 more
[griffon-pool-1-thread-2] ERROR griffon.core.GriffonExceptionHandler - Uncaught Exception. Stacktrace was sanitized. Set System property 'griffon.full.stacktrace' to 'true' for full report.
griffon.exceptions.InstanceMethodInvocationException: An error occurred while invoking instance method com.softgan.ConductoresController.view()
at griffon.util.GriffonClassUtils.invokeExactInstanceMethod(GriffonClassUtils.java:3186)
Caused by: griffon.exceptions.GriffonException: An error occurred while executing a task inside the UI thread
at com.softgan.ConductoresController.view(ConductoresController.java:122)
at griffon.util.MethodUtils.invokeExactMethod(MethodUtils.java:407)
at griffon.util.MethodUtils.invokeExactMethod(MethodUtils.java:356)
at griffon.util.GriffonClassUtils.invokeExactInstanceMethod(GriffonClassUtils.java:3182)
Caused by: java.util.concurrent.ExecutionException: griffon.exceptions.InstanceNotFoundException: Could not find an instance of type com.softgan.VerConductoresView
... 4 more
Caused by: griffon.exceptions.InstanceNotFoundException: Could not find an instance of type com.softgan.VerConductoresView
Caused by: com.google.inject.ProvisionException: Unable to provision, see the following errors:
1) Error injecting constructor, java.lang.NullPointerException
at com.softgan.VerConductoresView.<init>(VerConductoresView.java:31)
while locating com.softgan.VerConductoresView
1 error
at com.google.inject.internal.InternalProvisionException.toProvisionException(InternalProvisionException.java:226)
at com.google.inject.internal.InjectorImpl$1.get(InjectorImpl.java:1053)
at com.google.inject.internal.InjectorImpl.getInstance(InjectorImpl.java:1086)
Caused by: java.lang.NullPointerException
at com.softgan.AbstractViewPopUp.<init>(AbstractViewPopUp.java:72)
at com.softgan.VerConductoresView.<init>(VerConductoresView.java:31)
at com.softgan.VerConductoresView$$FastClassByGuice$$d0c2bde8.newInstance(<generated>)
at com.google.inject.internal.DefaultConstructionProxyFactory$FastClassProxy.newInstance(DefaultConstructionProxyFactory.java:89)
at com.google.inject.internal.ConstructorInjector.provision(ConstructorInjector.java:114)
at com.google.inject.internal.ConstructorInjector.access$000(ConstructorInjector.java:32)
at com.google.inject.internal.ConstructorInjector$1.call(ConstructorInjector.java:98)
at com.google.inject.internal.ProvisionListenerStackCallback$Provision.provision(ProvisionListenerStackCallback.java:112)
at com.google.inject.internal.ProvisionListenerStackCallback$Provision.provision(ProvisionListenerStackCallback.java:120)
at com.google.inject.internal.ProvisionListenerStackCallback.provision(ProvisionListenerStackCallback.java:66)
at com.google.inject.internal.ConstructorInjector.construct(ConstructorInjector.java:93)
at com.google.inject.internal.ConstructorBindingImpl$Factory.get(ConstructorBindingImpl.java:306)
at com.google.inject.internal.InjectorImpl$1.get(InjectorImpl.java:1050)
... 1 more
UPDATE
I already found what the problem was. The constructor was not aware of the loadFromFXML method as in the constructor of the view the UI has not been loaded yet. What I did was simply put the loadFromFXML() inside the init() method of the Abstract Class and call it directly from the Concrete View Class. I found out this by calling the loadFromFXML from the initUI method, which is where the UI can be accessed.
AST transformations only apply if you're compiling Groovy code, which may not be what you're doing. The loadFromFXML() method expects a resource to be available on the classpath by matching the given argument using the following value transformation
arg.replaceAll('.', '/') + ".fxml"
This means your code resolves "com.softgan.viewPopUp" to "com/softgan/viewPopUp.fxml". Does that file exist in src/main/resources or griffon-app/resources?

javafx exception : Controller value already specified

I'm calling a method to load a window by passing some parameters to a specific internal method of this window, but I've got this exception:
GRAVE: null
javafx.fxml.LoadException: Controller value already specified.
unknown path:12
here is my method
public void openDef(String sys, String comp, String code) throws Exception {
Stage defStage = new Stage();
FXMLLoader loader = new FXMLLoader();
DefTableController cont = new DefTableController();//calling class controller
loader.setController(cont);
Parent frame = loader.load(getClass().getResource("defTable.fxml").openStream());
cont.getSysChoice(sys, comp, code);//call the method by passing parameters
Scene sceneDef = new Scene(frame);
defStage.setTitle("Défaillance du " + comp);
defStage.setScene(sceneDef);
defStage.show();
}
I don't understand why it consider that the controller is already set? and how to fix that ?
thank you
Remove the fx:controller attribute from the FXML file. That attribute is an instruction to the FXMLLoader to create a new controller: since you have already set one by calling setController it is contradictory.
JavaFX Error: Controller value already specified
This guy answered it ^ Props to him!

JavaFX NullPointerException Button in other class

I've to create an application for a school project. After you login, you should go to the dashboard. That works, but, it throws a NullPointerException when I try to set the button do disabled.
In this file, the stage is changing (after logging in):
public class ScreenController {
public void setScene(Stage stage, Parent root,Button button, String file){
if(file == "dashboard"){
stage = (Stage) button.getScene().getWindow();
try {
root = FXMLLoader.load(getClass().getResource("Dashboard.fxml"));
} catch (IOException ex) {
System.out.println("IOException: "+ex);
}
Scene scene = new Scene(root);
stage.setTitle("Corendon - Dashboard");
stage.setScene(scene);
stage.show();
Dashboard dashboard = new Dashboard();
}
}
}
And on the last line, the button should be set to disabled...
Dashboard dashboard = new Dashboard();
... by this class:
public class Dashboard extends ScreenController {
#FXML public Button buttonDashboard1;
public Dashboard(){
buttonDashboard1.setDisable(true);
}
}
But that is not working it throws the NullPointerException:
Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1774)
at
...
Caused by: java.lang.NullPointerException
at fastenyourseatbelts.Dashboard.<init>(Dashboard.java:11)
at fastenyourseatbelts.ScreenController.setScene(ScreenController.java:33)
at fastenyourseatbelts.LoginController.buttonLogin(LoginController.java:74)
... 59 more
I'm trying for hours now, but I don't get the solution... Does anyone know what is going wrong and why?
Move the code from the constructor of your controller to the initialize. This method is invoked by the FXMLLoader, after injection of all fields is completed and therefore should have access to the buttonDashboard1 instance:
public class Dashboard extends ScreenController {
#FXML public Button buttonDashboard1;
#FXML
private void initialize() {
buttonDashboard1.setDisable(true);
}
}
You also have to make sure the controller is either specified in the root element of the fxml file e.g. (replace packagename with the package of the Dashboard class)
<VBox xmlns:fx="http://javafx.com/fxml/1" fx:controller="packagename.Dashboard">
<children>
<Button text="click me" fx:id="buttonDashboard1"/>
</children>
</VBox>
or set as controller for the fxml before loading it:
FXMLLoader loader = new FXMLLoader(getClass().getResource("Dashboard.fxml"));
Dashboard dashboard = new Dashboard();
loader.setController(dashboard);
root = loader.load();
(don't use a fx:controller attribute in this case)

what must be specified in the fx:controller=""

I used some commands for getting the controller of a fxml file. at first I used an address like this:
fx:controller="PersonOverviewController"
and the code in main class was like this
FXMLLoader loader = new FXMLLoader();
loader.setLocation(MainApp.class.getResource("view/PersonOverview.fxml"));
AnchorPane ap = loader.load();
PersonOverviewController pc = loader.getController();
pc.setTableContent(this);
but it doesn't work.
in another attempt, I changed the
fx:controller="address.view.PersonOverviewController"
and this time it worked.
Why this is the case?
FXMLLoader needs the binary name of the controller class to be specified. If the package of your PersonOverviewController is address.view you therefore have to include it in the attribute value.
FXMLLoader basically creates the controller instance like this, if no controller factory is set:
String fxController = ...
Class controllerClass = getClassLoader().loadClass(fxController);
Object controller = controllerClass.newInstance();

FXML Doccument refusing to import other fxml files

I have a main FXML document for my program which contains a TabPane. For each tab I want it to have its own controller and fxml file. When I try to include the external fmxl files into the main fxml document, my program refuses to run. here is my main FXML document:
here is a copy of my java file
#Override
public void start(Stage stage) throws Exception {
FXMLLoader fxml = new FXMLLoader();
Parent root = fxml.load(getClass().getResource("FXMLDocument.fxml").openStream());
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
FXMLDocumentController fdc = fxml.getController();
}
Error:
Caused by: javafx.fxml.LoadException: Base location is undefined. unknown path:97
This error is caused because you have not set the location property of the FXMLLoader, and instead you are specifying an InputStream from which to load the FXML. I think the FXMLLoader must need to know the location of the original fxml file in order to resolve the location of the included file. You should really only use the load(InputStream) method in exceptional circumstances: when you are loading the fxml from a source other than a resource (i.e. file or resource in your application jar file).
Instead, use
FXMLLoader fxml = new FXMLLoader();
fxml.setLocation(getClass().getResource("FXMLDocument.fxml"));
Parent root = fxml.load();
or, equivalently,
FXMLLoader fxml = new FXMLLoader(getClass().getResource("FXMLDocument.fxml"));
Parent root = fxml.load();

Resources