Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I can't seem to figure out how to correct the following error. I have tried different initiating methods but none seem to work and I

I can't seem to figure out how to correct the following error. I have tried different initiating methods but none seem to work and I continue to get the same error.

What I am trying to do is run my Run_Query method in ResultSetTableModel class when the Execute_Command button is pressed in my DisplayQueryResult class. I've run the Run_Query function numerous of times and get the result needed but when I call it when the button is pressed the program is unable to run through the method. Any help will be appreciated.

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

at DisplayQueryResult$2.actionPerformed(DisplayQueryResult.java:106)

at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)

at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)

at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)

at javax.swing.DefaultButtonModel.setPressed(Unknown Source)

at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)

at java.awt.Component.processMouseEvent(Unknown Source)

at javax.swing.JComponent.processMouseEvent(Unknown Source)

at java.awt.Component.processEvent(Unknown Source)

at java.awt.Container.processEvent(Unknown Source)

at java.awt.Component.dispatchEventImpl(Unknown Source)

at java.awt.Container.dispatchEventImpl(Unknown Source)

at java.awt.Component.dispatchEvent(Unknown Source)

at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)

at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)

at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)

at java.awt.Container.dispatchEventImpl(Unknown Source)

at java.awt.Window.dispatchEventImpl(Unknown Source)

at java.awt.Component.dispatchEvent(Unknown Source)

at java.awt.EventQueue.dispatchEventImpl(Unknown Source)

at java.awt.EventQueue.access$500(Unknown Source)

at java.awt.EventQueue$3.run(Unknown Source)

at java.awt.EventQueue$3.run(Unknown Source)

at java.security.AccessController.doPrivileged(Native Method)

at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)

at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)

at java.awt.EventQueue$4.run(Unknown Source)

at java.awt.EventQueue$4.run(Unknown Source)

at java.security.AccessController.doPrivileged(Native Method)

at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)

at java.awt.EventQueue.dispatchEvent(Unknown Source)

at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)

at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)

at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)

at java.awt.EventDispatchThread.pumpEvents(Unknown Source)

at java.awt.EventDispatchThread.pumpEvents(Unknown Source)

at java.awt.EventDispatchThread.run(Unknown Source)

Here is my code

ResultSetTableModel

import java.sql.Connection;

import java.sql.Statement;

import java.sql.DriverManager;

import java.sql.ResultSet;

import java.sql.ResultSetMetaData;

import javax.swing.table.AbstractTableModel;

import java.sql.SQLException;

import java.util.Properties;

import javax.sql.DataSource;

import com.mysql.jdbc.*;

import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

public class ResultSetTableModel

{

private Connection connection;

private Statement statement;

private ResultSet resultSet;

private ResultSetMetaData metaData;

private int numberOfRows;

private boolean connectedToDatabase;

DisplayQueryResult test = new DisplayQueryResult();

public void Run_Query() throws SQLException

{

resultSet = statement.executeQuery("select * from bikes");

metaData = resultSet.getMetaData();

int numberOfColumns = metaData.getColumnCount();

for(int i = 1; i <= numberOfColumns; i++)

{

System.out.printf("%-20s\t", metaData.getColumnName(i));

}

System.out.println();

for(int i = 1; i <= numberOfColumns; i++)

{

System.out.printf("%-20s\t", "-----------------");

}

System.out.println();

while(resultSet.next())

{

for(int i = 1; i <= numberOfColumns; i++)

{

System.out.printf("%-20s\t", resultSet.getObject(i));

}

System.out.println();

}

statement.close();

connection.close();

}

public void setConnection(Connection con) throws SQLException

{

connection = con;

System.out.println(" Yes!");

statement = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);

//Run_Query();

/*resultSet = statement.executeQuery("select * from bikes");

metaData = resultSet.getMetaData();

int numberOfColumns = metaData.getColumnCount();

for(int i = 1; i <= numberOfColumns; i++)

{

System.out.printf("%-20s\t", metaData.getColumnName(i));

}

System.out.println();

for(int i = 1; i <= numberOfColumns; i++)

{

System.out.printf("%-20s\t", "-----------------");

}

System.out.println();

while(resultSet.next())

{

for(int i = 1; i <= numberOfColumns; i++)

{

System.out.printf("%-20s\t", resultSet.getObject(i));

}

System.out.println();

}

statement.close();

connection.close();*/

}

}

DisplayQueryResult class

import java.awt.EventQueue;

import java.sql.Connection;

import java.sql.SQLException;

import java.sql.Statement;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JTable;

import javax.swing.JTextArea;

import javax.swing.JButton;

import java.awt.event.ActionListener;

import java.awt.event.ActionEvent;

import javax.swing.JTextField;

import javax.swing.JPasswordField;

import java.awt.Color;

import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.swing.JComboBox;

import javax.swing.DefaultComboBoxModel;

public class DisplayQueryResult

