Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

VERY Basic Forum

Posted on 2004-10-29
6
Medium Priority
?
248 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
Comment
Question by:DrWarezz
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 9

Author Comment

by:DrWarezz
ID: 12442676
>"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:
TimYates earned 2000 total points
ID: 12442712
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
ID: 12442722
in ur html form make sure that method = "get"
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 9

Author Comment

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

Thanks!
[r.D]
0
 
LVL 35

Expert Comment

by:TimYates
ID: 12442837
:-)  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
ID: 12442846
:-) Yeah, I move onto servlets, and forget the basics of Java -- lol :o\

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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An overview of cyber security, cyber crime, and personal protection against hackers. Includes a brief summary of the Equifax breach and why everyone should be aware of it. Other subjects include: how cyber security has failed to advance with technol…
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

609 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