Exception in thread

Hi everybody,

I am getting "Exception in thread "main" java.lang.NullPointerException" when I try to run the code. The pointed lines for the error are all related to searchDB method. Please, anyone can help figuring out what's wrong?

package onlineshopping;

import javax.swing.*; //for frame class

import java.awt.event.*; //for GUI event Handlers

import java.awt.*; //for container class

import java.sql.*;

public class OnlineShopping extends JFrame

{

// Data Members

private Connection OnlineShopping;

public Statement onlineStmt;

public ResultSet onlineRs;

// Constructors

public OnlineShopping(String title) {

super(title);

loadDriver();

connectDatabase();

int test[] = new int[100];

test = searchDB("clothes","Condition","used");

//JOptionPane.showMessageDialog(null,test[0]);

//JOptionPane.showMessageDialog(null,test[1]);

//JOptionPane.showMessageDialog(null,test[2]);

//JOptionPane.showMessageDialog(null,test[3]);

//writeToDB("clothes","Title","ok3");

//writeToDB("clothes","Quantity",7);

//addWindowListener(new WindowHandler());

}

//methods

private void loadDriver()

{

try

{

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

}

catch (ClassNotFoundException err)

{

JOptionPane.showMessageDialog(null,"Could not Load Sun Driver");

System.exit(1);

}

}

private void connectDatabase()

{

try

{

OnlineShopping = DriverManager.getConnection("jdbc:odbc:OnlineShopping");

}

catch (SQLException error)

{

JOptionPane.showMessageDialog(null,"Error Connecting to database:\n" + error.toString());

}

}

public void createResultSet(String tableName)

{

try

{

onlineStmt = OnlineShopping.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,

ResultSet.CONCUR_UPDATABLE);

onlineRs = onlineStmt.executeQuery("Select * from "+tableName+ " ;");

}

catch (SQLException error)

{

JOptionPane.showMessageDialog(null,"Error Connecting to database:\n" + error.toString());

}

}

private void writeToDB(String tableName,String columnTitle,String dataEntry)

{

try

{

createResultSet(tableName);

onlineRs.moveToInsertRow();

onlineRs.updateString(columnTitle,dataEntry);

onlineRs.insertRow();

}

catch (SQLException error)

{

JOptionPane.showMessageDialog(null,"Error Connecting to database:\n" + error.toString());

}

}

private void writeToDB(String tableName,String columnTitle,int dataEntry)

{

try

{

createResultSet(tableName);

onlineRs.moveToInsertRow();

onlineRs.updateInt(columnTitle,dataEntry);

onlineRs.insertRow();

}

catch (SQLException error)

{

JOptionPane.showMessageDialog(null,"Error Connecting to database:\n" + error.toString());

}

}

public int[] searchDB(String tableName,String columnTitle,String searchWord)

{

int searchReturn[]= new int[100];

int i=0;

try

{

createResultSet(tableName);

while (onlineRs.next())

{

if (onlineRs.getString(columnTitle).toLowerCase().contains(searchWord.toLowerCase())){

searchReturn=onlineRs.getInt(1);

i++;

}

}

}

catch (SQLException error)

{

JOptionPane.showMessageDialog(null,"Error Connecting to database:\n" + error.toString());

}

return searchReturn;

}

private class WindowHandler extends WindowAdapter

{

public void windowClosing(WindowEvent e)

{

System.exit(0);

}

}

}

The main only constucts an OnlineShopping object.

[3883 byte] By [Magellooa] at [2007-11-27 3:27:44]
# 1

Use the code tags. it makes it easier to read

public int[] searchDB(String tableName,String columnTitle,String searchWord)

{

int searchReturn[]= new int[100];

int i=0;

try

{

createResultSet(tableName);

while (onlineRs.next())

{

if (onlineRs.getString(columnTitle).toLowerCase().contains(searchWord.toLowerCase())) {

searchReturn=onlineRs.getInt(1);

i++;

}

}

}

catch (SQLException error)

{

JOptionPane.showMessageDialog(null,"Error Connecting to database:\n" + error.toString());

}

return searchReturn;

}

for this line in particular

(onlineRs.getString(columnTitle).toLowerCase().contains(searchWord.toLowerCase()))

- I would make sure that your result set has values. Make sure that onlineRs.getString(columnTitle) doesn't equal null first.

- You are incrementing the i counter in there, but never use it. Is that supposed to be used for onlineRs.getInt(i)

maybe?

- I don't think you need that semicolon at the end of the query in createResult method.

hope this helps

kdajania at 2007-7-12 8:30:31 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...