Solved

VERY Basic Forum

Posted on 2004-10-29
238 Views
Last Modified: 2010-04-01
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
Question by:DrWarezz
    6 Comments
     
    LVL 9

    Author Comment

    by:DrWarezz
    >"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
     
    LVL 35

    Accepted Solution

    by:
    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
     
    LVL 13

    Expert Comment

    by:petmagdy
    in ur html form make sure that method = "get"
    0
     
    LVL 9

    Author Comment

    by:DrWarezz
    Hey Tim :-)
    That done the job!! :D

    Thanks!
    [r.D]
    0
     
    LVL 35

    Expert Comment

    by:TimYates
    :-)  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
     
    LVL 9

    Author Comment

    by:DrWarezz
    :-) Yeah, I move onto servlets, and forget the basics of Java -- lol :o\

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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone. Privacy Policy Terms of Use

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Suggested Solutions

    Phishing is at the top of most security top 10 efforts you should be pursuing in 2016 and beyond. If you don't have phishing incorporated into your Security Awareness Program yet, now is the time. Phishers, and the scams they use, are only going to …
    Read about why website design really matters in today's demanding market.
    With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…
    how to add IIS SMTP to handle application/Scanner relays into office 365.

    877 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now