• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

model.getValueAt(1,2);

 
Shelby Simpson
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm probably over looking something simple here but I am having problems with the method getValueAt() in the TableModel class. The problem is that it returns Objects and I want to add up the sum of primitive data types,int. Not sure how to approach this any help would be appreciated.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If those objects happen to store ints, then you can get the ints out and add them up. If the objects you put in don't store ints, then there are no ints there to add anyway.

For instance, if you put in Integer objects (possibly without knowing it by passing an int primitive to something that autoboxes(⇐click) it for you), then you can call Integer's intValue() method to get the int out, or just let auto-unboxing do it for you.

If you put in some other object that stores an int, then you'll have to get that int out by whatever means that object provides. For instance maybe it's a Person with a height, that has a getHeight() method.

If the method return Object and does not use generics(⇐click), then you'll have to cast. For example:


I think auto-ubox might even let you do:

 
Shelby Simpson
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the help. I did try cast typing but I keep getting this error -
Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer

Here is the code I'm working on -
 
Rob Camick
Ranch Hand
Posts: 2616
9
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer


Don't add a String to the model. Instead add an Integer.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic