Tablemodel/vector problem (ArrayIndexOutOfBoundsException)

I have the following code that works fine in 1.4.2, but I have to run the same code using a 1.3.1 system.

DefaultTableModel dtm = new DefaultTableModel(new String[]{},1);
......
table = new JTable();
table.setModel(dtm);
......
public void setValues(String[] values)
    {
        for(int a = 0; a < values.length; a++)
        {
            if(a < table.getColumnCount())
                if(values[a] == null)
                {
                    dtm.setValueAt("", 0, a);
                }
                else
                {
                    table.getModel().setValueAt((String)values[a], 0, a);
                }
        }
    }

The error I get when running 1.3.1 is:

java.lang.ArrayIndexOutOfBoundsException: 0 >= 0
        at java.util.Vector.elementAt(Vector.java(Compiled Code))
        at javax.swing.table.DefaultTableModel.getValueAt(DefaultTableModel.java
:668)

Any help you can give me would be appreciated.
yeggstryAsked:
Who is Participating?
 
zzynxConnect With a Mentor Software engineerCommented:
Well, if you say that this

 table.getModel().setValueAt((String)values[a], 0, a);

is the line causing the error.
It simply means that the model of table (dtm) is empty when you make this call.
You ask for the first item (index 0) while it is empty.

Sure you fill up dtm?
0
 
zzynxSoftware engineerCommented:
A longer stack trace would help to determine where in *your* code the exception occurs.
0
 
Giant2Commented:
From the little stacktrace you post I see that when access to a Vector at position 0, this vector is empty.
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

 
TimYatesCommented:
> The error I get when running 1.3.1 is:

Do you get any further error?

It looks like it's trying to call getValueAt on a row that doesn't exist...

I think we may need to see more code...

Or...you could maybe use this class instead of DefaultTableModel: (I haven't tested this code at all) :-(

--------------

public class SaferDefaultTableModel extends DefaultTableModel
{
    public SaferDefaultTableModel() { super() ; }
    public SaferDefaultTableModel( int rowCount, int columnCount ) { super( rowCount, columnCount ) ; }
    public SaferDefaultTableModel( Object[][] data, Object[] columnNames ) { super( data, columnNames ) ; }
    public SaferDefaultTableModel( Object[] columnNames, int rowCount ) { super( columnNames, rowCount ) ; }
    public SaferDefaultTableModel( Vector columnNames, int rowCount ) { super( columnNames, rowCount ) ; }
    public SaferDefaultTableModel( Vector data, Vector columnNames ) { super( data, columnNames ) ; }

    public Object getValueAt( int row, int column )
    {
        if( row < dataVector.size() && column < ((Vector)dataVector.elementAt( row )).size() )
            return super.getValueAt( row, column ) ;
        return null ;
    }
}
0
 
WebstormCommented:
Hi yeggstry,

>>        at javax.swing.table.DefaultTableModel.getValueAt(DefaultTableModel.java:668)
This line indicate that the exception was not catched on line 668 in the file 'DefaultTableModel.java'
The nexts lines should indicates where the error occur in your source code.

0
 
yeggstryAuthor Commented:
The line I am getting the error on is at the "else" statement i.e.

else
                {
//THE LINE BELOW IS CAUSING THE ERROR
                    table.getModel().setValueAt((String)values[a], 0, a);
                }


It would make sense if it was not working in both, but it works fine in 1.4.1 but not 1.3.1.

Sorry but I cant give you any more of the stacktrace, but I know that it is this line that is causing the error (I have debugged it etc.)
0
 
zzynxSoftware engineerCommented:
>> The nexts lines should indicates where the error occur in your source code.
I expect it to be in a getValueAt() function called on dtm.
So, not in the source code snippet you posted.
0
 
zzynxSoftware engineerCommented:
>> Sorry but I cant give you any more of the stacktrace
Why not?
0
 
WebstormConnect With a Mentor Commented:

Try this code in your loop:

            if(a < table.getColumnCount())
            {  // <---
                if(values[a] == null)
                {
                    dtm.setValueAt("", 0, a);
                }
                else
                {
                    table.getModel().setValueAt((String)values[a], 0, a);
                }
            }  // <---
0
 
WebstormCommented:
I think the problem was from the else statement which was interpreted as the else for the if(a < table.getColumnCount()) statement. So setValueAt was called on a bad column index value.
0
 
yeggstryAuthor Commented:
ok, I added debug in as follows into the else statement:

                else
                {
                    System.out.println("added-->  " + values[a]);
                    System.out.println("" + table.getModel().getRowCount());
                    System.out.println("" + table.getModel().getColumnCount());
                    table.getModel().setValueAt((String)values[a], 0, a);
                }

and get the following output

added-->  Project Channel
1
1
java.lang.ArrayIndexOutOfBoundsException: 0 >= 0
        at java.util.Vector.setElementAt(Vector.java:499)
        at javax.swing.table.DefaultTableModel.setValueAt(DefaultTableModel.java
:684)

so the dtm has enough space for me to put this string in. As I said, the EXACT SAME CODE works in 1.4.1
0
 
zzynxSoftware engineerCommented:
It's indeed better to add those brackets there, but I doubt if that is the main problem.
0
 
WebstormCommented:
Have you tried my suggestion ?

If yes, then add another debug ouput after you call the setValueAt() method :

                else
                {
                    System.out.println("column =  " + a);
                    System.out.println("added-->  " + values[a]);
                    System.out.println("" + table.getModel().getRowCount());
                    System.out.println("" + table.getModel().getColumnCount());
                    table.getModel().setValueAt((String)values[a], 0, a);
                    System.out.println("setValue() OK");
                }
0
 
TimYatesCommented:
can you post all your class code?
0
 
WebstormCommented:
>> It's indeed better to add those brackets there, but I doubt if that is the main problem.
{ } are needed here, because their interpretation may vary with the java compiler used.
0
 
zzynxSoftware engineerCommented:
I rephrase:
It's indeed needed to add those brackets there, ... but I doubt if that is the main problem
;°)
0
 
