java.lang.NullPointerException:: Location is required solution Error Caused by appearance

java.lang.NullPointerException:: Location is required solution Error Caused by appearance

problem causes

Cause of the problem is due to a null pointer, which is the result getClass (). GetResource ( "sample.fxml ") This method is empty
we can test the code slightly divided into two sections
Here Insert Picture Description
as shown, if it is empty, then the output hello, the program stops
the test results as shown below
Here Insert Picture Description

analysis

Above we know, we did not find the file, then we put that fxml layout in the directory can solve the problem of the null pointer.
Let's take a look at the path to change is where (using getResource ( ""))
Here Insert Picture Description
Then I check the Maven, because we use Maven, it will java files compiled into class files in a default folder called target in. other files will be ignored and not placed in the target folder

Solution

The solution is simple, we only need to set the pom.xml, the layout fxml file in the target file to a folder

 <build>
    <resources>
        
        <resource>
            <!-- 这里是放在 src/main/java-->
            <directory>src/main/java</directory>
            <includes>
                <include>**/*.properties</include>
                <include>**/*.fxml</include>
                <include>**/fxml/*.fxml</include>
                <!-- 如果想要弄个包名专门放fxml文件,像上一行这样添加设置 -->
                <!-- 之后,使用getResource("fxml/xx.fxml")这样子 -->
            </includes>
            <filtering>false</filtering>
        </resource>
    </resources>
</build>

Transfer @ stars-one original source

Published 68 original articles · won praise 7 · views 2516

Guess you like

Origin blog.csdn.net/Cui6023056/article/details/104838817