Java表无法实例化

时间:2014-10-27 20:48:46

标签: java swing nullpointerexception jtable instance

我有以下课程

课程解析

class Parsing {
  private ClassWithTable c = new ClassWithTable();

  public void doAction(){
    parseRowData();
    c.initComponents();
  }

  private void parseRowData(){
    Object[][] rowData = new Object[3][2];
    for(int i = 0; i < 3; i++){
        rowData[i][0] = "A string";
        rowData[i][1] = null;
    }
    c.setRowData(rowData);
  }
}

class ClassWithTable

class ClassWithTable {
  private JTable table;
  private Object[][]rowData;
  private final String[] colNames = new String[]{null,null};

  public void initComponents() {
    table = new JTable(getRowData(),colNames); //in this line I get the exception
    System.out.println(table.getValueAt(1, 1));
  }

  public void setRowData(Object[][]rowData){this.rowData = rowData;}
  private Object[][] getRowData(){return this.rowData;}  
  public JTable getTable(){return table;}
}

类JTableInstantiation

public class JTableInstantiation {
  public static void main (String[] af){
    ClassWithTable c = new ClassWithTable();
    Parsing p = new Parsing();
    p.doAction();

  }
}

当我运行它时,我得到一个java.lang.NullPointerException。我发现ClassWithTable中的表无法实例化,尽管rowData采用了所有正确的值。能告诉我为什么会这样吗?我究竟做错了什么?提前谢谢。

异常表:

Exception in thread "main" java.lang.NullPointerException
at javax.swing.JTable$1.getColumnName(JTable.java:685)
at javax.swing.JTable.addColumn(JTable.java:2801)
at javax.swing.JTable.createDefaultColumnsFromModel(JTable.java:1287)
at javax.swing.JTable.tableChanged(JTable.java:4386)
at javax.swing.JTable.setModel(JTable.java:3688)
at javax.swing.JTable.<init>(JTable.java:632)
at javax.swing.JTable.<init>(JTable.java:573)
at javax.swing.JTable.<init>(JTable.java:684)
at drafts.ClassWithTable.initComponents(JTableInstantiation.java:28)
at drafts.Parsing.doAction(JTableInstantiation.java:10)
at drafts.JTableInstantiation.main(JTableInstantiation.java:41)

2 个答案:

答案 0 :(得分:3)

跟踪您的字段。您有两个ClassWithTable个对象。

类JTableInstantiation

ClassWithTable vEntity = new ClassWithTable();

你在这里有vEntity。然后你做:

  Parsing p = new Parsing();
  p.doAction();

查看Parsing

中的内容
private ClassWithTable c = new ClassWithTable();

您根本没有使用vEntity做任何事情。您需要为vEntity分配p.getClassWithTable()或将vEntity传递给p

答案 1 :(得分:1)

class Parsing {
  private ClassWithTable c = new ClassWithTable();

  public void doAction(){
    parseRowData();
    c.initComponents();
    System.out.println(c.getTable().getValueAt(1, 1));
  }

  private void parseRowData(){
    Object[][] rowData = new Object[3][2];
    for(int i = 0; i < 3; i++){
        rowData[i][0] = "A string";
        rowData[i][1] = null;
    }
    c.setRowData(rowData);
  }
}

class ClassWithTable {
  private JTable table;
  private Object[][]rowData;
  private final String[] colNames = new String[]{"A","B"}; // CHANGED nulls for Strings

  public void initComponents() {
    table = new JTable(getRowData(),colNames); //in this line I get the exception
    System.out.println(table.getValueAt(1, 1));
  }

  public void setRowData(Object[][]rowData){this.rowData = rowData;}
  private Object[][] getRowData(){return this.rowData;}  
  public JTable getTable(){return table;}
}
public class JTableInstantiation {
  public static void main (String[] af){
    Parsing p = new Parsing();
    p.doAction();
  }
}