yeggstryAuthor Commented:
>> can you post all your class code?

No i cannot, this is for the company I work for.
Thanks for the brackets Webstorm, I'll go slap the person that made this code (I have been given the task of figuring out why this isnt working in 1.3.1 and am stumped - google search returns very little).

i have added the column debug line as suggested and have now got the following output:

column =  0
added-->  Project Channel
1
1
java.lang.ArrayIndexOutOfBoundsException: 0 >= 0
        at java.util.Vector.setElementAt(Vector.java:499)
        at javax.swing.table.DefaultTableModel.setValueAt(DefaultTableModel.java
:684)

so from what I understand, it is trying to enter "Project Channel" into (0,0) of the model that is 1x1 large. Still dont understand the problem :/
0
 
WebstormCommented:
ok, let's doubt about it until we get yeggstry's anwer :-)
0
 
zzynxSoftware engineerCommented:
Doubt has become certainty ;°))
0
 
yeggstryAuthor Commented:
it isnt the main problem, I added the brackets in and still gave that error, tho I would personally put them in (as I said, I slapped the guy who wrote this code, dont worry, he leaves 2mw ;))
0
 
Giant2Commented:
I believe you have a setValue over your TableModel.
Try to debug this.
and print the size of the Vector you are inserting in.
0
 
yeggstryAuthor Commented:
erm... I dont fully understand what you mean Giant2
0
 
zzynxSoftware engineerCommented:
>> Try to debug this.
That's what I also already thought about.
Try to step into that setValue() stuff.
See if the model (Vector) you are adding to is the model you think you are adding to...
0
 
zzynxSoftware engineerCommented:
Although your extra println's output

      1
      1

you get:

java.lang.ArrayIndexOutOfBoundsException: 0 >= 0
        at java.util.Vector.setElementAt(Vector.java:499)

Which indicates an empty Vector
0
 
Giant2Commented:
you did:
>table.getModel().setValueAt((String)values[a], 0, a);
try before this:
System.out.println("Column presents:"+table.getModel().getColumnCount());
System.out.println("Row present:"+table.getModel().getRowCount());

