This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes model.getValueAt(1,2); Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "model.getValueAt(1,2);" Watch "model.getValueAt(1,2);" New topic
Author

model.getValueAt(1,2);

Shelby Simpson
Greenhorn

Joined: Feb 17, 2013
Posts: 26
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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Feb 17, 2013
Posts: 26
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

Joined: Jun 13, 2009
Posts: 2204
    
    7
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: model.getValueAt(1,2);