Solved

Tablemodel/vector problem (ArrayIndexOutOfBoundsException)

Posted on 2004-09-02
40
528 Views
Last Modified: 2008-01-09
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.
0
Comment
Question by:yeggstry
  • 14
  • 11
  • 7
  • +2
40 Comments
 
LVL 37

Expert Comment

by:zzynx
ID: 11962350
A longer stack trace would help to determine where in *your* code the exception occurs.
0
 
LVL 12

Expert Comment

by:Giant2
ID: 11962390
From the little stacktrace you post I see that when access to a Vector at position 0, this vector is empty.
0
 
LVL 35

Expert Comment

by:TimYates
ID: 11962393
> 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
 
LVL 13

Expert Comment

by:Webstorm
ID: 11962648
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
 

Author Comment

by:yeggstry
ID: 11962674
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
 
LVL 37

Expert Comment

by:zzynx
ID: 11962694
>> 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
 
LVL 37

Expert Comment

by:zzynx
ID: 11962701
>> Sorry but I cant give you any more of the stacktrace
Why not?
0
 
LVL 37

Accepted Solution

by:
zzynx earned 250 total points
ID: 11962730
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
 
LVL 13

Assisted Solution

by:Webstorm
Webstorm earned 250 total points
ID: 11962754

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
 
LVL 13

Expert Comment

by:Webstorm
ID: 11962769
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
 

Author Comment

by:yeggstry
ID: 11962791
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
 
LVL 37

Expert Comment

by:zzynx
ID: 11962815
It's indeed better to add those brackets there, but I doubt if that is the main problem.
0
 
LVL 13

Expert Comment

by:Webstorm
ID: 11962836
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
 
LVL 35

Expert Comment

by:TimYates
ID: 11962867
can you post all your class code?
0
 
LVL 13

Expert Comment

by:Webstorm
ID: 11962868
>> 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
 
LVL 37

Expert Comment

by:zzynx
ID: 11962911
I rephrase:
It's indeed needed to add those brackets there, ... but I doubt if that is the main problem
;°)
0
 

Author Comment

by:yeggstry
ID: 11962949
>> 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
 
LVL 13

Expert Comment

by:Webstorm
ID: 11962951
ok, let's doubt about it until we get yeggstry's anwer :-)
0
 
LVL 37

Expert Comment

by:zzynx
ID: 11962971
Doubt has become certainty ;°))
0
 

Author Comment

by:yeggstry
ID: 11962978
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 12

Expert Comment

by:Giant2
ID: 11962987
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
 

Author Comment

by:yeggstry
ID: 11963001
erm... I dont fully understand what you mean Giant2
0
 
LVL 37

Expert Comment

by:zzynx
ID: 11963028
>> 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
 
LVL 37

Expert Comment

by:zzynx
ID: 11963050
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
 
LVL 12

Expert Comment

by:Giant2
ID: 11963064
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
 
LVL 37

Expert Comment

by:zzynx
ID: 11963126
Giant, he already did
0
 

Author Comment

by:yeggstry
ID: 11963142
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
 
LVL 37

Expert Comment

by:zzynx
ID: 11963175
The code for DefaultTableModel is available in the JDK, so you can step into it.
0
 
LVL 12

Expert Comment

by:Giant2
ID: 11963336
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
 

Author Comment

by:yeggstry
ID: 11963360
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
 

Author Comment

by:yeggstry
ID: 11963392
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
 
LVL 35

Expert Comment

by:TimYates
ID: 11963401
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
 
LVL 37

Expert Comment

by:zzynx
ID: 11963459
----- 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
 
LVL 12

Expert Comment

by:Giant2
ID: 11963499
>>No, you didn't ;°)

Whops! Now I see correctly (today was an hard day), sorry.
0
 

Author Comment

by:yeggstry
ID: 11963690
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
 

Author Comment

by:yeggstry
ID: 11964040
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
 
LVL 37

Expert Comment

by:zzynx
ID: 11964327
- 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
 

Author Comment

by:yeggstry
ID: 11964551
k, split the points between you and webstorm.

Thanks a lot for your help :)
0
 
LVL 37

Expert Comment

by:zzynx
ID: 11964601
Thanks for appreciating our efforts :°)
0
 
LVL 13

Expert Comment

by:Webstorm
ID: 11964812
:-)
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now