• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

problem with returning values to Jcombo box origin see code

 
Lisa Foster
Ranch Hand
Posts: 116
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to build my combo box from getting values from db.db for origin values and I am using set to get unique values any suggestions of what I am doing wrong?
I am trying to return values to client to populate Combo box with origin I use set to keep values unique see code I get database exception.
public String[] getComboOriginValues(int fieldNum) throws DatabaseException {
invariant();
Set origin = null;
try {
seek(1); String [] values = null;int r;
for (r = 1; r <= recordCount; r++)
{
values = readRecord(); origin.add(values[fieldNum]);
}
}

catch (IOException e)
{
throw new DatabaseException(UNEXPECTED + e);
}

return (String[])origin.toArray();
//return (String[])origin.toArray(new String(0)); //this dont work
}
Thanks Lisa
 
Lisa Foster
Ranch Hand
Posts: 116
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am getting the exception at this point in my forloop
public String[] getComboOriginValues(int fieldNum) throws DatabaseException {
invariant();
Set origin = null;
try {
seek(1); String [] values = null; int r;
for (r = 1; r <= recordCount; r++)
{
values = readRecord();
//My program throws exception right before origin.add
origin.add(values[fieldNum]);
}
}
catch (IOException e)
{
throw new DatabaseException(UNEXPECTED + e);
}
return (String[])origin.toArray();
//return (String[])origin.toArray(new String(0)); //this dont work
}
thanks Lisa
 
Paul Smiley
Ranch Hand
Posts: 244
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't add to a set until it has a location - you have to do new Set() first. I used a TreeSet to sort them in order.
 
Lisa Foster
Ranch Hand
Posts: 116
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe Paul I tryed to new a set and said cant new an interface???
Im not sure thanks Paul
 
Lisa Foster
Ranch Hand
Posts: 116
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Paul TreeSort is magic thanks Alot
Lisa
 
Rahul Rathore
Ranch Hand
Posts: 324
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lisa
Inadvertently I had said return (String[])values.toArray(new String(0)) which is wrong. Actually what I had meant to write was (String[])values.toArray(new String[0]) i.e. [] not (). Just correct that.
Also, getComboOriginValues() is not a happy name for a data method(). Give it a generic name.

[This message has been edited by Rahul Rathore (edited March 11, 2001).]
 
Nito Raj
Greenhorn
Posts: 5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does anyone have the problem like this?
using
String [] airPortCode1 = data.getDistinctValues(2);
origin = new JComboBox(airPortCode1);
String [] airPortCode2 = data.getDistinctValues(3);
destination = new JComboBox(airPortCode2);
then nothing is populated in the destination ComboBox
only either origin or destination is populated not the both.
any comment or suggestion would be appreciated!
Thanks,
Nito
 
Paul Smiley
Ranch Hand
Posts: 244
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know - I filled mine with vectors and they worked fine. Where are the combo boxes declared? Initialized to null or are they new'ed at that point? Have you tried to work with the model?
 
Rudy Yeung
Ranch Hand
Posts: 183
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nito,
Simply enough, have you debugged what is inside airportCode1 and airportCode2?
Rudy
 
Paul Smiley
Ranch Hand
Posts: 244
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nito,
When you are passing the 2 and 3, are those indices of the array that is returned from readRecord? Shouldn't they be 1 and 2 since they are zero based? Forgive me if I'm wrong, just trying to throw out suggestions...
 
Nito Raj
Greenhorn
Posts: 5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I figured it out guys. Guess what, I need to move the filepointer back to the original place.
Thank you Paul and Rudy
Nito
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic