|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
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
|
|||
|
|||
|
Can you post the whole stack trace? It may say where the null pointer exception is happening at.
|
|
#3
|
||||
|
||||
|
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
__________________
~ishnid; Have you tried: [ search.cpan.org | perldoc | Java API | mysql.com | google ] Apostrophes are NOT used for possessive pronouns or for noun plurals, including acronyms. |
|
#4
|
|||
|
|||
|
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 |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Java Help > Please help... |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|