I am trying to access a database using a properties file but can not
get my program to run. Error message: Fata exception error. Program
will exit.
java.lang.NullPointerException
at com.cbksec.cogs.fxfeed.COGSFXFeed.<init>(COGSFXFeed.java:124)
at com.cbksec.cogs.fxfeed.COGSFXFeed.main(COGSFXFeed.java:144)
11:06:14 DEBUG [logger initialized successfully]
Exception in thread "main"
package com.cbksec.cogs.fxfeed;
import java.awt.*;
import java.io.*;
import java.sql.*;
import java.util.*;
import javax.swing.*;
import java.sql.Connection;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
public class COGSFXFeed extends JFrame {
// the central logger instance
private static Logger logger = Logger.getLogger(COGSFXFeed.class);
// holds the properties file values
static final String FILE = "COGSFXFeed.properties";
// declare Connection and Statement for accessing
// and querying database
private Connection connection;
private Statement statement;
// constructor connects to database, queries database, processes
// results and displays results in window
public COGSFXFeed() {
super("Currency Table of CogsDB Database");
// connect to database CogsDB and query database
try {
// initializing the logger
PropertyConfigurator.configure("COGSFXFeed.Properties");
PropertyConfigurator.configure("Logger.Properties");
logger.debug("logger initialized successfully");
Properties config = LoadProperties();
// load database driver class
Class.forName(config.getProperty("JdbcDriver"));
String url =
config.getProperty("DatabaseURL")
+ "://"
+ config.getProperty("DatabaseServer")
+ ":"
+ config.getProperty("DatabasePort")
+ "/"
+ config.getProperty("DatabaseName");
logger.debug("successfully loaded database driver");
// establish connection to database
connection =
DriverManager.getConnection(
url,
config.getProperty("DatabaseUser"),
config.getProperty("DatabasePassword"));
logger.debug("successfully established
connection to database");
// create Statement for querying database
statement = connection.createStatement();
// query database
ResultSet resultSet =
statement.executeQuery(config.getProperty("CurrencySelectStatement"));
// process query results
StringBuffer results = new StringBuffer();
ResultSetMetaData metaData = resultSet.getMetaData();
int numberOfColumns = metaData.getColumnCount();
for (int i = 1; i <= numberOfColumns; i++)
results.append(metaData.getColumnName(i) + "\t");
results.append("\n");
while (resultSet.next()) {
for (int i = 1; i <= numberOfColumns; i++)
results.append(resultSet.getObject(i) + "\t");
results.append("\n");
}
// set up GUI and display window
JTextArea textArea = new JTextArea(results.toString());
Container container = getContentPane();
container.add(new JScrollPane(textArea));
setSize(300, 100); // set window size
setVisible(true); // display window
} // end try
// detect problems interacting with the database
catch (SQLException sqlException) {
JOptionPane.showMessageDialog(
null,
sqlException.getMessage(),
"Database Error",
JOptionPane.ERROR_MESSAGE);
System.exit(1);
}
// detect problems loading database driver
catch (ClassNotFoundException classNotFound) {
JOptionPane.showMessageDialog(
null,
classNotFound.getMessage(),
"Driver Not Found",
JOptionPane.ERROR_MESSAGE);
System.exit(1);
}
// ensure statement and connection are closed properly
finally {
try {
statement.close();
connection.close();
}
// handle exceptions closing statement and connection
catch (SQLException sqlException) {
JOptionPane.showMessageDialog(
null,
sqlException.getMessage(),
"Database Error",
JOptionPane.ERROR_MESSAGE);
System.exit(1);
}
}
} // end COGSFXFeed constructor
// launch the application
public static void main(String args[]) {
COGSFXFeed window = new COGSFXFeed();
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private Properties LoadProperties() {
InputStream l_InputStream = null;
Properties l_Properties = null;
try {
l_InputStream =
getClass().getClassLoader().getResourceAsStream(FILE);
l_Properties.load(l_InputStream);
} catch (Exception e) {
} finally {
try {
if (l_InputStream != null)
l_InputStream.close();
} catch (Exception e) {
}
}
return l_Properties;
}
} // end class COGSFXFeed |