Thread: Please help...

    #1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2001
    Posts
    11
    Rep Power
    0

    Exclamation Please help...


    Please help... Everytime I access db.jsp I get the following error:

    org.apache.jasper.JasperException: Exception: An unknown error occurred: java.lang.NullPointerException

    Here's my javabean


    package beans.bag;

    import java.sql.*;
    import java.util.*;

    public class connectDB {

    /* DECLARE VARIABLES */
    String error;

    String host = "localhost";
    String user = "";
    String pass = "";
    // private String database;
    Connection con;
    // private ResultSet data;

    // public connectDB() {}

    public void connect(String database) throws ClassNotFoundException, SQLException, Exception {
    try {
    Class.forName("com.mysql.jdbc.Driver").newInstance();
    String dbh = "jdbc:mysql://" + host + "/" + database + "?user=" + user + "&password=" + pass;
    Connection con = DriverManager.getConnection(dbh);
    }
    catch (ClassNotFoundException cnfE) {
    error = "ClassNotFoundException: Could not locate DB driver.";
    throw new ClassNotFoundException (error);
    }
    catch (SQLException sqlE) {
    error = "SQLException: Could not connect to " + database;
    throw new SQLException (error);
    }
    catch (Exception eE) {
    error = "Exception: An unknown error occured: " + eE;
    }
    }

    public ResultSet returnResults() throws SQLException, Exception {
    ResultSet rs;
    try {
    Statement sth = con.createStatement();
    String query = "SELECT * FROM 01_Users";
    rs = sth.executeQuery(query);
    }
    catch (SQLException sqlE) {
    error = "Fcuuuuu" + sqlE;
    throw new SQLException (error);
    }
    catch (Exception eE) {
    error = "Exception: An unknown error occurred: " + eE;
    throw new Exception (error);
    }

    return rs;
    }
    }


    And here's my jsp page


    <%@ page contentType="text/html; charset=iso-8859-1" language="java" import="java.sql.*, java.io.*, java.util.*" %>
    <jsp:useBean id="dbc" scope="page" class="beans.bag.connectDB" />

    <html>
    <head>
    <title>Database<title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    </head>

    <body>

    <%
    dbc.connect ("epower_e_power");
    ResultSet rs = dbc.returnResults ();
    while (rs.next()) {
    // some stuff happens
    }
    %>

    </body>
    </html>


    Can some one please help me...
    Thanking you in advance

    Frank
  2. #2
  3. No Profile Picture
    Clueless llama
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Feb 2001
    Location
    Lincoln, NE. USA
    Posts
    2,353
    Rep Power
    117
    Can you post the whole stack trace? It may say where the null pointer exception is happening at.
  4. #3
  5. kill 9, $$;
    Devshed Supreme Being (6500+ posts)

    Join Date
    Sep 2001
    Location
    Shanghai, An tSín
    Posts
    6,898
    Rep Power
    3887
    I haven't used beans myself but I would imagine that your connectDB class needs a constructor. Your NullPointerException is possibly caused because you're not instantiating your connectDB class. Try this at the top of it:
    Code:
    /* DECLARE VARIABLES */
    private String error;
    private String host;
    private String user;
    private String pass;
    private Connection con;
    
    public connectDB() {
       host = "localhost";
       user = "blah";
       pass="blah";
    }
    I also see in your connect() method you have declared 'con' as a Connection object again. You've already done that when you're declaring variables at the top so you probably shouldn't do it again.

    Of course, as I said, I haven't used beans before so I could be totally wrong - still, no harm in trying eh?
    ~ishnid
  6. #4
  7. No Profile Picture
    Moderator =(8^(|)
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2002
    Location
    Sacramento, CA
    Posts
    1,710
    Rep Power
    14
    I don't know if this is the problem, but try changing
    Code:
     Connection con = DriverManager.getConnection(dbh);
    to be
    Code:
    con = DriverManager.getConnection(dbh);
    Also, you should probably make your host/user/pass/con variables private, but that's a seperate issue .
    -james

IMN logo majestic logo threadwatch logo seochat tools logo