Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at DatabaseTwo.manufacturerSearch(DatabaseTwo.java:81)
at SearchManufacturerScreen.searchForManufacturer(SearchManufacturerScreen.java:241)
at SearchManufacturerScreen.actionPerformed(SearchManufacturerScreen.java:192)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.java:6263)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
at java.awt.Component.processEvent(Component.java:6028)
at java.awt.Container.processEvent(Container.java:2041)
at java.awt.Component.dispatchEventImpl(Component.java:4630)
at java.awt.Container.dispatchEventImpl(Container.java:2099)
at java.awt.Component.dispatchEvent(Component.java:4460)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4574)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4238)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168)
at java.awt.Container.dispatchEventImpl(Container.java:2085)
at java.awt.Window.dispatchEventImpl(Window.java:2478)
at java.awt.Component.dispatchEvent(Component.java:4460)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)


I understand that the error code given above tells me where the null field exists however, what confuses me is that I am basically copying a program from another software engineer in an attempt to understand it better and replacing his variables with my own. The thing compiles and runs, I can enter data to the database but cannot retrieve the data.

Considering the code first specifies the DatabaseTwo class and the manufacturerSearch method i'll post the Database Two code.

I'm wondering if any of you can see the issue in this class or if it might tie to another one of the other 6 classes in this program.

For reference, line 81 is:

statement = connectionToFirePlaceDB.createStatement();

Thanks for any help
Silent

DatabaseTwo code as follows:

import java.sql.*;
public class DatabaseTwo
{

    static public Connection connectionToFirePlaceDB;
    
    static public Statement statement;
    
    static public int loadDriver()
    {
        try
        {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        }
        catch (ClassNotFoundException exception)
        {
            return (-1);
        }
        return (0);
    }
    
    static public int makeConnectionToFirePlaceDB()
    {
        try
        {
            connectionToFirePlaceDB = DriverManager.getConnection("jdbc:odbc:FirePlace");
        }
        catch (SQLException exception)
        {
            return (-1);
        }
        return (0);
    }
    
    static public int closeConnection()
    {
        try
        {
            connectionToFirePlaceDB.close();
        }
        catch (SQLException exception)
        {
            return (-1);
        }
        return (0);
    }
    
    static public int writeToFirePlaceDB(String midIn, String mNameIn, String mContactIn)
    {
        String SQLString;
        SQLString = "INSERT INTO MANUFACTURER VALUES ('" + midIn + "','" + mNameIn + "','" + mContactIn + "')";
        try
        {
            statement = connectionToFirePlaceDB.createStatement();
            statement.executeUpdate (SQLString);
        }
        catch (SQLException exception)
        {
            return (-1);
        }
        return (0);
    }
    
    static public ResultSet manufacturerSearch(String midIn, String mNameIn)
    {
        ResultSet rs = null;
        String SQLString = null;
        
        if (!(midIn.equals("")))
        {
            SQLString = "SELECT * FROM MANUFACTURER WHERE mid = '" + midIn +"'";
        }
        else
        if (!(mNameIn.equals("")))
        {
            SQLString = "SELECT * FROM MANUFACTURER WHERE mName = '" + mNameIn +"'";
        }
        
        try
        {
            statement = connectionToFirePlaceDB.createStatement();
            rs = statement.executeQuery(SQLString);
        }
        catch (SQLException exception)
        {
            return rs;
        }
        return rs;
    }
    
    static public int deleteManufacturer(String midIn)
    {
        int code;
        String SQLString = "DELETE * FROM MANUFACTURER WHERE mid = '" + midIn +"'";
        try
        {
            statement = connectionToFirePlaceDB.createStatement();
            code = statement.executeUpdate(SQLString);
        }
        catch (SQLException exception)
        {
            return (-1);
        }
        return(code);
    }
    
    static public int upDateManufacturer(String midIn, String mNameIn, String mContactIn)
    {
        int code;
        String SQLString = "UPDATE MANUFACTURER SET mid = '" + midIn + "', mName = '" + mNameIn + "', mContact = '" + 
                                                            mContactIn +"'";
        try
        {
            statement = connectionToFirePlaceDB.createStatement();
            code = statement.executeUpdate(SQLString);
        }
        catch (SQLException exception)
        {
            return (-1);
        }
        return (code);
    }
    
}


// End of class

Edited 5 Years Ago by SilentHunter13: n/a

The only thing on that line that could be null is connectionToFirePlaceDB (easily checked by printing this variable immediately before that line).
Which makes
connectionToFirePlaceDB = DriverManager.getConnection("jdbc:odbc:FirePlace");
the prime candidate for where the problem starts. Especially since the idiot who coded this chose to catch any errors from the getConnection and discard all the debugging info they contain.

The only thing on that line that could be null is connectionToFirePlaceDB (easily checked by printing this variable immediately before that line).
Which makes
connectionToFirePlaceDB = DriverManager.getConnection("jdbc:odbc:FirePlace");
the prime candidate for where the problem starts. Especially since the idiot who coded this chose to catch any errors from the getConnection and discard all the debugging info they contain.

Thanks, i'll look into it.

Edited 5 Years Ago by SilentHunter13: n/a

Thanks, i'll look into it.

I've been going over this problem with my class mates, still unable to get in contact with the software engineer who first programmed this, and we have not found a solution to the problem.

Any further help would be greatly appreciated.

Edited 5 Years Ago by SilentHunter13: n/a

Put an
exception.printStackTrace(); into EVERY catch clause, and see what exceptions are being thrown.

I've read around a bit before posting this (reading about exception.printStackTrace() as it has not been introduced to us before), and i'm not quite sure what you want me to do?

try
        {
            statement = connectionToFirePlaceDB.createStatement();
            rs = statement.executeQuery (SQLString);
        }
        catch (SQLException exception)
        {
            exception.printStackTrace();
            return rs;            
        }

?

Edited 5 Years Ago by SilentHunter13: n/a

Yes, exactly that.
When en exception is thrown you will catch it and its printStackTrace() method will immediately print all the details of the exception and where it happened.
When you are testing a program you should start with one of these in every catch block in the program. Otherwize you will be debugging with your eyes closed.

It doesn't seem to change anything. I still get the same error code as mensioned in the orriginal post. Nothing is printed from using that. I've read on other things that I might need to make it into a string before trying to produce it?

Sorry - must go now. I'll look again tomorrow.
You don't need to add anything to printStackTrace(), just make sure you have one in EVERY catch block.

This article has been dead for over six months. Start a new discussion instead.