Results 1 to 5 of 5
- 10-23-2009, 04:36 AM #1
Member
- Join Date
- Dec 2008
- Posts
- 50
- Rep Power
- 0
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
Here is the method that is being calledJava 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(); } }
Does anyone have any ideas why this error is being thrown or at least some common reasons?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; }
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.
- 10-23-2009, 08:26 AM #2
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
The exception stack trace tells you the exact line number where the exception was thrown on. Post that line in your code.
- 10-23-2009, 09:59 AM #3
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:
- 10-23-2009, 01:34 PM #4
Member
- Join Date
- Dec 2008
- Posts
- 50
- Rep Power
- 0
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...)
Lastly, note taken on the Prepared Statements, I'll change them.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)
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.
- 10-23-2009, 01:40 PM #5
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
Similar Threads
-
NullPointerException
By tommyyyy in forum New To JavaReplies: 9Last Post: 03-26-2009, 10:51 PM -
NullPointerException I NEED HELP
By mayhewj7 in forum New To JavaReplies: 2Last Post: 02-13-2009, 08:03 AM -
I get a NullPointerException and don't know why
By hendrix79 in forum New To JavaReplies: 9Last Post: 12-14-2008, 06:18 AM -
NullPointerException
By adeeb in forum AWT / SwingReplies: 3Last Post: 06-11-2008, 08:42 AM -
NullPointerException
By mensa in forum Java 2DReplies: 5Last Post: 05-03-2008, 11:19 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks