我正在尝试学习 Java 和 Jsf,并且一直在从事聊天功能项目。在点击发送按钮后,我一直在尝试显示消息。Eclipse 中出现的错误是我的操作控制器 addMessage 方法从发送按钮出现的空指针异常。你能告诉我我的代码有什么问题吗?我已经为此工作了 3 天,并尝试从网上应用不同的解决方案,但似乎无法修复它。我非常需要这个尽快,所以我在这里发布这个寻求帮助。这是我的代码:
XHTML 页面:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets">
<h:head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Chat Room</title>
</h:head>
<h:body>
<!-- Pao edited -->
<div class="title" align="center">Welcome to ACT Chat Room</div>
<!-- collective chat messages -->
<div id="chatMessageList">
<table align="center">
<tr>
<td valign="top">
<h:outputText value="#{chatPage.chatMessageList}" />
</td>
</tr>
</table>
</div>
<!-- chat message -->
<div id="chatMessage">
<h:form>
<table align="center">
<tr>
<td>Enter Message:</td>
<td><h:inputTextarea value="#{chatPage.message}" rows="3" cols="75"/></td>
<td><h:commandButton value="Send" action="#{chatPage.addMessage()}" /></td>
</tr>
</table>
</h:form>
</div>
<!-- End editing here -->
</h:body>
</html>
聊天页面类:
@ManagedBean
@SessionScoped
public class ChatPage {
private String username;
private String message;
private String creationTime;
private String id;
private List<ChatMessage> chatMessageList;
private ChatMessage chatMessage;
public ChatPage(){
}
(getters and setters)
/将chatPage.message 放入chatMessage.message 并添加到chatMessageList 的方法/
public void addMessage(){
ChatMessage chatMessage = new ChatMessage();
chatMessage.setMessage(message);
this.chatMessageList.add(chatMessage);
}
}
聊天消息类:
public class ChatMessage {
private int id;
private String message;
private String creationTime;
(getters and setters)
}
这是我得到的错误:
java.lang.NullPointerException
actportal.view.chat.ChatPage.addMessage(ChatPage.java:65)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
java.lang.reflect.Method.invoke(Unknown Source)
org.apache.el.parser.AstValue.invoke(AstValue.java:278)
org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:274)
将不胜感激任何帮助。提前致谢!