Database to table problem

Hi,

The basic thing I'm trying to do here is output a set of results from a SQL query to a table and I keep getting the following error:

Exception in thread"AWT-EventQueue-0" java.lang.NullPointerException

at clsDatabase.runQuery(clsDatabase.java:46)

at frmAppointmentSearch.btnSearchActionPerformed(frmAppointmentSearch.java:404)

at frmAppointmentSearch.access$600(frmAppointmentSearch.java:12)

at frmAppointmentSearch$8.actionPerformed(frmAppointmentSearch.java:177)

at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)

at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)

at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)

at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)

at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)

at java.awt.Component.processMouseEvent(Component.java:6038)

at javax.swing.JComponent.processMouseEvent(JComponent.java:3260)

at java.awt.Component.processEvent(Component.java:5803)

at java.awt.Container.processEvent(Container.java:2058)

at java.awt.Component.dispatchEventImpl(Component.java:4410)

at java.awt.Container.dispatchEventImpl(Container.java:2116)

at java.awt.Component.dispatchEvent(Component.java:4240)

at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4322)

at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3986)

at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3916)

at java.awt.Container.dispatchEventImpl(Container.java:2102)

at java.awt.Window.dispatchEventImpl(Window.java:2429)

at java.awt.Component.dispatchEvent(Component.java:4240)

at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)

at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273)

at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183)

at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:173)

at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168)

at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160)

at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)

And the following is the code I'm trying to use (its a Microsoft Access database, if that's of any help):

clsDatabase sales =new clsDatabase();

try{

sales.loadDriver();

results = sales.runQuery(criteria,"search","null");

int columnID = 1;

int rowID = 1;

while(results.next() ==true)

{

tblData.setValueAt(results.getString(columnID), rowID, columnID);

columnID++;

if (Math.IEEEremainder((double)columnID, 6.0) != 0.0)

{

//If all six columns of a row have been filled, move to the next row

columnID = 1;

++rowID;

//tblData.editCellAt(rowID,columnID);

}

else

{

}

}

}catch (ClassNotFoundException ex){

ex.printStackTrace();

}catch (SQLException ex){

ex.printStackTrace();

}

Here is the code I'm using to connect my database to my program:

import java.sql.*;

import java.util.*;

publicclass clsDatabase{

Connection connection;

Statement statement;

/** Creates a new instance of clsDatabase */

public clsDatabase(){

}

publicvoid loadDriver()throws ClassNotFoundException{

try{

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

connection = DriverManager.getConnection("jdbc:odbc:sales_database");

}

catch (ClassNotFoundException ex){

ex.printStackTrace();

}catch (SQLException ex){

ex.printStackTrace();

}

}

public ResultSet runQuery(String criteria, String type, String deleteID)

{

try{

boolean foundResults =false;

ResultSet myData;

if (type =="search")

{

if(statement.execute(criteria) !=false)

{

foundResults =true;

}

if (foundResults ==true)

{

myData = statement.getResultSet();

ResultSetMetaData info = myData.getMetaData();

return myData;

}

}

elseif (type =="deleteCustomer")

{

statement.execute("SELECT * FROM Customer WHERE CustomerID = '" + deleteID +"'");

myData = statement.getResultSet();

myData.deleteRow();

statement.execute("SELECT * FROM Customer");

myData = statement.getResultSet();

return myData;

}

elseif (type =="deleteAppointment")

{

statement.execute("SELECT * FROM Appointment WHERE AppointmentID = '" + deleteID +"'");

myData = statement.getResultSet();

myData.deleteRow();

statement.execute("SELECT * FROM Appointment");

myData = statement.getResultSet();

return myData;

}

elseif (type =="editCustomer")

{

statement.execute("SELECT * FROM Customer");

myData = statement.getResultSet();

return myData;

}

elseif (type =="editAppointment")

{

statement.execute("SELECT * FROM Appointment");

myData = statement.getResultSet();

return myData;

}

else

{

returnnull;

}

}catch (SQLException ex){

ex.printStackTrace();

}

returnnull;

}

}

Sorry for the long post, but I figured it would be best to get it all out there at once. Also, I have already linked the database in the ODBC applet within Administrative Tools under the Control Panel. Any ideas?

Thanks in advance.

[9184 byte] By [bcwolfea] at [2007-11-27 0:35:56]
# 1

The obvious idea would be to read the stack trace and act on it:Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException

at clsDatabase.runQuery(clsDatabase.java:46)That says that line 46, in method runQuery in your clsDatabase class, is throwing a NullPointerExpression. That's in your code. Look at line 46 (your text editor will tell you which line that is) and find out why.

DrClapa at 2007-7-11 22:44:58 > top of Java-index,Java Essentials,Java Programming...
# 2

I already knew that...I don't know why the error is being thrown. I was simply posting the background code so that anyone reading this thread could see potentially what the root of the problem was. The line that's erroring out is:

if(statement.execute(criteria) != false)

Which is where the SQL statement is supposed to be run as a query on the database. Any useful ideas?

bcwolfea at 2007-7-11 22:44:58 > top of Java-index,Java Essentials,Java Programming...
# 3
What is the value of criteria at this point in the code?use a System.out.println to take a look.
ChristopherAngela at 2007-7-11 22:44:58 > top of Java-index,Java Essentials,Java Programming...
# 4

It looks to me that you're not initializing the statement

if (type == "search")

{

// you need to initialize statement before you try the next line

if(statement.execute(criteria) != false)

{

foundResults = true;

}

if (foundResults == true)

{

myData = statement.getResultSet();

ResultSetMetaData info = myData.getMetaData();

return myData;

}

}

It's possible that you are and I'm not seeing it, but that what leaps out in my mind, and if it's not initialized it would give exactly the error you're seeing.

Hope this helps.

PS.

puckstopper31a at 2007-7-11 22:44:58 > top of Java-index,Java Essentials,Java Programming...
# 5

> I already knew that...I don't know why the error is

> being thrown. I was simply posting the background

> code so that anyone reading this thread could see

> potentially what the root of the problem was. The

> line that's erroring out is:

>

> if(statement.execute(criteria) !=

> false)

>

> Which is where the SQL statement is supposed to be

> run as a query on the database. Any useful ideas?

Try to initialize your statement first:

statement = connection.createStatement();

Danniel_Williana at 2007-7-11 22:44:58 > top of Java-index,Java Essentials,Java Programming...
# 6

On top of all the advices, I just want to point out one more issue:

the original poster is comparing String using "==":

if (type == "search")

Strings must be compared using equals() instead (unless he/she knows the difference and really wants "=="):

if (type.equals("search"))

KathyMcDonnella at 2007-7-11 22:44:58 > top of Java-index,Java Essentials,Java Programming...