0

I am working on a school project and have no idea where I am going wrong. Any help is appreciated. The project is a simple chat servlet which we have to be able to run on our school's Tomcat server.Right now I have a Servlet and a JSP.

public class ChatServ extends HttpServlet {
    public List chatList;
    public boolean stillAlive;

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        stillAlive = true;
        chatList = new List();
        chatList.add("Hello World!");
        request.setAttribute("chatList", chatList);
        while(stillAlive){
            request.setAttribute("chatList", chatList);

            String message = request.getParameter("message");
            if (message != null) {
                message = message.trim();
                if (message.length() != 0) {
                    chatList.add(message);
                }
            }
        }
    }
}

And the JSP I have written is:

<%@ page import="finalProject.*,java.util.List" language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>My Chat Project</title>
</head>
<body>
<%= request.getParameter("username")%>

<p>

<textarea name="chat" rows="13" cols="60">
<%
try{
List chat = (List) request.getAttribute("chatList");

    int start = chat.size()-10;
    for (int i=start; i<chat.size(); i++) {
    out.println(i + ": " + chat.get(i));
    }
}catch(NullPointerException e){
    out.println("Try again next time!");
}
 %>
</textarea>
</br>
<form action="ChatServ">
Say: <input type="text" name="message" size="60"/>
<br/>
<input type="submit" name="submit" value="Talk/Refresh"/>
</form>
</p>
</body>
</html>

Any assistance would be greatly appreciated. Right now I am getting a null pointer from the JSP trying to get the List and I can't figure out why. I am still new to the web side of this. Thanks!

SEVERE: Servlet.service() for servlet [jsp] in context with path [/FinalProject] threw exception [An exception occurred processing JSP page /chat.jsp at line 19

16: //try{
17:     List chat = (List) request.getAttribute("chatList");
18: 
19:     int start = chat.size()-10;
20:     for (int i=start; i<chat.size(); i++) {
21:         out.println(i + ": " + chat.get(i));
22:     }


Stacktrace:] with root cause
java.lang.NullPointerException
at org.apache.jsp.chat_jsp._jspService(chat_jsp.java:131)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at     org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:438)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at     org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
at     org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at     org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at     org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212)
at     org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
at     org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1096)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:674)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1500)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1456)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Unknown Source)
Ryan Palen
  • 11
  • 1
  • What is the problem? you just put your code but you didn't say anything about your problem – Dazak Dec 07 '15 at 01:14
  • At the very end I did. I apologize for making it an after thought. I am getting a null pointer error from the section in the JSP I have in the try/catch block. All I am wanting it to do is send the line to the servlet so the servlet can add the text to the List. Then the page should refresh, loading in the List and dropping the last 10 lines in the chat box. – Ryan Palen Dec 07 '15 at 01:21
  • Can you post a stack trace of the null pointer error so that we can see at what line it's happening? – dursk Dec 07 '15 at 01:26
  • Possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – Teepeemm Dec 07 '15 at 01:58
  • I figured out why I was getting the Null Pointer Exception. My math in the for loop in the JSP didn't account for negatives. The code was trying to use the List[-9] entry, which doesn't exist. – Ryan Palen Dec 07 '15 at 02:08

0 Answers0