Google Answers Logo
View Question
 
Q: Java programming ( No Answer,   1 Comment )
Question  
Subject: Java programming
Category: Computers > Programming
Asked by: math01-ga
List Price: $15.00
Posted: 08 Oct 2004 02:07 PDT
Expires: 14 Oct 2004 08:01 PDT
Question ID: 411943
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

Clarification of Question by math01-ga on 12 Oct 2004 04:25 PDT
Hi zigsoft-ga,

I have tried what you suggested but now get the error message:
"COGSFXFeed.java": exception java.lang.ClassNotFoundException has
already been caught at line 118, column 17

Clarification of Question by math01-ga on 14 Oct 2004 08:01 PDT
Hi zigsoft-ga,

I guess I will have to close this posting since we have no solution to my problem.

Thanks anyway
Answer  
There is no answer at this time.

Comments  
Subject: Re: Java programming
From: zigsoft-ga on 08 Oct 2004 09:31 PDT
 
It looks to me like because of the try block and the location of the
finally, the variable 'statement' may not have been initialized.

try this
                // ensure statement and connection are closed properly
                finally {

                        try {
                                if (statment != null) statement.close();
                                if (connection != null) connection.close();
                        }

                        // handle exceptions closing statement and connection
                        catch (SQLException sqlException) {
                                JOptionPane.showMessageDialog(
                                        null,
                                        sqlException.getMessage(),
                                        "Database Error",
                                        JOptionPane.ERROR_MESSAGE);

                                System.exit(1);
                        }
                }

You should also consider that you are not catching a certain type of
exception, therefore 'statement' is not initialize.  You should
include

...

                        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);
                }
<NEW>
                catch (Exception exc) {
                        JOptionPane.showMessageDialog(
                                null,
                                exc.getMessage(),
                                "Unknown Error",
                                JOptionPane.ERROR_MESSAGE);
                }

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy