I’m currently learning Java and I’m trying to deploy a simple project to WildFly 28.0.1.Final but I’m encountering a problem. The project contains only three classes (HelloServlet
, Sentence
, SentenceRepository
) and uses MySQL 8.0.33. I’m writing the code with Java 11.
In terms of what I’ve tried so far, I’ve attempted to adjust my project’s dependencies within the pom.xml
file, believing that perhaps there was an issue related to library compatibility or something missing. I’ve also tried adjusting the beans.xml
and persistence.xml
configurations, though I wasn’t entirely sure what I needed to change, but I wanted to see if modifications there would solve the problem.
As for my expectations, when I deploy my WAR file to WildFly, I was expecting the server to start without issue. I thought it would read the pom.xml
, beans.xml
, and persistence.xml
correctly, wire up all dependencies, initialize my HelloServlet, and be ready to handle incoming requests. I’m surprised by the NullPointerException
as I’ve checked my code and there’s no obvious place where I’m accessing something that should be null.
In essence, my goal is to understand how to set up a simple project with WildFly, MySQL, and Java, and to get this project running without errors. Any guidance towards identifying the root cause of this NullPointerException
and resolving it would be greatly appreciated.
My project structure:
├── pom.xml
├── README.md
└── src
└── main
├── java
│ └── com
│ └── penguin
│ ├── model
│ │ └── Sentence.java
│ ├── repository
│ │ └── SentenceRepository.java
│ └── servlet
│ └── HelloServlet.java
├── resources
│ └── META-INF
│ ├── beans.xml
│ └── persistence.xml
└── webapp
├── index.jsp
└── WEB-INF
└── web.xml
Whenever I try to deploy the WAR file, I get the following error:
22:26:49,900 ERROR [org.jboss.msc.service.fail] (ServerService Thread Pool -- 144) MSC000001: Failed to start service jboss.deployment.unit."language-study.war".undertow-deployment: org.jboss.msc.service.StartException in service jboss.deployment.unit."language-study.war".undertow-deployment: java.lang.RuntimeException: java.lang.NullPointerException
at [email protected]//org.wildfly.extension.undertow.deployment.UndertowDeploymentService$1.run(UndertowDeploymentService.java:90)
at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515)
at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
at [email protected]//org.jboss.threads.ContextClassLoaderSavingRunnable.run(ContextClassLoaderSavingRunnable.java:35)
at [email protected]//org.jboss.threads.EnhancedQueueExecutor.safeRun(EnhancedQueueExecutor.java:1990)
at [email protected]//org.jboss.threads.EnhancedQueueExecutor$ThreadBody.doRunTask(EnhancedQueueExecutor.java:1486)
at [email protected]//org.jboss.threads.EnhancedQueueExecutor$ThreadBody.run(EnhancedQueueExecutor.java:1377)
at java.base/java.lang.Thread.run(Thread.java:829)
at [email protected]//org.jboss.threads.JBossThread.run(JBossThread.java:513)
Caused by: java.lang.RuntimeException: java.lang.NullPointerException
at [email protected]//io.undertow.servlet.core.DeploymentManagerImpl.undeploy(DeploymentManagerImpl.java:686)
at [email protected]//org.wildfly.extension.undertow.deployment.UndertowDeploymentService.startContext(UndertowDeploymentService.java:110)
at [email protected]//org.wildfly.extension.undertow.deployment.UndertowDeploymentService$1.run(UndertowDeploymentService.java:87)
... 8 more
Caused by: java.lang.NullPointerException
at [email protected]//io.undertow.servlet.core.DeploymentImpl.createThreadSetupAction(DeploymentImpl.java:176)
at [email protected]//io.undertow.servlet.core.DeploymentManagerImpl.undeploy(DeploymentManagerImpl.java:669)
... 10 more
22:26:49,908 ERROR [org.jboss.as.controller.management-operation] (External Management Request Threads -- 1) WFLYCTL0013: Operation ("add") failed - address: ([("deployment" => "language-study.war")]) - failure description: {"WFLYCTL0080: Failed services" => {"jboss.deployment.unit."language-study.war".undertow-deployment" => "java.lang.RuntimeException: java.lang.NullPointerException
Caused by: java.lang.RuntimeException: java.lang.NullPointerException
Caused by: java.lang.NullPointerException"}}
22:26:49,909 ERROR [org.jboss.as.controller.management-operation] (External Management Request Threads -- 1) WFLYCTL0013: Operation ("add") failed - address: ([("deployment" => "language-study.war")]) - failure description: {"WFLYCTL0080: Failed services" => {"jboss.deployment.unit."language-study.war".undertow-deployment" => "java.lang.RuntimeException: java.lang.NullPointerException
Caused by: java.lang.RuntimeException: java.lang.NullPointerException
Caused by: java.lang.NullPointerException"}}
22:26:49,909 ERROR [org.jboss.as.server] (External Management Request Threads -- 1) WFLYSRV0021: Deploy of deployment "language-study.war" was rolled back with the following failure message:
{"WFLYCTL0080: Failed services" => {"jboss.deployment.unit."language-study.war".undertow-deployment" => "java.lang.RuntimeException: java.lang.NullPointerException
Caused by: java.lang.RuntimeException: java.lang.NullPointerException
Caused by: java.lang.NullPointerException"}}
I’m not sure where the NullPointerException
is coming from. I haven’t been able to find the source of the problem in my code or the server configuration
My persistence.xml
:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="LanguageStudyDS" transaction-type="JTA">
<jta-data-source>java:/LanguageStudyDS</jta-data-source>
<properties>
<property name="hibernate.archive.autodetection" value="class" />
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.format_sql" value="true" />
</properties>
</persistence-unit>
</persistence>
My beans.xml
:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
bean-discovery-mode="all">
</beans>
My web.xml
:
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>HelloServlet</servlet-name>
<servlet-class>com.penguin.servlet.HelloServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloServlet</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
</web-app>
My pom.xml
:
<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 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.penguin</groupId>
<artifactId>language-study</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>language-study Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<maven.compiler.release>11</maven.compiler.release>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>jakarta.platform</groupId>
<artifactId>jakarta.jakartaee-api</artifactId>
<version>10.0.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>jakarta.servlet</groupId>
<artifactId>jakarta.servlet-api</artifactId>
<version>6.0.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<version>8.0.33</version>
</dependency>
<dependency>
<groupId>jakarta.persistence</groupId>
<artifactId>jakarta.persistence-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.hibernate.orm</groupId>
<artifactId>hibernate-core</artifactId>
<version>6.2.6.Final</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.wildfly</groupId>
<artifactId>wildfly-jpa</artifactId>
<version>28.0.1.Final</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.faces</groupId>
<artifactId>javax.faces-api</artifactId>
<version>2.3</version>
</dependency>
<dependency>
<groupId>org.jboss.weld.servlet</groupId>
<artifactId>weld-servlet</artifactId>
<version>2.4.8.Final</version>
</dependency>
</dependencies>
<build>
<finalName>language-study</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<source>11</source>
<target>11</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
My HelloServlet.java
:
public class HelloServlet extends HttpServlet {
@EJB
private SentenceRepository repo;
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
if (repo == null) {
out.println("<h1>Error: repo object is null</h1>");
return;
}
Sentence sentence = null;
try {
// Inserting a sentence
sentence = new Sentence("你好,世界");
repo.save(sentence);
} catch (Exception ex) {
ex.printStackTrace();
out.println("<h1>Error: " + ex.getMessage() + "</h1>");
}
try {
// Fetching a sentence
Sentence fetchedSentence = repo.find(sentence.getId());
out.println("<h1>Id: " + fetchedSentence.getId() + ", Sentence: " + fetchedSentence.getMandarinSentence() + "</h1>");
} catch (Exception ex) {
ex.printStackTrace();
out.println("<h1>Error: " + ex.getMessage() + "</h1>");
}
}
}
My SentenceRepository.java
:
@Stateless
public class SentenceRepository {
@PersistenceContext(unitName = "LanguageStudyDS")
private EntityManager entityManager;
public void save(Sentence sentence) {
try {
entityManager.persist(sentence);
} catch (Exception e) {
System.out.println(e);
}
}
public Sentence find(Long id) {
try {
return entityManager.find(Sentence.class, id);
} catch (Exception e) {
System.out.println(e);
}
return null;
}
public void delete(Long id) {
try {
Sentence sentence = find(id);
if (sentence != null && entityManager != null) {
entityManager.remove(sentence);
}
} catch (Exception e) {
System.out.println(e);
}
}
public void update(Long id, String newSentence) {
try {
Sentence sentence = find(id);
if (sentence != null) {
sentence.setMandarinSentence(newSentence);
}
} catch (Exception e) {
System.out.println(e);
}
}
}
My Sentence.java
:
@Entity
@Table(name = "sentences")
public class Sentence {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "mandarin_sentence")
private String mandarinSentence;
public Sentence() {
}
public Sentence(String mandarinSentence) {
this.mandarinSentence = mandarinSentence;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getMandarinSentence() {
return mandarinSentence;
}
public void setMandarinSentence(String mandarinSentence) {
this.mandarinSentence = mandarinSentence;
}
}
My module.xml
(inside the WildFly):
<?xml version="1.0" encoding="UTF-8"?>
<module xmlns="urn:jboss:module:1.1" name="com.mysql">
<resources>
<resource-root path="mysql-connector-j-8.0.33.jar"/>
</resources>
<dependencies>
<module name="javax.api"/>
<module name="javax.transaction.api"/>
</dependencies>
</module>
The modifications I made to the standalone.xml
(also inside the WildFly):
<datasource jndi-name="java:/LanguageStudyDS" pool-name="LanguageStudyDS" enabled="true" use-java-context="true">
<connection-url>jdbc:mysql://localhost:3306/language_study?useSSL=false</connection-url>
<driver>mysql</driver>
<security>
<user-name>username</user-name>
<password>password</password>
</security>
</datasource>
<driver name="mysql" module="com.mysql">
<driver-class>com.mysql.cj.jdbc.Driver</driver-class>
</driver>
2
Answers
After struggling with this problem for a while, I was finally able to find a solution. Here's how I resolved it:
I had to adjust my
web.xml
file to be compatible with Jakarta EE 10:Then I had to move the
beans.xml
fromsrc/main/resources/META-INF
tosrc/main/webapp/WEB-INF
.The last part of my journey was working with my project's dependencies. I had to make sure they were going to work with Jakarta EE 10 and WildFly 28.0.1.Final. I took a close look, made necessary adjustments, and set as many dependencies as I could to provided. Here's how my final dependencies looked:
I'd like to thank everyone who offered their help during this troubleshooting process. I can now deploy and run my code without any issues. I hope this solution is helpful to anyone else who encounters a similar problem.
1- You provide a WAR with your dependencies, WildFly will provide JakartEE 10 API implementations.
2- Your driver should be provided since you are creating a module to provide it.
3- Your XML descriptors are not JakartaEE 10 but Java EE
4- The error occurs during the undeploy not during the deployment itself. Maybe there is an error there before
5- You should take a look at WildFly Quickstarts https://github.com/wildfly/quickstart/tree/28.0.1.Final (todo-backend can show what your are trying to achieve here)