By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,468 Members | 1,859 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,468 IT Pros & Developers. It's quick & easy.

Errors when deleting a row in a JTable.

P: n/a
Hi everybody,

The following code (putted in a inherited object from AbstractTableModel
object) make some errors :

public void deleteRow(int rowToDelete){
try{
Object dataObject = datas.get(rowToDelete);
String idName = idField.getColumnName();
Method method = dataObjectClass.getMethod("get" +
StringServices.capitalize(idName), null);
Object o = method.invoke(dataObject, null);
deletedRows.add(o);
datas.remove(rowToDelete);
fireTableRowsDeleted(rowToDelete, rowToDelete);
}
catch(Exception e){
e.printStackTrace();
}
}

I don't understand why.

And here is the Stack trace exception

java.lang.NullPointerException
at javax.swing.JTable.prepareRenderer(Unknown Source)
at javax.swing.plaf.basic.BasicTableUI.paintCell(Unkn own Source)
at javax.swing.plaf.basic.BasicTableUI.paintCells(Unk nown 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.swing.JComponent.paint(Unknown Source)
at javax.swing.JViewport.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JLayeredPane.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paintWithOffscreenBuffer(Un known Source)
at javax.swing.JComponent.paintDoubleBuffered(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at java.awt.GraphicsCallback$PaintCallback.run(Unknow n Source)
at sun.awt.SunGraphicsCallback.runOneComponent(Unknow n Source)
at sun.awt.SunGraphicsCallback.runComponents(Unknown Source)
at java.awt.Container.paint(Unknown Source)
at sun.awt.RepaintArea.paint(Unknown Source)
at sun.awt.windows.WComponentPeer.handleEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForHierar chy(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarch y(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

Thanks all

Bernard

Jul 17 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
The same error occurs when I've no data in the table model Object for add
row operation

public void addRow(){
try{
Object o = dataObjectClass.newInstance();
datas.add(o);
insertedRows.add(datas.get(datas.size() - 1));
fireTableRowsInserted(datas.size() - 1, datas.size() - 1);
}
catch(Exception e){
e.printStackTrace();
}
}

java.lang.NullPointerException
at javax.swing.JTable.prepareRenderer(Unknown Source)
at javax.swing.plaf.basic.BasicTableUI.paintCell(Unkn own Source)
at javax.swing.plaf.basic.BasicTableUI.paintCells(Unk nown 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.swing.JComponent.paint(Unknown Source)
at javax.swing.JViewport.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JLayeredPane.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paintWithOffscreenBuffer(Un known Source)
at javax.swing.JComponent.paintDoubleBuffered(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at java.awt.GraphicsCallback$PaintCallback.run(Unknow n Source)
at sun.awt.SunGraphicsCallback.runOneComponent(Unknow n Source)
at sun.awt.SunGraphicsCallback.runComponents(Unknown Source)
at java.awt.Container.paint(Unknown Source)
at sun.awt.RepaintArea.paint(Unknown Source)
at sun.awt.windows.WComponentPeer.handleEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForHierar chy(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarch y(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
"Bernard Koninckx" <ko*************@excite.com> a écrit dans le message de
news: 40***********************@news.skynet.be...
Hi everybody,

The following code (putted in a inherited object from AbstractTableModel
object) make some errors :

public void deleteRow(int rowToDelete){
try{
Object dataObject = datas.get(rowToDelete);
String idName = idField.getColumnName();
Method method = dataObjectClass.getMethod("get" +
StringServices.capitalize(idName), null);
Object o = method.invoke(dataObject, null);
deletedRows.add(o);
datas.remove(rowToDelete);
fireTableRowsDeleted(rowToDelete, rowToDelete);
}
catch(Exception e){
e.printStackTrace();
}
}

I don't understand why.

And here is the Stack trace exception

java.lang.NullPointerException
at javax.swing.JTable.prepareRenderer(Unknown Source)
at javax.swing.plaf.basic.BasicTableUI.paintCell(Unkn own Source)
at javax.swing.plaf.basic.BasicTableUI.paintCells(Unk nown 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.swing.JComponent.paint(Unknown Source)
at javax.swing.JViewport.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JLayeredPane.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paintWithOffscreenBuffer(Un known Source)
at javax.swing.JComponent.paintDoubleBuffered(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at java.awt.GraphicsCallback$PaintCallback.run(Unknow n Source)
at sun.awt.SunGraphicsCallback.runOneComponent(Unknow n Source)
at sun.awt.SunGraphicsCallback.runComponents(Unknown Source)
at java.awt.Container.paint(Unknown Source)
at sun.awt.RepaintArea.paint(Unknown Source)
at sun.awt.windows.WComponentPeer.handleEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForHierar chy(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarch y(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

Thanks all

Bernard

Jul 17 '05 #2

P: n/a
Here is the complete code :

/*
* TableModelObject.java
*
* Created on 22 avril 2004, 11:39
*/

package com.framework.controls.table;

import java.io.InputStream;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.net.URL;
import java.util.ArrayList;
import java.util.Vector;

import javax.swing.table.AbstractTableModel;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.EntityResolver;
import org.xml.sax.InputSource;

import com.framework.misc.fields.Column;
import com.framework.misc.stringservices.StringServices;
import com.framework.misc.fields.IdField;

/**
*
* @author Bernard Koninckx
*/
public class TableModelObject extends AbstractTableModel {
private Class dataObjectClass;
private Vector datas;
private ArrayList columns;
private IdField idField;
private Vector insertedRows;
private Vector deletedRows;
private Vector updatedRows;

/** Creates a new instance of TableModelObject */
public TableModelObject(Vector alDatas, Class cData) {
this(alDatas, cData, null);
}

public TableModelObject(Vector alDatas, Class cData, URL aURL) {
URL url;
insertedRows = new Vector();
deletedRows = new Vector();
updatedRows = new Vector();
if (alDatas == null) datas = new Vector();
else datas = alDatas;
dataObjectClass = cData;
try{
InputStream in = (aURL == null) ?
cData.getResourceAsStream(StringServices.convertCl assName(dataObjectClass) +
".xml") : aURL.openStream();
setColumnsArray(in);
}
catch(Exception e){
e.printStackTrace();
}
}

private void setColumnsArray(InputStream aIn){
try{
columns = new ArrayList();

DocumentBuilderFactory factory =
DocumentBuilderFactory.newInstance();
factory.setIgnoringComments(true);
factory.setCoalescing(true);
factory.setNamespaceAware(false);
factory.setValidating(true);

DocumentBuilder parser = factory.newDocumentBuilder();
parser.setEntityResolver(new LocalEntityResolver());
Document document = parser.parse(aIn);

NodeList fields = document.getElementsByTagName("field");

for (int i = 0; i < fields.getLength(); i++){
Element el = (Element) fields.item(i);
Column col = new Column();
if
(el.getParentNode().getNodeName().equals("column_i dentifier")){
idField = new IdField();
idField.setColumnName(el.getAttribute("name"));
}
col.setColumnName(el.getAttribute("name"));
col.setColumnTitle(el.getAttribute("column_name")) ;
columns.add(col);
}
}
catch(Exception e){
e.printStackTrace();
}
}

private Field findField(Class classType, String asFieldName) throws
NullPointerException {
if (classType == null) { throw new
java.lang.NullPointerException("Cannot find field in ancestor class"); }
try{
return classType.getDeclaredField(asFieldName);
}
catch(Exception e){
return findField(classType.getSuperclass(), asFieldName);
}
}

private void invokeMethod(Object oData, Object oValue, String
methodName, Class className[]) throws
java.lang.IllegalAccessException,
java.lang.IllegalArgumentException,
java.lang.reflect.InvocationTargetException,
java.lang.NoSuchMethodException {

Method m;

try{
m = oData.getClass().getMethod(methodName, className);
Object oValues[] = new Object[1];
oValues[0] = oValue;
m.invoke(oData, oValues);
}
catch(Exception e){
Class newClassName[] = new Class[1];

if (className[0].toString().endsWith("Boolean")) newClassName[0]
= Boolean.TYPE;
if (className[0].toString().endsWith("Byte")) newClassName[0] =
Byte.TYPE;
if (className[0].toString().endsWith("Short")) newClassName[0] =
Short.TYPE;
if (className[0].toString().endsWith("Character"))
newClassName[0] = Character.TYPE;
if (className[0].toString().endsWith("Integer")) newClassName[0]
= Integer.TYPE;
if (className[0].toString().endsWith("Long")) newClassName[0] =
Long.TYPE;
if (className[0].toString().endsWith("Float")) newClassName[0] =
Float.TYPE;
if (className[0].toString().endsWith("Double")) newClassName[0]
= Double.TYPE;

m = oData.getClass().getMethod(methodName, newClassName);
Object oValues[] = new Object[1];
oValues[0] = oValue;
m.invoke(oData, oValues);
}
}

public Class getColumnClass(int columnIndex) {
if (getRowCount()>1)
return getValueAt(0,columnIndex).getClass();
return null;
}

public int getColumnCount() {
return columns.size();
}

public String getColumnName(int row){
Column col = (Column)columns.get(row);
return col.getColumnTitle();
}

public int getRowCount() {
return datas.size();
}

public Object getValueAt(int row, int col) {
Object dataObject = datas.get(row);
Column colInfo = (Column)columns.get(col);
String columnName =
StringServices.capitalize(colInfo.getColumnName()) ;
try{
Method method = dataObjectClass.getMethod("get" + columnName,
null);
return method.invoke(dataObject, null);
}
catch(Exception e){
e.printStackTrace();
return null;
}
}

public void setValueAt(Object aValue, int row, int col){
Object oData = datas.get(row);
Column colInfo = (Column) columns.get(col);
String columnName =
StringServices.capitalize(colInfo.getColumnName()) ;

try {
if (!insertedRows.contains(oData) && !
updatedRows.contains(oData)){
updatedRows.add(datas.get(row));
}
invokeMethod(oData, aValue ,"set" + columnName, new Class[] {
getColumnClass(col) } );
}
catch(Exception e){
e.printStackTrace();
}
fireTableRowsUpdated(row, row);
}

public boolean isCellEditable(int rowIndex, int columnIndex){
return true;
}

public void addRow(){
try{
Object o = dataObjectClass.newInstance();
datas.add(o);
insertedRows.add(datas.get(datas.size() - 1));
fireTableRowsInserted(datas.size() - 1, datas.size() - 1);
}
catch(Exception e){
e.printStackTrace();
}
}

public void deleteRow(int rowToDelete){
try{
Object dataObject = datas.get(rowToDelete);
String idName = idField.getColumnName();
Method method = dataObjectClass.getMethod("get" +
StringServices.capitalize(idName), null);
Object o = method.invoke(dataObject, null);
deletedRows.add(o);
datas.remove(rowToDelete);
this.fireTableRowsDeleted(rowToDelete, rowToDelete);
}
catch(Exception e){
e.printStackTrace();
}
}

public Class getDataTypeClass(){
return dataObjectClass;
}

private class LocalEntityResolver implements EntityResolver{
public InputSource resolveEntity(String publicId, String systemId){
if (publicId.equals("-//TableModelObject/TableModelObject DTD
2.0//EN")){
try {
return new
InputSource(TableModelObject.class.getResourceAsSt ream("TableModelObject.dtd
"));
}
catch (Exception e) {
return null;
}
}
return new InputSource(systemId);
}
}
}

"Bernard Koninckx" <ko*************@excite.com> a écrit dans le message de
news: 40***********************@news.skynet.be...
Hi everybody,

The following code (putted in a inherited object from AbstractTableModel
object) make some errors :

public void deleteRow(int rowToDelete){
try{
Object dataObject = datas.get(rowToDelete);
String idName = idField.getColumnName();
Method method = dataObjectClass.getMethod("get" +
StringServices.capitalize(idName), null);
Object o = method.invoke(dataObject, null);
deletedRows.add(o);
datas.remove(rowToDelete);
fireTableRowsDeleted(rowToDelete, rowToDelete);
}
catch(Exception e){
e.printStackTrace();
}
}

I don't understand why.

And here is the Stack trace exception

java.lang.NullPointerException
at javax.swing.JTable.prepareRenderer(Unknown Source)
at javax.swing.plaf.basic.BasicTableUI.paintCell(Unkn own Source)
at javax.swing.plaf.basic.BasicTableUI.paintCells(Unk nown 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.swing.JComponent.paint(Unknown Source)
at javax.swing.JViewport.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JLayeredPane.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paintWithOffscreenBuffer(Un known Source)
at javax.swing.JComponent.paintDoubleBuffered(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at java.awt.GraphicsCallback$PaintCallback.run(Unknow n Source)
at sun.awt.SunGraphicsCallback.runOneComponent(Unknow n Source)
at sun.awt.SunGraphicsCallback.runComponents(Unknown Source)
at java.awt.Container.paint(Unknown Source)
at sun.awt.RepaintArea.paint(Unknown Source)
at sun.awt.windows.WComponentPeer.handleEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForHierar chy(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarch y(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

Thanks all

Bernard

Jul 17 '05 #3

P: n/a
Hi, Bernard:
public Class getColumnClass(int columnIndex) {
if (getRowCount()>1)
return getValueAt(0,columnIndex).getClass();
return null;


I think it is verboten to return null in getColumnClass. At least, it
makes good sense that prepareRenderer doesn't like it - that method will
use the class fro looking up a suitable renderer (in an overcomplicated
maybe-column-bound maybe-class-bound scheme, by the way).

Try using Object.class or something like that instead.

Also, why do you mix Vectors and ArrayLists ? As far as I know, Vectors
are obsolete, synchronized (slow) and had better be replaced by Lists.

Let me know if I guessed right :)

Soren
Jul 17 '05 #4

P: n/a
The code below return a good class object but I've already the same stack
trace
error

public Class getColumnClass(int columnIndex) {
Column colInfo = (Column)columns.get(columnIndex);
String colName = colInfo.getColumnName();
Field fieldInfo = findField(dataObjectClass, colName);
Class className = fieldInfo.getType();

return fieldInfo.getType();
}

I don't understand what's really the problem.

Bernard

"Soren Kuula" <do************@bitplanet.net> a écrit dans le message de
news: w4**********************@news000.worldonline.dk...
Hi, Bernard:
public Class getColumnClass(int columnIndex) {
if (getRowCount()>1)
return getValueAt(0,columnIndex).getClass();
return null;


I think it is verboten to return null in getColumnClass. At least, it
makes good sense that prepareRenderer doesn't like it - that method will
use the class fro looking up a suitable renderer (in an overcomplicated
maybe-column-bound maybe-class-bound scheme, by the way).

Try using Object.class or something like that instead.

Also, why do you mix Vectors and ArrayLists ? As far as I know, Vectors
are obsolete, synchronized (slow) and had better be replaced by Lists.

Let me know if I guessed right :)

Soren

Jul 17 '05 #5

P: n/a
Bernard Koninckx wrote:
The code below return a good class object but I've already the same stack
trace
error

public Class getColumnClass(int columnIndex) {
Column colInfo = (Column)columns.get(columnIndex);
String colName = colInfo.getColumnName();
Field fieldInfo = findField(dataObjectClass, colName);
Class className = fieldInfo.getType();

return fieldInfo.getType();
}

I don't understand what's really the problem.


The problem IS your column classes. I can see that you sometimes return
Integer.TYPE, or TYPE of the wrapper class for some other simple type.
It does not work - JTable has no renderer installes for these types (I
am not even sure they are subtypes of Object). That's what you get
nullpointer exc.

Try this:
public Class getColumnClass(int columnIndex) {
Column colInfo = (Column)columns.get(columnIndex);
String colName = colInfo.getColumnName();
Field fieldInfo = findField(dataObjectClass, colName);
Class className = fieldInfo.getType();
if (className == Integer.TYPE)
return Object.class;
return fieldInfo.getType();
// if (getRowCount()>=1)
// return getValueAt(0,columnIndex).getClass();
// return null;
}

and it quits crashing.

That said, I think there is something overly complicated about your
table model. I't not worth all of your effort (reflection) to avoid
wrapper types in a TableModel, if that's what you want to do. If you
want to, a better way is still to have the getValueAt return a wrapper
type for underlying primitive types.

Also, avoid file name literals in your code. The code would not run here
until I had found and corrected them. And get rid o' them Vectors :)

Soren
Jul 17 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.