aspose file tools*
The moose likes Beginning Java and the fly likes run-time error HELP! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "run-time error HELP!" Watch "run-time error HELP!" New topic
Author

run-time error HELP!

Naf Rash
Ranch Hand

Joined: Feb 19, 2004
Posts: 85
I've been working on this program for over a week now and can't seem to understand why my program isn't working. As far as I can tell it stops working at line 238:
if (e.getSource() == url && e.getSource() == uid && e.getSource()==password && e.getSource() == mine){
As far as I can see everything is ok and there are no compilation errors so i can't understand why it won't work. The class is supposed to allow a user to make a connection to the database. The user initially presses 'Start' and then a new form can be seen asking the user to specify the database and connection details for a connection to be established. However, once this has been done and the mine! button is pressed, nothing happens. What should happen is that an attempt to establish connection is made and if this has not been made possible the relevant error message is displayed- but the code never gets this far. I would really appreciate any help you could provide me. Below is the code:

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.sql.*;
import java.util.*;
public class connectionBox extends JFrame {
private JTextField url, uid;
private JPasswordField password;
private JLabel urllabel, uidlabel, passwordlabel;
private connectionBox cb;
private JButton mine;
Connection connection;
public connectionBox(){
super ("Establishing a connection");
JMenuBar bar = new JMenuBar();//creates the menu bar
setJMenuBar(bar);//sets the menu-bar for the Frame
//create File Menu and Exit Menu
JMenu fileMenu = new JMenu ("File");
fileMenu.setMnemonic('F');
JMenuItem startMining = new JMenuItem ("Start Mining");
startMining.setMnemonic('N');
startMining.addActionListener(
new ActionListener(){
public void actionPerformed (ActionEvent e){
// if this happens then get the name of the database
//cb = new connectionBox();
Container c = getContentPane();
c.setLayout(new FlowLayout());

/* The following code provides a text box and asks the user to enter the database name for a
* conncetion to be established to it.*/
JPanel northPanel = new JPanel();
urllabel = new JLabel (" Enter the name of the database.");
northPanel.add(urllabel,BorderLayout.NORTH);
c.add(northPanel,BorderLayout.NORTH);
url = new JTextField (15);
//url.addActionListener (this);
c.add(url,BorderLayout.NORTH);
/* The following code provides a text box and asks for the User-ID to allow a connection to the
* database to be established. */
JPanel centrePanel = new JPanel();
uidlabel = new JLabel (" Enter the User-ID for the specified database."
+ "NB: If no User-ID exists enter 'null'.");
centrePanel.add(uidlabel);
c.add(centrePanel,BorderLayout.CENTER);
uid = new JTextField (15);
c.add(uid,BorderLayout.CENTER);
/* The following code provides a text box and asks for the Password to allow a connection to the
* database to be established. */
JPanel southPanel = new JPanel();
passwordlabel = new JLabel (" Enter the Password for the specified" +
"database. NB: If no Password exists enter 'null'.");
southPanel.add(passwordlabel);
c.add(southPanel, BorderLayout.CENTER);
password = new JPasswordField (15);
c.add(password, BorderLayout.CENTER);
/*The button 'Mine' is pressed to initiate the connection*/
JPanel minePanel= new JPanel();
mine = new JButton("Mine!");
mine.addActionListener(this);
minePanel.add(mine, BorderLayout.SOUTH);
c.add(minePanel, BorderLayout.SOUTH);

ActionHandler handler = new ActionHandler();
url.addActionListener(handler); //look at green book pg 567
uid.addActionListener(handler);
password.addActionListener(handler);
setSize (500, 300);
show();
}
}
);
fileMenu.add(startMining);
JMenuItem exit = new JMenuItem ("Exit");
exit.setMnemonic ('X');
exit.addActionListener(
new ActionListener(){
public void actionPerformed (ActionEvent e){
System.exit(0);
}
}
);
fileMenu.add(exit);
bar.add(fileMenu); //add the File menu

//create the Help menu
JMenu helpMenu = new JMenu ("Help");
helpMenu.setMnemonic ('h');
JMenuItem startHelp = new JMenuItem ("Start Help");
startHelp.setMnemonic('s');
startHelp.addActionListener(
new ActionListener(){
public void actionPerformed (ActionEvent e){
// if this happens then implement the documentation

}
}
);

helpMenu.add(startHelp);
bar.add(helpMenu); //add the File menu


/* The following code is for the buttons that user sees, allowing the user to utilise the data
* mining tool without having to go through the menus avaialable, ie, having to go through the
* file menu. */
Container contentPane = getContentPane();
JPanel panel = new JPanel();
final JButton startButton = new JButton ("Start");
final JButton exitButton = new JButton ("Exit");

startButton.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e){
//super ("Connection Specification"); is there a way of changing the
//original title to this when this section is ordered.
startButton.setEnabled(false);
startButton.setVisible(false);

exitButton.setEnabled(false);
exitButton.setVisible(false);
Container c = getContentPane();
c.setLayout(new FlowLayout());
/* The following code provides a text box and asks for the URL of the database that needs to be
* connected to.*/
JPanel northPanel = new JPanel();
urllabel = new JLabel (" Enter the name of the database.");
northPanel.add(urllabel,BorderLayout.NORTH);
c.add(northPanel,BorderLayout.NORTH);
url = new JTextField (15);
//url.addActionListener (this);
c.add(url,BorderLayout.NORTH);
/* The following code provides a text box and asks for the User-ID to allow a connection to the
* database to be established. */
JPanel centrePanel = new JPanel();
uidlabel = new JLabel (" Enter the User-ID for the specified database."
+ "NB: If no User-ID exists enter 'null'.");
centrePanel.add(uidlabel);
c.add(centrePanel,BorderLayout.CENTER);
uid = new JTextField (15);
//uid.addActionListener (this);
c.add(uid,BorderLayout.CENTER);
/* The following code provides a text box and asks for the Password to allow a connection to the
* database to be established. */
JPanel southPanel = new JPanel();
passwordlabel = new JLabel (" Enter the Password for the specified" +
"database. NB: If no Password exists enter 'null'.");
southPanel.add(passwordlabel);
c.add(southPanel, BorderLayout.CENTER);
password = new JPasswordField (15);
// password.addActionListener (this);
c.add(password, BorderLayout.CENTER);
JPanel minePanel= new JPanel();
mine = new JButton("Mine!");
//mine.addActionListener(this);
minePanel.add(mine, BorderLayout.SOUTH);
c.add(minePanel, BorderLayout.SOUTH);
ActionHandler handler = new ActionHandler();
url.addActionListener(handler); // look at green book pg 567
uid.addActionListener(handler);
password.addActionListener(handler);
mine.addActionListener(handler);

setSize (600, 300);
show();
System.out.println();
}
}
);

exitButton.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e){
System.exit(0);
}
}
);
panel.add(startButton);
panel.add (exitButton);
contentPane.add(panel, "South");
setSize(500,200);
show();


}
public static void main (String args[]){
connectionBox app = new connectionBox();
app.addWindowListener(
new WindowAdapter(){
public void windowClosing (WindowEvent e){
System.exit(0);
}
}
);
}

//inner class for event handling
private class ActionHandler implements ActionListener{

public void actionPerformed (ActionEvent e){
String url1="null", uid1="null";


//if (e.getSource() == mine){
if (e.getSource() == url && e.getSource() == uid && e.getSource()==password && e.getSource() == mine){

url1 = url.getText();
uid1 = uid.getText();

//char[] password1 = password.getPassword();
String password1 = password.getText();

JOptionPane.showMessageDialog(null,"mining");
tryConnection(url1, uid1, password1);

}
}
}
public void tryConnection(String url1,String uid1, String password1){

// String url1="null", uid1="null", password1="null";

try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
connection = DriverManager.getConnection(url1, uid1, password1);
}
catch(ClassNotFoundException cnfex){
System.err.println("Failed to load JDBC/ODBC driver.");
Container c = getContentPane();
c.setLayout(new FlowLayout());
//go and ask for the input again- does above do this?
return;
}
catch (SQLException sqlex){
System.err.println("Unable to connect.");
Container c = getContentPane();
c.setLayout(new FlowLayout());
//ask for the input again- does above do this?
}

