Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Java and JDK > Servlets
| Search | Today's Posts | Mark Forums Read
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Servlets section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 30th, 2006, 04:34 PM
Registered User
 
Join Date: Dec 2006
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default help with nullPointerException tomcat

I have made a catalog using a servlet but when i try to run it i just get this in the browser:

java.lang.NullPointerException
    at coreservlets.Catalog.getItem(Catalog.java:18)
    at coreservlets.CatalogPage.setItems(CatalogPage.java :40)
    at coreservlets.KidsBooksPage.init(KidsBooksPage.java :16)
    at javax.servlet.GenericServlet.init(GenericServlet.j ava:258)
    at org.apache.catalina.core.StandardWrapper.loadServl et(StandardWrapper.java:918)
    at org.apache.catalina.core.StandardWrapper.allocate( StandardWrapper.java:655)
    at org.apache.catalina.servlets.InvokerServlet.serveR equest(InvokerServlet.java:400)
    at org.apache.catalina.servlets.InvokerServlet.doGet( InvokerServlet.java:180)
    at javax.servlet.http.HttpServlet.service(HttpServlet .java:740)
    at javax.servlet.http.HttpServlet.service(HttpServlet .java:853)
    at org.apache.catalina.core.ApplicationFilterChain.in ternalDoFilter(ApplicationFilterChain.java:247)
    at org.apache.catalina.core.ApplicationFilterChain.do Filter(ApplicationFilterChain.java:193)
    at filters.ExampleFilter.doFilter(ExampleFilter.java: 149)
    at org.apache.catalina.core.ApplicationFilterChain.in ternalDoFilter(ApplicationFilterChain.java:213)
    at org.apache.catalina.core.ApplicationFilterChain.do Filter(ApplicationFilterChain.java:193)
    at filters.ExampleFilter.doFilter(ExampleFilter.java: 149)
    at org.apache.catalina.core.ApplicationFilterChain.in ternalDoFilter(ApplicationFilterChain.java:213)
    at org.apache.catalina.core.ApplicationFilterChain.do Filter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.StandardWrapperValve.invo ke(StandardWrapperValve.java:243)
    at org.apache.catalina.core.StandardPipeline.invokeNe xt(StandardPipeline.java:566)
    at org.apache.catalina.core.StandardPipeline.invoke(S tandardPipeline.java:472)
    at org.apache.catalina.core.ContainerBase.invoke(Cont ainerBase.java:943)
    at org.apache.catalina.core.StandardContextValve.invo ke(StandardContextValve.java:190)
    at org.apache.catalina.core.StandardPipeline.invokeNe xt(StandardPipeline.java:566)
    at org.apache.catalina.authenticator.AuthenticatorBas e.invoke(AuthenticatorBase.java:475)
    at org.apache.catalina.core.StandardPipeline.invokeNe xt(StandardPipeline.java:564)
    at org.apache.catalina.valves.CertificatesValve.invok e(CertificatesValve.java:246)
    at org.apache.catalina.core.StandardPipeline.invokeNe xt(StandardPipeline.java:564)
    at org.apache.catalina.core.StandardPipeline.invoke(S tandardPipeline.java:472)
    at org.apache.catalina.core.ContainerBase.invoke(Cont ainerBase.java:943)
    at org.apache.catalina.core.StandardContext.invoke(St andardContext.java:2347)
    at org.apache.catalina.core.StandardHostValve.invoke( StandardHostValve.java:180)
    at org.apache.catalina.core.StandardPipeline.invokeNe xt(StandardPipeline.java:566)
    at org.apache.catalina.valves.ErrorDispatcherValve.in voke(ErrorDispatcherValve.java:170)
    at org.apache.catalina.core.StandardPipeline.invokeNe xt(StandardPipeline.java:564)
    at org.apache.catalina.valves.ErrorReportValve.invoke (ErrorReportValve.java:170)
    at org.apache.catalina.core.StandardPipeline.invokeNe xt(StandardPipeline.java:564)
    at org.apache.catalina.valves.AccessLogValve.invoke(A ccessLogValve.java:468)
    at org.apache.catalina.core.StandardPipeline.invokeNe xt(StandardPipeline.java:564)
    at org.apache.catalina.core.StandardPipeline.invoke(S tandardPipeline.java:472)
    at org.apache.catalina.core.ContainerBase.invoke(Cont ainerBase.java:943)
    at org.apache.catalina.core.StandardEngineValve.invok e(StandardEngineValve.java:174)
    at org.apache.catalina.core.StandardPipeline.invokeNe xt(StandardPipeline.java:566)
    at org.apache.catalina.core.StandardPipeline.invoke(S tandardPipeline.java:472)
    at org.apache.catalina.core.ContainerBase.invoke(Cont ainerBase.java:943)
    at org.apache.catalina.connector.http.HttpProcessor.p rocess(HttpProcessor.java:1027)
    at org.apache.catalina.connector.http.HttpProcessor.r un(HttpProcessor.java:1125)
    at java.lang.Thread.run(Thread.java:595)
can anyone explain to me what would cause this i think it has something to do with this peice of code:

     for(int i=0; i<items.length; i++) {
          out.println("");
          printitem = items[i];
    if (printitem == null) {
        out.println("" +
                    "Unknown recording ID " +
                    "");
      } else {
           added[i] = items[i];
    out.println( added[i] + "\n" +
       item.gettitle() + "\n" +
           item.getprice() + "\n" +
       item.getdirector() + "\n" +
       item.getcategory() + "\n" +
       item.getduration() + "\n" +
       item.getrating() + "\n" +
       item.getyear() + "\n");

    out.println("\n</BODY></HTML>");
      }
    }

please any help would be excellent i need to get this working soon lol its driving me mad!!!!!! Below is my servlet file

package coreservlets;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;

public class Catalog extends HttpServlet {
private static CatalogItem added[];
private static CatalogItem[] items;
CatalogItem item;
CatalogItem details;

public static CatalogItem getItem(int recordingid) {
          CatalogItem item;
          for(int i=0; i<items.length; i++) {
            item = items[i];
            if (recordingid==item.getrecordingid()) {
              return(item);
          }
        }
        return(null);
      }

  public void doGet(HttpServletRequest request,
                    HttpServletResponse response)
      throws ServletException, IOException {
    response.setContentType("text/html");
    PrintWriter out = response.getWriter();



    Connection conn = null;
    String imageDB;
    String titleDB;
    String directorDB;
    String categoryDB;
    int durationDB;
    String ratingDB;
    String yearDB;
    float priceDB;
    int recordingidDB;




    try{
        Class.forName("com.mysql.jdbc.Driver").newInstance ();
    } catch(Exception e) {
        System.out.println(e);
    }


    try{

        conn = DriverManager.getConnection
        ("jdbc:my sql url goes here");

    }
       catch(SQLException se) {
        System.out.println(se);
    }


    try{

        String selectSQL = "select image_name, title, director, category, duration, rating, year_released, price, recording_id "+
                                       "from video_recordings ";


        Statement stmt = conn.createStatement();
        ResultSet rs1 = stmt.executeQuery(selectSQL);


// Retrieve the results
        while(rs1.next()){

        recordingidDB = rs1.getInt("recording_id");
        imageDB = rs1.getString("image_name");
        titleDB = rs1.getString("title");
        directorDB = rs1.getString("director");
        categoryDB = rs1.getString("category");
        durationDB = rs1.getInt("duration");
        ratingDB = rs1.getString("rating");
        yearDB = rs1.getString("year_released");
        priceDB = rs1.getFloat("price");
        double priceD = priceDB;

          item =
         new CatalogItem
            (recordingidDB,
              titleDB,
              directorDB,
              categoryDB,
               durationDB,
              ratingDB,
                yearDB,
              priceD);


    }

    String title = "Catalog";

    out.println( "<HTML>\n" +
                "<HEAD><TITLE>" + title + "</TITLE></HEAD>\n" +
                "<H1 ALIGN=\"CENTER\">" + title + "</H1>");


        for(int i=0; i<items.length; i++) {
          out.println("");
          details = items[i];
    if (details == null) {
        out.println("
                    sorry an error "
                    );
      } else {
           added[i] = items[i];
    out.println( added[i] + "\n" +
       item.gettitle() + "\n" +
           item.getprice() + "\n" +
       item.getdirector() + "\n" +
       item.getcategory() + "\n" +
       item.getduration() + "\n" +
       item.getrating() + "\n" +
       item.getyear() + "\n");

    out.println("\n</BODY></HTML>");
      }
    }


// Close the stament and database connection
// (must remember to always do this)
        stmt.close();
        conn.close();
    } catch(SQLException se) {
        System.out.println(se);
     }
  }
}

 
Old March 26th, 2007, 01:52 AM
Friend of Wrox
 
Join Date: Mar 2007
Location: Hyderabad, A.P., India.
Posts: 373
Thanks: 0
Thanked 1 Time in 1 Post
Default

Hi,

If you look at the condition part of the for loop in getItem() method, you are calling length on items variable, items might me null check for null value for items variable, i thing thats causing NullPointerException.

for(int i=0; i<items.length; i++) {

Regards,
Rakesh




Similar Threads
Thread Thread Starter Forum Replies Last Post
new to tomcat mgarito BOOK: Beginning JavaServer Pages 2 February 21st, 2006 11:51 PM
JWS Axis Tomcat (posted to Apache Tomcat too) rushman Servlets 0 April 15th, 2005 09:32 AM
Tomcat and IE Robert Menteer Apache Tomcat 0 December 10th, 2004 03:34 PM
Tomcat 3.3.2 to Tomcat 5.0.24 amith_pj Apache Tomcat 1 July 4th, 2004 10:52 PM
Has any tried out the tomcat 5.0? Qing JSP Basics 8 August 8th, 2003 04:44 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.