Java Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesJava Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old March 28th, 2003, 12:12 AM
supersonic supersonic is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2001
Posts: 11 supersonic User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation 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

Reply With Quote
  #2  
Old March 28th, 2003, 08:23 AM
Nemi Nemi is offline
Clueless llama
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Feb 2001
Location: Lincoln, NE. USA
Posts: 2,353 Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 2 Days 12 h 35 m 19 sec
Reputation Power: 111
Can you post the whole stack trace? It may say where the null pointer exception is happening at.

Reply With Quote
  #3  
Old March 28th, 2003, 11:57 AM
ishnid's Avatar
ishnid ishnid is offline
kill 9, $$;
Dev Shed God 1st Plane (5500 - 5999 posts)
 
Join Date: Sep 2001
Location: Dublin, Eire
Posts: 5,566 ishnid User rank is General 6th Grade (Above 100000 Reputation Level)ishnid User rank is General 6th Grade (Above 100000 Reputation Level)ishnid User rank is General 6th Grade (Above 100000 Reputation Level)ishnid User rank is General 6th Grade (Above 100000 Reputation Level)ishnid User rank is General 6th Grade (Above 100000 Reputation Level)ishnid User rank is General 6th Grade (Above 100000 Reputation Level)ishnid User rank is General 6th Grade (Above 100000 Reputation Level)ishnid User rank is General 6th Grade (Above 100000 Reputation Level)ishnid User rank is General 6th Grade (Above 100000 Reputation Level)ishnid User rank is General 6th Grade (Above 100000 Reputation Level)ishnid User rank is General 6th Grade (Above 100000 Reputation Level)ishnid User rank is General 6th Grade (Above 100000 Reputation Level)ishnid User rank is General 6th Grade (Above 100000 Reputation Level)ishnid User rank is General 6th Grade (Above 100000 Reputation Level)ishnid User rank is General 6th Grade (Above 100000 Reputation Level)ishnid User rank is General 6th Grade (Above 100000 Reputation Level) 
Time spent in forums: 3 Months 1 Week 4 Days 39 m 22 sec
Reputation Power: 1395
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

Reply With Quote
  #4  
Old March 28th, 2003, 11:58 AM
bricker42 bricker42 is offline
Moderator =(8^(|)
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Feb 2002
Location: Sacramento, CA
Posts: 1,710 bricker42 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 20 m 38 sec
Reputation Power: 8
Send a message via AIM to bricker42
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

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesJava Help > Please help...


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway