Results 1 to 5 of 5
  1. #1
    ribbs2521 is offline Member
    Join Date
    Dec 2008
    Posts
    50
    Rep Power
    0

    Default NullPointerException from createQuery()

    I've searched the internet for about two hours now and all I can figure out is that this has something to do with the table I'm trying to query from. I have two other queries that I call and they work fine. I read someone else's problem where it was stopping part way through the query which tells me it's something to do with the table.

    Here is the method calling the query
    Java Code:
    	private void setTableInfo(Object cbContents)
    	{
    		System.out.println("Start");
    		int j = 0;
    		DefaultTableModel model;
    		ResultSetMetaData rsmd;
    		ResultSet rs;
    
    		try
    		{
    			System.out.println("Trying for data by sending\nSELECT * FROM report_tables.idle_report WHERE family='"
    									+ cbContents + "'");
    			rs = myApplication.connection.createQuery("SELECT * FROM report_tables.idle_report WHERE family='"
    									+ cbContents + "'");
    			System.out.println("Getting MetaData");
    			rsmd = rs.getMetaData();
    			System.out.println("Got the RS and MetaData");
    			model = (DefaultTableModel)tblActiveList.getModel();
    			model.setColumnCount(rsmd.getColumnCount());
    			System.out.println("Set the Column count to " + rsmd.getColumnCount());
    		}
    		catch(Exception e)
    		{
    			e.printStackTrace();
    		}
    	}
    Here is the method that is being called
    Java Code:
    	public ResultSet createQuery(String query)
    	{
    		try
    		{
    			Statement stat4 = conn.createStatement();
    			result = stat4.executeQuery(query);
    		}
    		catch(SQLException e)
    		{
    			JOptionPane.showMessageDialog(null, "Error in createQuery " + e);
    		}
    		return result;
    	}
    Does anyone have any ideas why this error is being thrown or at least some common reasons?

    EDIT: I did some more testing and found maybe I was wrong on my assumption it was the table. I tried using a query that I know works because I used it in the program previously and it worked. Seems now that it may be the method that is calling the query. Still need help though.
    Last edited by ribbs2521; 10-23-2009 at 05:06 AM.

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    The exception stack trace tells you the exact line number where the exception was thrown on. Post that line in your code.

  3. #3
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    826
    Rep Power
    6

    Default

    One more suggestion here.For giving dynamic string values,don't go for Statement.Always use PreparedStatement.Because there is a chance that if u concatenate string and form a query lot of typo problems will come and lead to erroneous errors.
    Ramya:cool:

  4. #4
    ribbs2521 is offline Member
    Join Date
    Dec 2008
    Posts
    50
    Rep Power
    0

    Default

    Something else I noticed that was odd, when I referenced a protected variable from the other class it was null, but it shouldn't be I had set it previously but in this method it was null.

    Another thing, I'm not actually setting the connection in this class, the connection is set the class for the myApplication object, could that be causing my issue? So, basically, I create the connection in the main class and I try to reference that connection in the gui class in an effort to use one connection for all classes.

    Here is the stack trace (327 is where the rs = .... is and 18 is public class...)
    Java Code:
    java.lang.NullPointerException
            at Dashboard_GUI.setTableInfo(Dashboard_GUI.java:327)
            at Dashboard_GUI.access$100(Dashboard_GUI.java:18)
            at Dashboard_GUI$1.actionPerformed(Dashboard_GUI.java:285)
            at javax.swing.JComboBox.fireActionEvent(JComboBox.java:1242)
            at javax.swing.JComboBox.setSelectedItem(JComboBox.java:569)
            at javax.swing.JComboBox.setSelectedIndex(JComboBox.java:605)
            at javax.swing.plaf.basic.BasicComboPopup$Handler.mouseReleased(BasicCom
    boPopup.java:814)
            at java.awt.AWTEventMulticaster.mouseReleased(AWTEventMulticaster.java:2
    73)
            at java.awt.Component.processMouseEvent(Component.java:6041)
            at javax.swing.JComponent.processMouseEvent(JComponent.java:3265)
            at javax.swing.plaf.basic.BasicComboPopup$1.processMouseEvent(BasicCombo
    Popup.java:480)
            at java.awt.Component.processEvent(Component.java:5806)
            at java.awt.Container.processEvent(Container.java:2058)
            at java.awt.Component.dispatchEventImpl(Component.java:4413)
            at java.awt.Container.dispatchEventImpl(Container.java:2116)
            at java.awt.Component.dispatchEvent(Component.java:4243)
            at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4322
    )
            at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3986)
    
            at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3916)
            at java.awt.Container.dispatchEventImpl(Container.java:2102)
            at java.awt.Window.dispatchEventImpl(Window.java:2440)
            at java.awt.Component.dispatchEvent(Component.java:4243)
            at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
            at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThre
    ad.java:273)
            at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.
    java:183)
            at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThre
    ad.java:173)
            at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168)
    
            at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160)
    
            at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)
    Lastly, note taken on the Prepared Statements, I'll change them.

    EDIT: I tested it out and put the connection creating in the constructor of the main class and it's fine, now it requires two connections though (one for main class and one for my gui class). How can I use the connection created in the main class, I don't want two connections per application.
    Last edited by ribbs2521; 10-23-2009 at 01:41 PM.

  5. #5
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    So the offending line is rs = myApplication.connection.createQuery ...?
    That means either myApplication or myApplication.connection is null at that stage. You can easily find out which one it is by putting a println for each of those variables just before that line is reached.

Similar Threads

  1. NullPointerException
    By tommyyyy in forum New To Java
    Replies: 9
    Last Post: 03-26-2009, 10:51 PM
  2. NullPointerException I NEED HELP
    By mayhewj7 in forum New To Java
    Replies: 2
    Last Post: 02-13-2009, 08:03 AM
  3. I get a NullPointerException and don't know why
    By hendrix79 in forum New To Java
    Replies: 9
    Last Post: 12-14-2008, 06:18 AM
  4. NullPointerException
    By adeeb in forum AWT / Swing
    Replies: 3
    Last Post: 06-11-2008, 08:42 AM
  5. NullPointerException
    By mensa in forum Java 2D
    Replies: 5
    Last Post: 05-03-2008, 11:19 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •