views:

111

answers:

1

Hey there. been a busy debugger today. ill give ths short version.

ive made an array list that takes names from a database. then i put the contents of the arraylist into an array of strings. now i want too display the arrays contents in a JList.

the weird thing is it was working earlier. and ive two methods. ones just a little practice too make sure i was adding to the Jlist correctly. so heres the key codes.

this is the layout of my code.

variables constructor methods

in my variables i have these 3 defined

String[] contactListNames = new String[5];
ArrayList<String> rowNames = new ArrayList<String>();
JList contactList = new JList(contactListNames);

simple enough.

in my constructor i have them again.

contactListNames = new String[5];
    contactList = new JList(contactListNames);
//i dont have the array list defined though.

 printSqlDetails();
// the prinSqldetails was too make sure that the connectionw as alright. and its working fine.

     fillContactList();
 // this is the one thats causing me grief. its where all the work happens.

   // fillContactListTest();
// this was the tester that makes sure its adding to the list alright.

heres the code for fillContactListTest()

  public void fillContactListTest()
 {
  for(int i = 0;i<3;i++)
  {
   try
   {
    String contact;
    System.out.println(" please fill the list at index "+ i);
    Scanner in = new Scanner(System.in);
    contact = in.next();
    contactListNames[i] = contact;
    in.nextLine();
   }
   catch(Exception e)
   {
 e.printStackTrace();   
   }
  }
 }

heres the main one thats supposed too work.

public void fillContactList()
       {
           int i =0;
           createConnection();
           ArrayList<String> rowNames = new ArrayList<String>();
           try
           {
               Statement stmt = conn.createStatement();
               ResultSet namesList = stmt.executeQuery("SELECT name FROM Users");
               try
               {

                   while (namesList.next())
                   {
                       rowNames.add(namesList.getString(1));

                       contactListNames =(String[])rowNames.toArray(new String[rowNames.size()]);

                       // this used to print out contents of array list
                      // System.out.println("" + rowNames);

                       while(i<contactListNames.length)
                       {
                           System.out.println(" " + contactListNames[i]);
                           i++;
                       }
                   }
               }
               catch(SQLException q)
               {
                   q.printStackTrace();
               }
               conn.commit();
               stmt.close();
               conn.close();
           }
           catch(SQLException e)
           {
               e.printStackTrace();
           }
       }

i really need help here. im at my wits end. i just cant see why the first method would add to the JList no problem. but the second one wont.

both the contactListNames array and array list can print fine and have the names in them. but i must be transfering them too the jlist wrong. please help

p.s im aware this is long. but trust me its the short version.

A: 

You need to call

contactList = new JList(contactListNames);

after

fillContactList();

Because inside the fillContactList() you're replacing the contactListNames reference instead of filling it.

That said and unrelated to the described problem, there are more odds in the code, but you'll find them out sooner or later I think :) (the code is leaking JDBC connections, the code flow is inefficient, the code is not arranged in an intuitive OO way, etc.. but, again, this has all nothing to do with the described problem).

BalusC
didnt change anything. sorry
OVERTONE
Then the problem lies somewhere else than in the as far provided information. Try to [ask your question the smart way](http://catb.org/esr/faqs/smart-questions.html) and provide an [SSCCE](http://sscce.org) (so that we can just copy'n'paste the code **unchanged**, compile and exectute it to see the same problem). Please also work on your attitude, I am not sure if it is the language barrier, but you sound *very* rude in your last sentence. Certainly I am eager to help, but you have first to help us to help you.
BalusC
@BalusCI apologize. reading back on it now it was quite a bad comment for me to make. it wasnt meant too come across as rude but regardless it was a BIG mistake on my part. Im a beginner with java but not with manners. i really do appreaciate the help and hope you accept my sincere apologies. I'm eager to learn and in no way want to seem disrespectfull.p.s SSCCE is a hard topic to research and i want even aware of it until now. ill research ti some more. thanks you.
OVERTONE
No problem. With regard to the SSCCE: just create a class with a `main()` method which demonstrates/reproduces exactly the same problem in a single run and copypaste the entire source.
BalusC