JOptionPane.showMessageDialog(null,"Connection Successful!");
getTable();
}
private void getTable(){
Statement statement;
ResultSet resultSet = null;
String table;
try{
table =JOptionPane.showInputDialog ("Enter name of the table to be mined.");
String query = "SELECT * FROM " + table;
statement = connection.createStatement();
resultSet = statement.executeQuery(query);
displayResultSet(resultSet);
statement.close();
}
catch (SQLException e){
System.err.println("Problem connecting to the table.");
return;
//show some dialog expressing the dialog
}

//displayResultSet(resultSet);
}
private void displayResultSet(ResultSet rs)throws SQLException{

boolean moreRecords = rs.next(); //position to first records
//If there are no records, display a message
if (! moreRecords){
JOptionPane.showMessageDialog (this, "The specified table contains no"
+ " records");
setTitle ("No records to display");
return;
}
setTitle("table-method pass us this name" + " table from " + "url-get this");
Vector columnHeads = new Vector ();
Vector rows = new Vector();
try{
//get column heads
ResultSetMetaData rsmd = rs.getMetaData();
for (int i = 1; i <= rsmd.getColumnCount(); ++i)
columnHeads.addElement(rsmd.getColumnName(i));
//get row data
do{
rows.addElement(getNextRow(rs, rsmd));
} while (rs.next());
//display table with ResultSet contents
JTable thetable = new JTable (rows, columnHeads);
JScrollPane scroller = new JScrollPane(thetable);
getContentPane().add(scroller, BorderLayout.CENTER);
validate();
}
catch (SQLException e){
//display some message
}

}

private Vector getNextRow(ResultSet rs, ResultSetMetaData rsmd)
throws SQLException{
Vector currentRow = new Vector();
for (int i=1; i <= rsmd.getColumnCount(); ++i)
switch (rsmd.getColumnType(i)){
case Types.VARCHAR:currentRow.addElement(rs.getString(i));
break;
case Types.INTEGER:currentRow.addElement(new Long (rs.getLong (i)));
break;
default:
System.out.println("Type was: " + rsmd.getColumnTypeName (i));
}
return currentRow;
}
}
Billybob Marshall
Ranch Hand

Joined: Jan 27, 2004
Posts: 202
Originally posted by Naf Rash:
if (e.getSource() == url && e.getSource() == uid && e.getSource()==password && e.getSource() == mine){

Unless url, uid, password, and mine all reference the same object/null, that statement can't possibly ever evaluate to true. Perhaps you meant || (or) instead of && (and)?
Tom Blough
Ranch Hand

Joined: Jul 31, 2003
Posts: 263

It's probably because e.getSource() does not equal all four of those objects at the same time. Are you sure you did not mean to use "OR"?

Tom Blough


Tom Blough<br /> <blockquote><font size="1" face="Verdana, Arial">quote:</font><hr>Cum catapultae proscriptae erunt tum soli proscripti catapultas habebunt.<hr></blockquote>
Naf Rash
Ranch Hand

Joined: Feb 19, 2004
Posts: 85
Thank-you for your responses but all 4 things need to be satisfied before an attempt to make the connection can be made ie, the database name, the user-id and password need to be entered in the releveant textfields and the mine! button pressed. Do you have any other suggestions?
Billybob Marshall
Ranch Hand

Joined: Jan 27, 2004
Posts: 202
Originally posted by Naf Rash:
Thank-you for your responses but all 4 things need to be satisfied before an attempt to make the connection can be made ie, the database name, the user-id and password need to be entered in the releveant textfields and the mine! button pressed. Do you have any other suggestions?

Then you want something like this:
if ((uid.getText().length() > 0) && (password.getText().length() > 0) && ...
Naf Rash
Ranch Hand

Joined: Feb 19, 2004
Posts: 85
Thank-You! It worked- I have got other problems but they should be simple to sort out! Thanks again!!!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: run-time error HELP!