Results 1 to 7 of 7
  1. #1
    Paul_White is offline Member
    Join Date
    Apr 2011
    Posts
    15
    Rep Power
    0

    Question 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 06:13 PM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Paul_White View Post
    The vectors are declared as:
    Java Code:
      public static final ComboBoxModel vtable = null;
      public static final ComboBoxModel vtype = null;
    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.

  3. #3
    RichersooN is offline Senior Member
    Join Date
    Jan 2011
    Location
    Rizal Province, Philippiines
    Posts
    167
    Rep Power
    0

    Default

    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();

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by RichersooN View Post
    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();
    I don't know about this. You can only get the data after it has been retrieved so this may not work if the database hasn't yet been queried. Better to get the data when you know it's available and when you need it.

  5. #5
    RichersooN is offline Senior Member
    Join Date
    Jan 2011
    Location
    Rizal Province, Philippiines
    Posts
    167
    Rep Power
    0

    Default

    Java Code:
     public static final ComboBoxModel vtable = null;
      public static final ComboBoxModel vtype = null;
    by the way is the ComboBoxModel is a vector?

  6. #6
    Paul_White is offline Member
    Join Date
    Apr 2011
    Posts
    15
    Rep Power
    0

    Default 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

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. How to save the results for checkbox
    By pink123 in forum AWT / Swing
    Replies: 1
    Last Post: 03-07-2011, 11:19 PM
  2. wrong results with recursion
    By sara12345 in forum New To Java
    Replies: 0
    Last Post: 04-12-2010, 05:49 PM
  3. Sometimes get the right results sometimes dont
    By Battlefeldt in forum New To Java
    Replies: 0
    Last Post: 12-18-2009, 02:03 AM
  4. Vector<vector> loop thru
    By ocean in forum New To Java
    Replies: 11
    Last Post: 11-21-2009, 03:17 PM
  5. help with protected method in vector class
    By katie in forum Advanced Java
    Replies: 1
    Last Post: 08-06-2007, 11:59 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
  •