{

private final String DEFAULT_QUERY = "select * from bikes";

private ResultSetTableModel tableModel;

private JTextArea queryArea;

private JFrame frame;

private JLabel lbl_JDBC_Driver;

private JLabel datalbl_base_URL;

private JLabel lblUsername;

private JLabel lblPassword;

private JTextField Username;

private JPasswordField passwordField;

private JLabel connection_Tester;

private JComboBox Database_URL;

public Connection connection;

private Statement statement;

private String query;

private static ResultSetTableModel pleaseWork;

boolean connectedToDatabase = false;

/**

* Launch the application.

*/

public static void main(String[] args)

{

EventQueue.invokeLater(new Runnable()

{

public void run()

{

try

{

DisplayQueryResult window = new DisplayQueryResult();

window.frame.setVisible(true);

}

catch (Exception e)

{

e.printStackTrace();

}

}

});

}

/**

* Create the application.

*/

public DisplayQueryResult() {

initialize();

}

/**

* Initialize the contents of the frame.

*/

private void initialize()

{

try

{

frame = new JFrame();

frame.setBounds(100, 100, 745, 539);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.getContentPane().setLayout(null);

queryArea = new JTextArea();

queryArea.setWrapStyleWord(true);

queryArea.setBounds(427, 39, 265, 144);

frame.getContentPane().add(queryArea);

JTable resultTable = new JTable();

resultTable.setBounds(12, 255, 703, 224);

frame.getContentPane().add(resultTable);

JButton execute_btn = new JButton("Execute SQL Command");

execute_btn.addActionListener(new ActionListener()

{

public void actionPerformed(ActionEvent event)

{

query = queryArea.getText();

try {

pleaseWork.Run_Query();

}

catch (SQLException e)

{

// TODO Auto-generated catch block

e.printStackTrace();

}

}

});

execute_btn.setBounds(506, 219, 186, 25);

frame.getContentPane().add(execute_btn);

lbl_JDBC_Driver = new JLabel("JDBC Driver ");

lbl_JDBC_Driver.setBounds(12, 42, 124, 16);

frame.getContentPane().add(lbl_JDBC_Driver);

datalbl_base_URL = new JLabel("Database URL");

datalbl_base_URL.setBounds(12, 71, 88, 16);

frame.getContentPane().add(datalbl_base_URL);

lblUsername = new JLabel("Username");

lblUsername.setBounds(12, 100, 88, 16);

frame.getContentPane().add(lblUsername);

lblPassword = new JLabel("Password");

lblPassword.setBounds(12, 129, 56, 16);

frame.getContentPane().add(lblPassword);

Username = new JTextField();

Username.setBounds(141, 97, 116, 22);

frame.getContentPane().add(Username);

Username.setColumns(10);

passwordField = new JPasswordField();

passwordField.setBounds(141, 126, 116, 22);

frame.getContentPane().add(passwordField);

JButton btnClearCommand = new JButton("Clear Command");

btnClearCommand.setBounds(369, 219, 125, 25);

frame.getContentPane().add(btnClearCommand);

JButton btnConnectToDriver = new JButton("Connect to Driver");

btnConnectToDriver.addActionListener(new ActionListener()

{

public void actionPerformed(ActionEvent arg0)

{

String username = Username.getText().toString();

char[] password = passwordField.getPassword();

String pass = new String(password);

String URL = null;

if(Database_URL.getSelectedIndex() == 0)

{

URL = String.valueOf(Database_URL.getSelectedItem());

}

else if(Database_URL.getSelectedIndex() == 1)

{

URL = String.valueOf(Database_URL.getSelectedItem());

}

else

{

URL = String.valueOf(Database_URL.getSelectedItem());

}

try

{

connection = connection(username, pass, URL);

//tableModel = new ResultSetTableModel();

}

catch(SQLException | ClassNotFoundException e)

{

e.printStackTrace();

}

if(connection != null)

{

connection_Tester.setText("Connected to Database " + URL);

}

}

});

btnConnectToDriver.setBounds(192, 219, 165, 25);

frame.getContentPane().add(btnConnectToDriver);

connection_Tester = new JLabel("Not connected to Database");

connection_Tester.setForeground(Color.RED);

connection_Tester.setBackground(new Color(0, 0, 0));

connection_Tester.setBounds(12, 161, 165, 16);

frame.getContentPane().add(connection_Tester);

Database_URL = new JComboBox();

Database_URL.setModel(new DefaultComboBoxModel(new String[] {"jdbc:mysql://localhost:3310/project3", "jdbc:mysql://localhost:3310/bikedb", "jdbc:mysql://localhost:3310/test"}));

Database_URL.setBounds(141, 71, 245, 22);

frame.getContentPane().add(Database_URL);

JComboBox JDBC_Driver = new JComboBox();

JDBC_Driver.setModel(new DefaultComboBoxModel(new String[] {"com.mysql.jdbc.Driver", "oracle.jdbc.driver.OracleDriver", "com.ibm.db2.jdbc.netDB2Driver", "com.jdbc.odbc.jdbc.OdbcDriver"}));

JDBC_Driver.setBounds(141, 39, 245, 22);

frame.getContentPane().add(JDBC_Driver);

}

finally

{

}

}

public Connection connection(String username, String password, String url)throws SQLException, ClassNotFoundException

{

boolean connectedToDatabase = false;

MysqlDataSource dataSource = null;

try

{

dataSource = new MysqlDataSource();

dataSource.setURL(url);

dataSource.setUser(username);

dataSource.setPassword(password);

connection = dataSource.getConnection();

System.out.print(connection);

ResultSetTableModel test = new ResultSetTableModel();

test.setConnection(connection);

connectedToDatabase = true;

return connection;

}

catch(SQLException e)

{

e.printStackTrace();

System.exit(1);

return null;

}

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Computer Aided Database Design

Authors: Antonio Albano, Valeria De Antonellis, A. Di Leva

1st Edition

0444877355, 978-0444877352

Students also viewed these Databases questions