我来到你面前询问我在java编程时遇到的问题。这个主题是关于JTable的。
现在让我们谈谈我的目标。我有一个标准列表(一个arraylist),一个列表一个场景(仍然是一个arraylist)。我创建了一个类来将每个场景与一个值相关联,并且每个条件都有一个这个新对象的列表(现在我将调用新对象" ValueCriterion")。标准可以是1个或更多标准的父亲。我有一个用于儿童的tableModel(在ValueCriterion的帮助下(这个模型完美地工作)和父母(为每个场景显示每个孩子的价值,显示每个场景的价值)(这个是这个)哪个不行。)
现在问题。当我创建我的2个标准(1是另一个的孩子),没有问题,我有情景,仍然没有问题,但如果我有一个孩子对实际的父亲,当我选择父亲时,程序给我一个漂亮的错误(见下文)。老实说,我不知道该怎么办才能找到这个废话错误。
所以我希望有人可以帮助我,这里错误和模型的代码没有用。
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at javax.swing.JTable.prepareRenderer(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.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.JSplitPane.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.JSplitPane.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JSplitPane.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.paintToOffscreen(Unknown Source)
at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(Unknown Source)
at javax.swing.RepaintManager$PaintManager.paint(Unknown Source)
at javax.swing.RepaintManager.paint(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at java.awt.GraphicsCallback$PaintCallback.run(Unknown Source)
at sun.awt.SunGraphicsCallback.runOneComponent(Unknown Source)
at sun.awt.SunGraphicsCallback.runComponents(Unknown Source)
at java.awt.Container.paint(Unknown Source)
at java.awt.Window.paint(Unknown Source)
at javax.swing.RepaintManager$4.run(Unknown Source)
at javax.swing.RepaintManager$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.prePaintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.access$1300(Unknown Source)
at javax.swing.RepaintManager$ProcessingRunnable.run(Unknown Source)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
代码:
package views;
import java.util.ArrayList;
import java.util.HashMap;
import javax.swing.table.AbstractTableModel;
import models.Controller;
import models.Criterion;
import models.Scenario;
public class TableMod extends AbstractTableModel{
private static final long serialVersionUID = 1L;
private Object[][] data;
private String[] title;
private boolean onEdit = true;
private Criterion criterion;
private Controller cont;
public TableMod(Object[][] data, String[] title) {
this.data = data;
this.title = title;
}
public TableMod(Controller c) {
if(c != null){
this.criterion = c.getSelectedNode();
this.cont = c;
this.title = new String[]{"Scénario"};
if(criterion!=null){
this.data = new Object[0][criterion.getChildren().size()+1];
}else{
this.data = new Object[0][2];
}
}else{
this.title = new String[]{"Scénario","Childrens"};
this.data = new Object[0][2];
}
}
@Override
public int getColumnCount() {
return this.title.length;
}
@Override
public int getRowCount() {
return this.data.length;
}
@Override
public Object getValueAt(int row, int col) {
if(data.length>row){
if(data[row].length>col){
return this.data[row][col];
}else{
return null;
}
}else{
return null;
}
}
public Object[][] getValue() {
return this.data;
}
@Override
public String getColumnName(int col) {
return this.title[col];
}
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public Class getColumnClass(int col){
if(data[0].length>col){
if(data[0][col]!=null){
if(data[0][col].getClass()!=null){
return data[0][col].getClass();
}else{
return null;
}
}else{
return null;
}
}else{
return null;
}
}
@Override
public boolean isCellEditable(int row, int col) {
return (col == 3 && !onEdit)?false:true;
}
@Override
public void setValueAt(Object obj, int row, int col) {
if((col == 0 || col == 3) && obj.toString().isEmpty())
return;
else if(col == 0 && exist(obj, row))
return;
this.data[row][col] = obj;
fireTableCellUpdated(row, col);
}
public void setData(Object[][] data) {
this.data = data;
this.fireTableDataChanged();
}
public void setData(String scale){
for(int i=0; i<data.length; i++)
this.data[i][3] = scale;
this.fireTableDataChanged();
}
public void setData(HashMap<String, String> values){
for(int i=0; i<data.length; i++){
if(values.containsKey(data[i][0]))
data[i][3] = values.get(data[i][0]);
}
this.fireTableDataChanged();
}
public HashMap<String, String> getValues(){
HashMap<String, String> map = new HashMap<String, String>();
for(int i=0; i<data.length; i++){
map.put(data[i][0].toString(), data[i][3].toString());
}
return map;
}
public void load(ArrayList<Scenario> list){
if(this.criterion!=null){
if(criterion.getChildren()!=null){
data = new Object[list.size()][criterion.getChildren().size()+1];
for(int i=0; i<list.size(); i++){
data[i][0] = list.get(i).getName();
}
this.fireTableDataChanged();
}
}
}
public ArrayList<Scenario> getScenarios(){
int tmp;
ArrayList<Scenario> list = new ArrayList<Scenario>();
int t = 0;
for(int i=0; i<data.length; i++){
tmp=Scenario.getCount();
t = i;
t+= 1;
Scenario s = new Scenario(data[i][0].toString(),data[i][0].toString(),"",t);
list.add(s);
Scenario.setCount(tmp);
}
return list;
}
public void addRow(Object[] data){
int indice = 0;
int nbRow = this.getRowCount();
int nbCol = this.getColumnCount();
Object temp[][] = this.data;
this.data = new Object[nbRow+1][nbCol];
for(Object[] value : temp)
this.data[indice++] = value;
this.data[indice] = data;
this.fireTableDataChanged();
}
public void removeRow(int index){
if(index < 0){
return;
}
int i = 0, j= 0;
Object temp[][] = new Object[this.getRowCount()-1][this.getColumnCount()];
for(Object[] value : this.data){
if(i != index)
temp[j++] = value;
i++;
}
this.data = temp;
this.fireTableDataChanged();
}
public void setOnEdit(boolean b){
onEdit = b;
}
/**
* Cette méthode teste l'existence d'un objet dans le modèle
* @param ob objet
* @param row ligne de l'objet en cours de modification
* @return vrai en cas de doublon, faux sinon
*/
private boolean exist(Object ob, int row){
for(int i=0; i<data.length; i++){
if(i != row && data[i][0].equals(ob)){
return true;
}
}
return false;
}
public void setTit(Controller c){
this.criterion=c.getSelectedNode();
String[] childrenNames = new String[c.getSelectedNode().getChildren().size()+1];
childrenNames[0]="Scenario";
for(int i = 0; i<c.getSelectedNode().getChildren().size(); i++){
childrenNames[i+1]=c.getSelectedNode().getChildren().get(i).getName();
}
this.title = childrenNames;
}
public Criterion getCriterion(){
return this.criterion;
}
public void refreshTable(){
Object temp[][] = this.data;
this.data = new Object[this.getRowCount()][this.getColumnCount()];
for(int i = 0; i<this.getRowCount(); i++){
this.data[i][0]=temp[i][0];
for(int j = 1; j<this.getColumnCount(); j++){
for(int k = 0;k<criterion.getChildren().get(j-1).getCritValue().size();k++){
if(criterion.getChildren().get(j-1).getCritValue().get(k).getScenarioName().equals(this.data[i][0])){
this.data[i][j]=criterion.getChildren().get(j-1).getCritValue().get(k).getValue();
}
}
}
}
}
}
答案 0 :(得分:3)
这让我很担心:
public Class getColumnClass(int col){
if(data[0].length>col){
if(data[0][col]!=null){
if(data[0][col].getClass()!=null){
return data[0][col].getClass();
}else{
return null;
}
}else{
return null;
}
}else{
return null;
}
}
所有这些return null;
语句看起来都不健康,实际上看起来很冒险,特别是当渲染器尝试渲染一个包含空值的单元格时。至少返回super.getColumnClass(col);
作为默认返回语句。
另外,这种方法不能简化吗?例如,如果单元格保存非空值,则表示值getClass()
不为空。例如:
public Class getColumnClass(int col) {
if(data[0].length > col && data[0][col] != null) {
return data[0][col].getClass();
} else {
return super.getColumnClass(col); // a non-null default value
}
}
或者更好:
public Class getColumnClass(int col) {
if (col < 0 || col >= getColumnCount()) {
String text = "col value: " + col;
throw new IllegalArgumentException(text);
} else if(getRowCount() > 0 && data[0][col] != null) {
return data[0][col].getClass();
} else {
return super.getColumnClass(col);
}
}