table rendering problem

messageTable =new JTable(model){

public Class getColumnClass(int column)

{

return getValueAt(0, column).getClass();

}

public Component prepareRenderer(

TableCellRenderer renderer,int row,int column)

{

Component c = super.prepareRenderer(renderer, row, column);

c.setBackground(Color.GREEN);

//if (isRowSelected(row) && isColumnSelected(column))

//((JComponent)c).setBorder(new LineBorder(Color.red));

return c;

}

};

xception in thread "AWT-EventQueue-0" java.lang.NullPointerException

at MailClient$1.getColumnClass(MailClient.java:177)

at javax.swing.JTable.getCellRenderer(Unknown Source)

at javax.swing.plaf.basic.BasicTableUI.paintCell(Unknown Source)

at javax.swing.plaf.basic.BasicTableUI.paintCells(Unknown Source)

at javax.swing.plaf.basic.BasicTableUI.paint(Unknown Source)

at javax.swing.plaf.ComponentUI.update(Unknown Source)

at javax.swing.JComponent.paintComponent(Unknown Source)

at javax.swing.JComponent.paint(Unknown Source)

at javax.swing.JComponent.paintChildren(Unknown Source)

at javax.s/.........................................

reason behind exception not able to find please help

thank you

sumant

[1789 byte] By [JTable_need_infoa] at [2007-11-26 22:35:24]
# 1

reason behind exception not able to find please help

Well, the message "java.lang.NullPointerException at MailClient$1.getColumnClass(MailClient.java:177)" pretty obviously means that a reference which you are accessing on line 177 of MailClient.java is null.

Which means that one of your cell values (as returned by getValue()) is null.

Your getColumnClass() implemention is poor anyway: as we can see it doesn't cope with nulls, but it will also break through polymorphism. Let's say one of the columns contains Numbers (some Integers, some Doubles, some Longs)... using your implementation, you can never return the Number class, it will only return the actual class of the first element, so your method cannot describe the widest class of value in that column and you will have values which are not castable to the reported class.

itchyscratchya at 2007-7-10 11:44:33 > top of Java-index,Desktop,Core GUI APIs...
# 2
I know where the error is and what that meansWhat can be the reason.I am creating the model and then table with that model.data has been filled before creating modelso why that null pointer exception is still there
JTable_need_infoa at 2007-7-10 11:44:33 > top of Java-index,Desktop,Core GUI APIs...
# 3
I've told you precisely the reason. The value at "0, column" is null for some value of 'column.'
itchyscratchya at 2007-7-10 11:44:33 > top of Java-index,Desktop,Core GUI APIs...
# 4
I removed first method and it's working fine now
JTable_need_infoa at 2007-7-10 11:44:33 > top of Java-index,Desktop,Core GUI APIs...
# 5
Why did you even put it in then?
itchyscratchya at 2007-7-10 11:44:33 > top of Java-index,Desktop,Core GUI APIs...
# 6

> I removed first method and it's working fine now

Thats not the way to problem solve. If you don't know what the method does, then why are you using it.

The method is important because the table uses it to determine what renderer and editor to use for the given column of data. I guess if all your data is a String then its not that important, but you should understand what it does, before removing it.

The problem with the code is that you have a null value in the TableModel. So is this a valid situation. If not, then fix the problem with the bad data.

If a null is a valid value, then here is an example of a getColumnClass(..) method implementation:

public Class getColumnClass(int column)

{

for (int row = 0; row < getRowCount(); row++)

{

Object o = getValueAt(row, column);

if (o != null)

return o.getClass();

}

return Object.class;

}

camickra at 2007-7-10 11:44:33 > top of Java-index,Desktop,Core GUI APIs...
# 7
thank you i was not knowing tht
JTable_need_infoa at 2007-7-10 11:44:33 > top of Java-index,Desktop,Core GUI APIs...