• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 251
  • Last Modified:

VERY Basic Forum

Hello again :-)

[Might I point out; I know there's ALOT of stuff below, but the question is pretty basic, so please don't be put off ;-)]

I'm basically trying to create a VERY basic forum here. Well, it's actually more of a guest book. Which holds up to 20 posts, and then starts to overwrite from the beginning...

Here's the code:

//----8<---------------------------------- PostText.java ----------------------------------------------\\
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class PostText extends HttpServlet {
   
    int count=0;
    GuestEntry [] entry = new GuestEntry [20];
   
    public void doGet ( HttpServletRequest req, HttpServletResponse res ) throws ServletException, IOException {
       
        res.setContentType("text/html");
        PrintWriter out = res.getWriter();
       
        if ( count >= 20 ) count=0;
       
        out.println("<html><head><title>Forum | Posts</title></head>");
        out.println("<body bgcolor=\" #F0F0F0 \"");
       
        entry[count].email = req.getParameter("email");
        entry[count].name = req.getParameter("name");
        entry[count].comment = req.getParameter("comment");
       
        synchronized(this) {
            count++;
        }
       
        for ( int i=0; i<entry.length; i++ ) {
           
            if ( entry[i] != null ) {
                out.println("<br>-----------------------------------------------------------------<br>");
                out.println("<b>Name:</b> " + entry[i].name + "<br>");
                out.println("<b>Email:</b> " + entry[i].email + "<br>");
                out.println("<b>Comment:</b> " + entry[i].comment + "<br>");
                out.println("<p>");
            }
           
        }
       
        out.println("</body>");
        out.println("</html>");
       
    }
   
    class GuestEntry {
        public String name;
        public String email;
        public String comment;
    }
   
}
//----8<---------------------------------- PostText.java ----------------------------------------------\\

Okay, it compiles fine, and I've put all it in the write DIR, and have compiled the class file to the write DIR as well.

However, when I run it, (I've created an HTML doc which calls it with arguments) like so:

http://localhost/PostText?name=Robert&email=MyAddy&comment=aMessage

And I've added the following code to my web.xml file:

    <servlet>
        <servlet-name>PostText</servlet-name>
        <servlet-class>PostText</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>PostText</servlet-name>
        <url-pattern>/PostText</url-pattern>
    </servlet-mapping>

However, when I START Tomcat 4.1.31, an exception occurs, but it continues, and creates the server socket, and I can use the server.
This is probably related to my problem:

PROBLEM
-----------
When I load:
http://localhost/PostText?name=Robert&email=MyAddy&comment=aMessage

I get this error:

--------------------------------------------------------------------------------------------------------------------------------------
HTTP Status 500 -

--------------------------------------------------------------------------------

type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

java.lang.NullPointerException
      at PostText.doGet(PostText.java:21)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:696)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:809)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:200)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:146)
      at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:209)
      at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:596)
      at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:433)
      at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:948)
      at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:144)
      at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:596)
      at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:433)
      at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:948)
      at org.apache.catalina.core.StandardContext.invoke(StandardContext.java:2358)
      at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:133)
      at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:596)
      at org.apache.catalina.valves.ErrorDispatcherValve.invoke(ErrorDispatcherValve.java:118)
      at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:594)
      at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:116)
      at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:594)
      at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:433)
      at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:948)
      at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:127)
      at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:596)
      at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:433)
      at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:948)
      at org.apache.coyote.tomcat4.CoyoteAdapter.service(CoyoteAdapter.java:152)
      at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:799)
      at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:705)
      at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:577)
      at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:683)
      at java.lang.Thread.run(Thread.java:536)



--------------------------------------------------------------------------------

Apache Tomcat/4.1.31
--------------------------------------------------------------------------------------------------------------------------------------


Any ideas? :o\

Thanks in advance! :D
[r.D]
0
DrWarezz
Asked:
DrWarezz
  • 3
  • 2
1 Solution
 
DrWarezzAuthor Commented:
>"Okay, it compiles fine, and I've put all it in the write DIR, and have compiled the class file to the write DIR as well."
lol - Sorry (late night), by "write", of course, I mean "right"  :-)
0
 
TimYatesCommented:
You have to initialise the array...

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class PostText extends HttpServlet {
   
    int count=0;
    GuestEntry [] entry = new GuestEntry [20];
   
    public void init(ServletConfig config) throws ServletException
    {
        // Initialise the array (maybe load it from a file or something instead of this)
        for( int i = 0 ; i < entry.length ; i++ )
            entry[ i ] = new GuestEntry() ;
    }

    public void doGet ( HttpServletRequest req, HttpServletResponse res ) throws ServletException, IOException {
       
        res.setContentType("text/html");
        PrintWriter out = res.getWriter();
       
        if ( count >= 20 ) count=0;
       
        out.println("<html><head><title>Forum | Posts</title></head>");
        out.println("<body bgcolor=\" #F0F0F0 \"");
       
        entry[count].email = req.getParameter("email");
        entry[count].name = req.getParameter("name");
        entry[count].comment = req.getParameter("comment");
       
        synchronized(this) {
            count++;
        }
       
        for ( int i=0; i<entry.length; i++ ) {
           
            if ( entry[i] != null ) {
                out.println("<br>-----------------------------------------------------------------<br>");
                out.println("<b>Name:</b> " + entry[i].name + "<br>");
                out.println("<b>Email:</b> " + entry[i].email + "<br>");
                out.println("<b>Comment:</b> " + entry[i].comment + "<br>");
                out.println("<p>");
            }
           
        }
       
        out.println("</body>");
        out.println("</html>");
       
    }
   
    class GuestEntry {
        public String name;
        public String email;
        public String comment;
    }
   
}
0
 
petmagdyCommented:
in ur html form make sure that method = "get"
0
Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

 
DrWarezzAuthor Commented:
Hey Tim :-)
That done the job!! :D

Thanks!
[r.D]
0
 
TimYatesCommented:
:-)  No worries :-)

It's always something to remember with arrays in java...  Arrays of primitives are fine;

  int[] arr = new int[ 10 ] ;
  arr[ 2 ] = 4 ;

will work, but with arrays of objects, you need to create an object for each element before you can access them:

  Integer[] arr = new Integer[ 4 ] ;

all elements are null, so you need to create an instance of each element :-)

Good luck!

Tim
0
 
DrWarezzAuthor Commented:
:-) Yeah, I move onto servlets, and forget the basics of Java -- lol :o\

Thanks alot Tim :-)
[r.D]
0

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now