Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

why exception ?  RSS feed

 
jrookie
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello
i get java.lang.ArrayIndexOutOfBoundsException
and i don't understand it why...
code open only one table (6 rows and 6 columns), all anothers show this exception.
structure of all tables equals. Different rows only
i put in code strings
System.out.println("====Element=== "+ud.columns[2].elementAt(0));
System.out.println("====Element=== "+ud.columns[3].elementAt(0));
for test. they showing right data...
so why tables show this exception ?
code is
public void setDataModel(UserData ud)
{
this.ud=ud;
this.cols=ud.cols;
this.rows=ud.columns[0].size();
this.columnnames=ud.columnnames;
this.columntypes=ud.columntypes;
this.usertable=null;
this.usertable=new Object[cols][rows];
System.out.println("cols="+cols+" rows="+rows);
for (i=0;i<rows;i++)
{
for (j=0;j<cols;j++)
{
System.out.println("====Element=== "+ud.columns[2].elementAt(0));
System.out.println("====Element=== "+ud.columns[3].elementAt(0));
System.out.println("i="+i+" j="+j);
usertable[i][j]=ud.columns[j].elementAt(i);// <- here is exception
System.out.println("element="+usertable[i][j]);
}
}
}
public int getColumnCount()
{
return (cols);
}

public String getColumnName(int cols)
{
return (columnnames[cols]+"\n"+columntypes[cols]);
}

public int getRowCount()
{
return (rows);
}

public Object getValueAt(int rows,int cols)
{
return (usertable[rows][cols]);
}
 
Chantal Ackermann
Ranch Hand
Posts: 508
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
either some of your rows do not contain all 6 values or the value of cols is not equal to columns.length.
IMHO I find it bad oo-programming to have variables called "cols" and "columns" in your UserData object. UserData should not be concerned about columns - why should it? columns are very specific to tables and their models. the function of a table model _is_ to map some (any) kind of data to a table structure.
my recommendation:
your UserData object should have getter methods like:
getUserName(int id) or getUserNames(), getUserNumber(), getUserIDs(), or whatever your different columns represent. also a method getUserCount() which would be used by the model to compute the row count (well, getUserCount() will most likely return the exact value for getRowCount(), but the model will be the only one to know of it). at the moment, your UserData object has more of a table model than the model you're builiding from it. if this is the case you might want to transform your UserData object to a TableModel (by extending AbstractTableModel). But this makes only sense if the UserData object "is a" TableModel and nothing else.
From what I know, in Object Oriented Programming, it is "bad" coding to access variables in an object of another type directly. Rather use get and set-methods. this gives you more flexibility later on.
sorry for being this general - but I think there is more to java than just knowing the syntax.
cheers
Chantal
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"jrookie" -

Welcome to the JavaRanch! Please adjust your displayed name to meet the
JavaRanch Naming Policy.
You can change it here.

Thanks! and welcome to the JavaRanch!
 
jrookie
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this topic is closed
problem is fixed
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!