Auto Boxing not working in Java 6?
807588Mar 12 2009 — edited Mar 12 2009Hi All,
I am studying and practicing to become Java Certified and I am just using an example from the Java Certification Guide Chapter 11 Swing Components from Sybex for Java 5.
I am using Eclipse and the latest Java 6 SDK in WIndows XP.
The code below runs fine in Solaris 10 using Java 1.5.0_14-b03:
import java.util.*;
import javax.swing.*;
import javax.swing.table.*;
public class StringLengthTableModel extends AbstractTableModel {
private String[] strings;
public StringLengthTableModel(String[] strings) {
this.strings = strings;
}
public int getRowCount() {
return strings.length;
}
public int getColumnCount() {
return 2;
}
public Object getValueAt(int row, int col) {
if (col == 0) {
return strings[row];
} else {
return strings[row].length();
}
}
public String getColumnName(int col) {
return (col == 0) ? "String" : "Length";
}
public static void main(String[] args) {
JFrame frame = new JFrame();
String[] strings = { "January", "February", "March", "April", "May",
"June", "July", "August", "September", "October", "November",
"December" };
StringLengthTableModel model = new StringLengthTableModel(strings);
JTable table = new JTable(model);
frame.getContentPane().add(table);
frame.setSize(500, 250);
frame.setVisible(true);
}
}
However in Windows XP and using the latest Eclipse IDE and the Latest Java 6 SDK I get:
Exception in thread "AWT-EventQueue-0" java.lang.Error: Unresolved compilation problem:
Type mismatch: cannot convert from int to Object
The questionable method line is
return strings[row].length();
in the method
public Object getValueAt(int row, int col) {
if (col == 0) {
return strings[row];
} else {
return strings[row].length();
}
}
Any ideas????
You would think and hope the latest release of Java like Java 6 would be backward compatible with the auto boxing and auto un boxing features that were introduced in Java 5.
Any comments, thoughts, ideas would be appreciated.
Thanks,
Carlos A. Morillo
Carlos.Morillo@GMail.COM