In this manner you can know how big is the data representation of the Model. So if the position 0 is empty or not.
0
 
zzynxSoftware engineerCommented:
Giant, he already did
0
 
yeggstryAuthor Commented:
I had already done that Giant2:

Column presents:1
Row present:1

is what I get anyway

if I have an empty Vector zzynx, how do I solve it?
the dtm I am using is part of the Java API itself so I dont see how I can step into that code...
0
 
zzynxSoftware engineerCommented:
The code for DefaultTableModel is available in the JDK, so you can step into it.
0
 
Giant2Commented:
Ok.
You did:
>table.getModel().setValueAt((String)values[a], 0, a);
with a=1 (if I read well the previous post).
the size of the model is 1 row and 1 column and with the code you are inserting value to position 0,1.
This position doesn't exist. The position available (because the size is 1x1) is only 0,0.
This is the error, I believe.



0
 
yeggstryAuthor Commented:
only problem is that I am creating the files using Netbeans, but am compiling in command prompt (because of the setup of the system). Also since its a runtime error I cant step into it there.

oh, one thing I forgot to mention (dont know if it matters or not) is that something is assigned to the dtm before its linked to the table....

dtm.setColumnIdentifiers(columnNames);
       
table = new JTable();
table.setModel(dtm);

Sorry if I'm being a little dumb here but I simply dont understand how it can be working in one version and not in another when the methods are the same (unless ofc Sun buggered up/changed some of the code somewhere down the line).
0
 
yeggstryAuthor Commented:
no Giant2, as I said before...

>so from what I understand, it is trying to enter "Project Channel" into (0,0) of the model that is 1x1 large. Still dont understand the problem :/

which would make sense.
0
 
TimYatesCommented:
Are you;

a)  compiling it under 1.4, and running it under 1.3?  Or
b)  compiling it under 1.3 and running it under 1.3?

If you are doing a), can you try b)

> unless ofc Sun buggered up/changed some of the code somewhere down the line

They change loads of code between versions... :-/  But not really on something as basic as Vector...
0
 
zzynxSoftware engineerCommented:
----- Giant

>> with a=1 (if I read well the previous post).

No, you didn't ;°)
>> System.out.println("column =  " + a);
with as output:
>>column =  0
>>added-->  Project Channel
>>1
>>1

------- yeggstry

>> something is assigned to the dtm before its linked to the table
That's no problem

>>Sorry if I'm being a little dumb here
You are not

>> I am creating the files using Netbeans, but am compiling in command prompt
A pity you can't run/debug it in netBeans...

Out of ideas. Sorry (hands and shoulders up)
0
 
Giant2Commented:
>>No, you didn't ;°)

Whops! Now I see correctly (today was an hard day), sorry.
0
 
yeggstryAuthor Commented:
I checked and compiled in both 1.3 and 1.4. No change.

One problem I have is that I am using websphere :/ (again, something I have to use as part of the system).
0
 
yeggstryAuthor Commented:
ok, well this is VERY messed up.

I found a solution: I have to set the number of columns. A bit odd, but there you go. This now works in 1.3.1 and 1.4.1:

        dtm.setColumnIdentifiers(columnNames);
        dtm.setColumnCount(columns);
       
        table = new JTable();
        table.setModel(dtm);

where columns is the number of columns that I want to set the dtm to.

Not sure what to do about the points for this question, any suggestions?
0
 
zzynxSoftware engineerCommented:
- If you want to show your appreciation to all of us trying to help you, you can accept a comment of each of us.

- If you want to delete this question, post a zero-point question in http://www.experts-exchange.com/Community_Support/

Subject: Moderator Please Delete
Body: Please delete this question:
http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_21116587.html
0
 
yeggstryAuthor Commented:
k, split the points between you and webstorm.

Thanks a lot for your help :)
0
 
zzynxSoftware engineerCommented:
Thanks for appreciating our efforts :°)
0
 
WebstormCommented:
:-)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.