Link to home
Start Free TrialLog in
Avatar of yeggstry
yeggstry

asked on

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.
Avatar of zzynx
zzynx
Flag of Belgium image

A longer stack trace would help to determine where in *your* code the exception occurs.
Avatar of Giant2
Giant2

From the little stacktrace you post I see that when access to a Vector at position 0, this vector is empty.
> 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 ;
    }
}
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.

Avatar of yeggstry

ASKER

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.)
>> 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.
>> Sorry but I cant give you any more of the stacktrace
Why not?
ASKER CERTIFIED SOLUTION
Avatar of zzynx
zzynx
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
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
It's indeed better to add those brackets there, but I doubt if that is the main problem.
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");
                }
can you post all your class code?
>> 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.
I rephrase:
It's indeed needed to add those brackets there, ... but I doubt if that is the main problem
;°)
>> 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 :/
ok, let's doubt about it until we get yeggstry's anwer :-)
Doubt has become certainty ;°))
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 ;))
I believe you have a setValue over your TableModel.
Try to debug this.
and print the size of the Vector you are inserting in.
erm... I dont fully understand what you mean Giant2
>> 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...
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
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.
Giant, he already did
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...
The code for DefaultTableModel is available in the JDK, so you can step into it.
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.



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).
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.
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...
----- 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)
>>No, you didn't ;°)

Whops! Now I see correctly (today was an hard day), sorry.
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).
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?
- 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 https://www.experts-exchange.com/Community_Support/

Subject: Moderator Please Delete
Body: Please delete this question:
https://www.experts-exchange.com/questions/21116587/Tablemodel-vector-problem-ArrayIndexOutOfBoundsException.html
k, split the points between you and webstorm.

Thanks a lot for your help :)
Thanks for appreciating our efforts :°)