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]
LVL 9
DrWarezzAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
petmagdyCommented:
in ur html form make sure that method = "get"
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JSP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.