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.
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).
精彩评论