Results 1 to 7 of 7
- 05-04-2011, 05:11 PM #1
Member
- Join Date
- Apr 2011
- Posts
- 15
- Rep Power
- 0
Vector results need to be available to another class
Hi,
Thanks to Furable and Richersoon my project has moved on nicely.
However I am stuck on what should be a simple fix for those in the know.
I have a class (DatabaseAcess) which accesses a mysql database and retrives a list of tables and a list of entry types from one of the tables. I am as recommended using a vector to collect the data from the query.
Within the same class I can populate my jcomboboxes with the results returned.
The vectors are declared as:
public static final ComboBoxModel vtable = null;
public static final ComboBoxModel vtype = null;
I have a separate class for my GUI and cannot get the values to use within this class.
I have tried all sorts but no success so far. Currently the line I am having problems with is:
tables = new JComboBox(MyProject.DatabaseAccess.vtable);
and I am getting the following error:
Exception in thread "main" java.lang.NullPointerException
at javax.swing.JComboBox.setModel(Unknown Source)
at javax.swing.JComboBox.<init>(Unknown Source)
at MyProject.GUI.main(WIP.java:143)
If I try system.out.println I get null
I have tried importing MyProject.* and Java.io.*
But (according to the IDE) neither of these are being used.
I am at a loss to know what the fix is. The nearest I have got is no errors and blank jcomboboxes.
Could someone please enlighten me?
Thank you very much.
Paul
:confused:Last edited by Paul_White; 05-04-2011 at 05:13 PM.
-
You shouldn't be using static variables.
I have tried all sorts but no success so far. Currently the line I am having problems with is:
tables = new JComboBox(MyProject.DatabaseAccess.vtable);
and I am getting the following error:
Exception in thread "main" java.lang.NullPointerException
So either the DatabaseAccess class is null or its vtable variable is null.
But regardless, your DatabaseAcess (should that be DatabaseAccess?) should only have instance variables. I suppose it should have a constructor to set things up and a separate public method, perhaps called doQuery(), that does the actual database query. It should have public getter methods to allow outside classes to get the data.
Then the GUI could have a DatabaseAccess variable and construct it. On whatever event desired (button push perhaps), your GUI would then call the DatabaseAccess variable's doQuery method, possibly in a background thread, and then after and only after the query has completed, the GUI would call the DatabaseAccess variable's getter methods to extract the data obtained.
- 05-04-2011, 05:47 PM #3
Senior Member
- Join Date
- Jan 2011
- Location
- Rizal Province, Philippiines
- Posts
- 167
- Rep Power
- 0
You should have public method that will return the vector in class1.
Then in class2 you should have a declare a vector variable.
Java Code:private Vector v2; v2 = class1.getVector1();
-
- 05-05-2011, 02:32 AM #5
Senior Member
- Join Date
- Jan 2011
- Location
- Rizal Province, Philippiines
- Posts
- 167
- Rep Power
- 0
by the way is the ComboBoxModel is a vector?Java Code:public static final ComboBoxModel vtable = null; public static final ComboBoxModel vtype = null;
- 05-07-2011, 07:33 PM #6
Member
- Join Date
- Apr 2011
- Posts
- 15
- Rep Power
- 0
All working thank you
Furable and Richersoon,
Thank you very much for pointing me in the right direction. It took me a little while to sort out all my mistakes this time.
Yes the SQL query is a vector as you suggested
I had made all sorts of mistakes which is why I was struggling.
FYI the code was running under
public static void main(String[] args) {
which I now understand was not helping at all. No wonder the best I could do was get null.
Anyway with the pointers you gave me I am now calling the separate classes in the correct order and getting the data passed between them.
Once again thank you very much for your help.
Paul
-
Similar Threads
-
How to save the results for checkbox
By pink123 in forum AWT / SwingReplies: 1Last Post: 03-07-2011, 10:19 PM -
wrong results with recursion
By sara12345 in forum New To JavaReplies: 0Last Post: 04-12-2010, 04:49 PM -
Sometimes get the right results sometimes dont
By Battlefeldt in forum New To JavaReplies: 0Last Post: 12-18-2009, 01:03 AM -
Vector<vector> loop thru
By ocean in forum New To JavaReplies: 11Last Post: 11-21-2009, 02:17 PM -
help with protected method in vector class
By katie in forum Advanced JavaReplies: 1Last Post: 08-06-2007, 10:59 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks