On Tue, 13 Mar 2001 [EMAIL PROTECTED] wrote:

> Well, here is the .jsp I don't know what could affect it, except
> maybe the scope on the useBean?  It all seemed so logical, and so
> simple.  Just made for exactly what I need to do.

Well, you're getting beyond things I've done (e.g. using beans in
JSPs), so I can't really say for sure, but here's my conjecture as to
what's going on:

You've coded up DataBaseSelect as a servlet, but you're not using it
as a servlet, you're using it as a bean.  So, its init() method is not
getting called (that's done from the servlet container), so the
ServletConfig is null, so you're getting a NullPointerException.

I think your design is a little confused, and you need to better
understand how things work (or maybe I'm the one that's confused :-).

One thing you could do is read the context-param in the JSP -- using a
line like the following:

  application.getInitParameter("dbuser");

and then pass that in the call to select.getResult().


> <html>
> <head>
> <title>Select everything from a database</title>
> </head>
> <body>
> 
> <jsp:useBean id="select" class="DataBaseSelect" scope="request"/>
> 
> <% out.print(select.connect()); %>
> <br>
> <% out.print(select.select()); %>
> 
> <p>Format results
> 
> <br>
> <%@ page import="java.util.Vector" %>
> <% Vector aResult = select.getResult(); %>
> 
> <table>
> <% for (int i=0; i < aResult.size(); i++) { %>
>    <tr>
>        <td> <% out.print(i); %> </td>
>        <td> <% out.print(aResult.elementAt(i)); %> </td>
>    </tr>
> <% } %>
> </table>
> 
> </body>
> </html>
> 
> > -----Original Message-----
> > From:       Milt Epstein [SMTP:[EMAIL PROTECTED]]
> > Sent:       Tuesday, March 13, 2001 3:27 PM
> > To: [EMAIL PROTECTED]
> > Subject:    RE: getServletContext null pointer exception
> > 
> > On Tue, 13 Mar 2001 [EMAIL PROTECTED] wrote:
> > 
> > > Yup, the jsp is in webapps/jdbctut/jdbctut.jsp
> > > 
> > > This is where i get the err.
> > > 
> > > if i use  this stmt.
> > > ==>       String value = getServletContext().getInitParameter("dbuser");
> > > 
> > > or if i use this stmt
> > > ==>
> > >
> > System.out.println(getServletConfig().getServletContext().getInitParameter
> > > 
> > > or if i use these stmts
> > >   ServletConfig sc = getServletConfig(); 
> > > ==>      ServletContext sctx = sc.getServletContext();
> > >   System.out.println(sctx.getInitParameter("dbuser"));
> > [ ... ]
> > 
> > Ahhh, it looks to me like a ServletConfig problem -- probably the well
> > known init ServletConfig problem.  Do you override init(ServletConfig
> > config) in your servlet?  If so, you need to make sure that the first
> > thing it does is call super.init(config).  Or, as introduced in the
> > 2.1 API, you can override a no-argument init() method.
> > 
> > Scratch that -- I see that your servlet code is still included below,
> > including the overriden init, and it looks like you do properly call
> > super.init(config).  But from what you indicated above, it looks like
> > the ServletConfig or the ServletContext is null.  I'm not sure why
> > else that would be.
> > 
> > How are you calling the servlet?  Is that happening in the JSP?  Maybe
> > it has something to do with that.
> > 
> > Not sure what else to suggest at this point.
> > 
> > 
> > > > -----Original Message-----
> > > > From:   Milt Epstein [SMTP:[EMAIL PROTECTED]]
> > > > Sent:   Tuesday, March 13, 2001 2:23 PM
> > > > To:     [EMAIL PROTECTED]
> > > > Subject:        RE: getServletContext null pointer exception
> > > > 
> > > > On Tue, 13 Mar 2001 [EMAIL PROTECTED] wrote:
> > > > 
> > > > > Thanks for your reply,
> > > > > 
> > > > > This is the URL I am using
> > > > http://localhost:8080/jdbctut/jdbctut.jsp
> > > > > 
> > > > > The class file is going into the  webapps/jdbctut/Web-inf/classes
> > of
> > > > the
> > > > > standard tom-cat installation.
> > > > [ ... ]
> > > > 
> > > > Hmmm.  I didn't realize you were using a JSP.  But since this is
> > > > context-param's we're talking about, I don't think it should matter
> > > > (how you set up init-param's varies slightly for servlets and JSPs).
> > > > 
> > > > Where is the JSP file located?  webapps/jdbctut/jdbctut.jsp?
> > > > 
> > > > Can you give more info about what the problem is, what error you are
> > > > getting?  You say you are getting a null pointer exception.  What does
> > > > the stack trace look like on that?  Does it indicate where the
> > > > exception occurs (i.e. what line)?  You might need to turn off the
> > > > compiler to see the line numbers.
> > > > 
> > > > 
> > > > > > -----Original Message-----
> > > > > > From:       Milt Epstein [SMTP:[EMAIL PROTECTED]]
> > > > > > Sent:       Saturday, March 10, 2001 9:26 AM
> > > > > > To: [EMAIL PROTECTED]
> > > > > > Subject:    Re: getServletContext null pointer exception
> > > > > > 
> > > > > > On Fri, 9 Mar 2001 [EMAIL PROTECTED] wrote:
> > > > > > 
> > > > > > > Hi,
> > > > > > > 
> > > > > > > I have been unable to get at the servletcontext param values.  I
> > > > keep
> > > > > > > getting the null pointer exception.
> > > > > > > I have looked thru all the archives and quite a few sites trying
> > all
> > > > the
> > > > > > > recommendations, but to no avail.
> > > > > > > Must be doing something really stupid, or I've missed a basic
> > > > concept
> > > > > > > somewhere.
> > > > > > > I am running tomcat 3.2.1
> > > > > > > 
> > > > > > > Any hints would be gratefully appreciated.
> > > > > > > Thanks in advance.
> > > > > > > Chuck
> > > > > > 
> > > > > > What URL are you using to access the servlet?  Where (in the
> > directory
> > > > > > structure) is your servlet class located?
> > > > > > 
> > > > > > 
> > > > > > > Here's my web.xml and the code trying to get at it.
> > > > > > > 
> > > > > > > <web-app>
> > > > > > >     <display-name>jdbctut</display-name>
> > > > > > >     <description>This is version X.X of an application to
> > > > > > > perform</description>
> > > > > > >     <context-param>
> > > > > > >       <param-name>dbuser</param-name>
> > > > > > >       <param-value>fabdev</param-value>
> > > > > > >       <description>User name to logon to the Db.</description>
> > > > > > >     </context-param>
> > > > > > >     <servlet>
> > > > > > >       <servlet-name>DataBaseSelect</servlet-name>
> > > > > > >       <description> This servlet </description>
> > > > > > >       <servlet-class>DataBaseSelect</servlet-class>
> > > > > > >       <!-- Load this servlet at server startup time -->
> > > > > > >       <load-on-startup>5</load-on-startup>
> > > > > > >     </servlet>
> > > > > > >     <servlet-mapping>
> > > > > > >       <servlet-name>DataBaseSelect</servlet-name>
> > > > > > >       <url-pattern>jdbctut</url-pattern>
> > > > > > >     </servlet-mapping>
> > > > > > >     <session-config>
> > > > > > >       <session-timeout>30</session-timeout>    <!-- 30 minutes
> > -->
> > > > > > >     </session-config>
> > > > > > > </web-app>
> > > > > > > 
> > > > > > > import java.sql.*;
> > > > > > > import java.util.Vector;
> > > > > > > import javax.servlet.http.*;
> > > > > > > import javax.servlet.*;
> > > > > > > import java.io.*;
> > > > > > > public class DataBaseSelect extends HttpServlet {
> > > > > > >    
> > > > > > >    private Vector result;
> > > > > > >    private String url =
> > > > > > >
> > > >
> > "jdbc:informix-sqli://poldev:2005/fabdev:INFORMIXSERVER=poldev_713_tcp";
> > > > > > > 
> > > > > > >   public void init( ServletConfig config ) throws
> > > > > > > javax.servlet.ServletException{
> > > > > > >      super.init( config ) ; // Essential!!
> > > > > > >   }
> > > > > > >      
> > > > > > >    public DataBaseSelect() {
> > > > > > >       result = new Vector();
> > > > > > >    } // constructor DataBaseSelect
> > > > > > >    
> > > > > > >    public String connect() {
> > > > > > >       try {
> > > > > > >    Class.forName("com.informix.jdbc.IfxDriver").newInstance();
> > > > > > >    return "Driver Loaded!";
> > > > > > >       } catch (Exception E) {
> > > > > > >    return "Unable to load driver.";
> > > > > > >       }
> > > > > > >    }
> > > > > > >    
> > > > > > >    public String select() {
> > > > > > > //        String value =
> > > > getServletContext().getInitParameter("dbuser");
> > > > > > > //
> > > > > > >
> > > > > >
> > > >
> > System.out.println(getServletConfig().getServletContext().getInitParameter
> > > > > > ("
> > > > > > > dbuser"));
> > > > > > >   ServletConfig sc = getServletConfig(); 
> > > > > > >             ServletContext sctx = sc.getServletContext();
> > > > > > >   System.out.println(sctx.getInitParameter("dbuser"));
> > > > > > > 
> > > > > > >       try {
> > > > > > >    Connection C = DriverManager.getConnection(url, "fabdev",
> > > > > > > "fabdev$");
> > > > > > >    Statement Stmt = C.createStatement();
> > > > > > >    ResultSet myResult = Stmt.executeQuery("SELECT lst_nm from
> > > > > > > person_profile ORDER BY lst_nm");
> > > > > > >    
> > > > > > >    while (myResult.next()) {
> > > > > > >       result.addElement(myResult.getString(1));
> > > > > > >    }
> > > > > > >    
> > > > > > >          // Clean up
> > > > > > >          myResult.close();
> > > > > > >          Stmt.close();
> > > > > > >          C.close();
> > > > > > >    return "Connection Success!";
> > > > > > >       } catch (SQLException E) {
> > > > > > >           return "SQLException: " + E.getMessage();
> > > > > > >       }
> > > > > > >    }
> > > > > > >       
> > > > > > >    /**
> > > > > > >     * Accessor for result
> > > > > > >     **/
> > > > > > >    public Vector getResult() {
> > > > > > >       return result;
> > > > > > >    }
> > > > > > >    
> > > > > > >    /**
> > > > > > >     * Mutator for result
> > > > > > >     **/
> > > > > > >    public void setResult(Vector avector) {
> > > > > > >      result = avector;
> > > > > > >    }  
> > > > > > >    
> > > > > > > } // class DataBaseSelect
> > > > > > > 
> > 
> > Milt Epstein
> > Research Programmer
> > Software/Systems Development Group
> > Computing and Communications Services Office (CCSO)
> > University of Illinois at Urbana-Champaign (UIUC)
> > [EMAIL PROTECTED]
> > 
> > 
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: [EMAIL PROTECTED]
> > For additional commands, email: [EMAIL PROTECTED]
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, email: [EMAIL PROTECTED]
> 

Milt Epstein
Research Programmer
Software/Systems Development Group
Computing and Communications Services Office (CCSO)
University of Illinois at Urbana-Champaign (UIUC)
[EMAIL PROTECTED]


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, email: [EMAIL PROTECTED]

Reply via email to