Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 745751
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T09:37:10+00:00 2026-05-14T09:37:10+00:00

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

  • 0

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.

  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T09:37:11+00:00Added an answer on May 14, 2026 at 9:37 am

    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).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hey I have been battling with this problem for a while now. Perhaps there
Hey there, I've got a block of HTML that I'm going to be using
Hey I was wondering if there were any way to upload images in ASP?
Hey, is there any built in functions or something like that in php that
Hey, in the Programming Pearls book, there is a source code for setting, clearing
Hey, I've been developing an application in the windows console with Java, and want
Hey there, I'm writing a template container class and for the past few hours
Hey there. I have a c# application that parses txt files and imports the
Hey, I've been looking at the possibility of adding a scripting language into my
Hey guys, I have been programming with as3 for the past 4 months and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.