Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to set/change String dates to Object Date in ArrayList<Object[]>

 
joe encalada
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a table model which extends from AbstractTableModel and data in the form of  ArrayList<Object[]> . Elements on ArrayList<Object[]> are String and in column 5 I have dates in string class so I want to change every string date to date object.



If I use setValueAt(Object value, int row, int col) to set any string word everything works ok, but If I try to set a date (setValueAt(new Date(),0,5)) I get Exception in thread "AWT-EventQueue-0" java.lang.ArrayStoreException: java.util.Date.

Here is my code for setValueAt():



I hope you can help me.
 
Henry Wong
author
Marshal
Pie
Posts: 22111
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
joe encalada wrote:If I try to set a date (setValueAt(new Date(),0,5)) I get Exception in thread "AWT-EventQueue-0" java.lang.ArrayStoreException: java.util.Date.


An array store exception is caused by trying to set an element of an array which is not allowed.

And ... Unfortunately, you did not show us the code, that initialized the ArrayList, so we can't tell exactly what is the array type that you placed into the list.

It may help, if you showed us the array list initalization code.

Henry
 
Henry Wong
author
Marshal
Pie
Posts: 22111
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To elaborate some more. If your initialization code added the object arrays like so...

then, when you try to add a date object, you will get the exception. On the other hand, if your initialization code added the object arrays like so...

then it would have worked.

Henry
 
Rob Camick
Ranch Hand
Posts: 2673
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Variable names should NOT start with an upper case character. One is correct and one isn't  Be consistent!!!
 
joe encalada
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are right Rob. I'll correct my code.

 
joe encalada
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry here is my code:


And this is data in file.dcsv

0,,1,,2,,3,,4,,01/01/2016,,6,,7
0,,1,,2,,3,,4,,02/02/2016,,6,,7
0,,1,,2,,3,,4,,03/03/2016,,6,,7
 
Henry Wong
author
Marshal
Pie
Posts: 22111
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

It is as I explained in my previous post.  Your Object array is actually a String array, and hence, cannot hold Date objects. See line 129 in the code, to see the String array (returned from the split() method).

Henry
 
Rob Camick
Ranch Hand
Posts: 2673
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all:

1) Variable names should not start with an upper case character. Most of your names are correct other are not. Follow Java conventions and be consistent.

2) Why are you creating a custom TableModel? The DefaultTableModel supports an addRow(...) method. So when you read your rows of data you just add each row to the model. There is no need for the custom model other than to override the isCellEditable(...) method.

To expand on Henry's comment, you need to:

1. Split each row of data into a String[] array (not an Object array to be consistent with the String.split(...) API).
2. Create an Object[] array
3. Create a loop to copy the objects from the String array to the Object array.
 
joe encalada
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see your point. I'll do your suggestion (to use DefaultTableModel and copy data from Array String to Array Object).

Thanks Henry, Rob.
 
joe encalada
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rob, I'm doing what you suggested and I've created a method addCsvData to pass Array String to Array Object here my code:

But I'm getting this error "AWT-EventQueue-0" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
 
Rob Camick
Ranch Hand
Posts: 2673
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please read my suggestion again. I stated:

1. The only reason to extend the DefaultTableModel is to implement the isCellEditable(...) method.

2. You use the addRow(...) method to add individual rows of data to the model.

So you first create an empty DefaultTableModel by just specifying the column names. Read the API for the proper constructor.

Then as you read each row of data from the file you split the data, copy the data to the Object[] array and then add the Object[] array to the table model.
 
joe encalada
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rob, I've modified addCsvData(ArrayList<String[]> dataIn) method like this:


But no rows are added to my table, why?
 
Rob Camick
Ranch Hand
Posts: 2673
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't extend DefaultTableModel!!!

Just use the DefaultTableModel as is.

The reading of the data is not part of the model, it is just code you add when you create the JTable and DefaultTableModel.

Something like:


 
joe encalada
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Rob, I did it with this line.


 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic