Home > Software engineering >  ClassCastException cannot be cast to class X and Y are in unnamed module of loader 'app'
ClassCastException cannot be cast to class X and Y are in unnamed module of loader 'app'

Time:10-27

I have a class called ControllerConfiguration where I add these two beans to the ApplicationContext:

    @Bean
    @Lazy
    @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
    public ConnectorFittingController connectorFittingController() throws IOException {
        return loadController(ConnectorFittingController.class);
    }

    @Bean
    @Lazy
    @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
    public ConnectorFittingResultController connectorFittingResultController() throws IOException {
        return loadController(ConnectorFittingResultController.class);
    }

Now the following exception gets thrown:

Caused by: java.lang.ClassCastException: class de.some.project.controller.connectorfitting.ConnectorFittingResultController cannot be cast to class de.some.project.controller.connectedconnectors.ConnectedConnectorsController (de.some.project.controller.connectorfitting.ConnectorFittingResultController and de.some.project.controller.connectedconnectors.ConnectedConnectorsController are in unnamed module of loader 'app')
    at de.some.project.controller.ControllerConfiguration.connectedConnectorsController(ControllerConfiguration.java:156)
    at de.some.project.controller.ControllerConfiguration$$EnhancerBySpringCGLIB$$6a944412.CGLIB$connectedConnectorsController$3(<generated>)
    at de.some.project.controller.ControllerConfiguration$$EnhancerBySpringCGLIB$$6a944412$$FastClassBySpringCGLIB$$18aa65ce.invoke(<generated>)
    at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:244)
    at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:331)
    at de.some.project.controller.ControllerConfiguration$$EnhancerBySpringCGLIB$$6a944412.connectedConnectorsController(<generated>)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154)
    ... 62 more

This makes no sense for because I never try anywhere to cast to one of these classes. I read in other questions that this has something to do with the spring developer tools. But I don't use them in my project.

Here is my pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.6</version>
    </parent>
    <groupId>private.company</groupId>
    <artifactId>projectname</artifactId>
    <version>0.0.32</version>
    <name>project</name>
    <description></description>
    <properties>
        <java.version>11</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>net.harawata</groupId>
            <artifactId>appdirs</artifactId>
            <version>1.2.1</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.jfoenix</groupId>
            <artifactId>jfoenix</artifactId>
            <version>8.0.10</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.9</version>
        </dependency>
        <!-- Logging utilities -->
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-slf4j-impl</artifactId>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>log4j-over-slf4j</artifactId>
        </dependency>
        <dependency>
            <groupId>org.controlsfx</groupId>
            <artifactId>controlsfx</artifactId>
            <version>11.1.0</version>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-params</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.testfx</groupId>
            <artifactId>testfx-junit5</artifactId>
            <version>4.0.16-alpha</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.19.1</version>
                <dependencies>
                    <dependency>
                        <groupId>org.junit.platform</groupId>
                        <artifactId>junit-platform-surefire-provider</artifactId>
                        <version>1.1.0</version>
                    </dependency>
                    <dependency>
                        <groupId>org.junit.jupiter</groupId>
                        <artifactId>junit-jupiter-engine</artifactId>
                        <version>5.1.0</version>
                    </dependency>
                </dependencies>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

Edit: This is the loadController method.

    /**
     * Uses {@link FXMLLoader} to load the view for a presenter. The FXML file name
     * is derived from the class name. The presenters root is set to the loader's
     * root. If a similarly named CSS file exists, it is added to the presenters
     * style sheets. The resource bundle is set using the same derived name.
     * 
     * @param clazz The Presenter class to load the FXML for.
     * @return The Presenter, i.e. the views controller.
     *
     * @throws IOException If the XML file can't be loaded.
     */
    protected <C extends AbstractHarconController<T>, T extends Parent> C loadController(Class<C> clazz)
            throws IOException {

        String derivedXmlName = deriveXMLName(clazz);

        try (InputStream fxmlStream = Thread.currentThread().getContextClassLoader()
                .getResourceAsStream(derivedXmlName)) {
            LOGGER.info("DerivedXMLName: {}", derivedXmlName);

            FXMLLoader loader = new FXMLLoader();

            try {
                loader.setResources(ResourceBundle.getBundle(deriveBundleName(clazz)));
            } catch (MissingResourceException mre) {
                // Missing resource is ok, only log it for tracability, continue as normal
                LOGGER.trace("Unable to load resource bundle for {}: {}", deriveBundleName(clazz), mre.getMessage());
                LOGGER.trace("", mre);
            } // try

            loader.load(fxmlStream);
            addCSSIfAvailable(loader.getRoot(), clazz);

            return loader.getController();
        } // try
    } 

CodePudding user response:

As you can see in the Stack Trace, Error is coming from the place de.some.project.controller.ControllerConfiguration.connectedConnectorsController(ControllerConfiguration.java:156)

You are referencing a wrong place I think. Actually Error say you try to convert ConnectorFittingResultController class to ConnectedConnectorsController class at ControllerConfiguration.java:156 Please check that point.

CodePudding user response:

The problem was that I defined the wrong controller in the corresponding FXML-file...

Thanks to you all!

  • Related