0

I have the following classes

class Parsing

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;}
}

class JTableInstantiation

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

  }
}

When I run it, I get a java.lang.NullPointerException. I figured out that the table inside ClassWithTable cannot be instantiated, despite the fact that rowData take all right values. Could you please explain to me why is this happening? What am I doing wrong? Thank you in advance.

Exception table:

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)
Vassilis De
  • 363
  • 2
  • 17

2 Answers2

3

Keep track of your fields. You have two ClassWithTable objects.

class JTableInstantiation

ClassWithTable vEntity = new ClassWithTable();

You have vEntity here. Then you do:

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

Look at what's inside Parsing

private ClassWithTable c = new ClassWithTable();

You haven't done anything with vEntity at all. You need to either assign vEntity the value of p.getClassWithTable() or pass vEntity to p.

Community
  • 1
  • 1
Compass
  • 5,502
  • 4
  • 27
  • 40
  • I corrected the fields. You may see the edit – Vassilis De Oct 27 '14 at 21:01
  • couldn't I remove vEntity from JTableInstantiation, move println inside Parsing#parseRowData and call p.doAction() inside main to print table? – Vassilis De Oct 27 '14 at 21:15
  • @VassilisDe Yes. It depends ultimately on what you want to do with your method. If Parsing is the only one that is responsible for the Table, then you can keep it in there as a field. – Compass Oct 27 '14 at 21:24
  • Well I did this pretty much like my updated question. Like you said, `Parsing` is the only class that is responsible for the Table, but still I get that exception... – Vassilis De Oct 27 '14 at 21:30
  • 1
    @VassilisDe Try `new String[]{"A","B"};` for column names. I believe it also needs actual values for the names. – Compass Oct 27 '14 at 21:32
  • you are genious!!!! That was the problem...everything works!! Thank you so much!!! – Vassilis De Oct 27 '14 at 21:36
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();
  }
}
Ezequiel
  • 3,277
  • 1
  • 15
  • 27
  • couldn't I just move println inside Parsing#parseRowData and call `p.doAction()` to print table? initComponents() is called, but not inside `main()` – Vassilis De Oct 27 '14 at 21:13
  • @Vassilis De now I see you are instatiating twice ClassWithTable. I'll uodate my answer. – Ezequiel Oct 27 '14 at 21:17
  • I have it EXACTLY the way you wrote it, but I'm still getting the same exeption – Vassilis De Oct 27 '14 at 21:28
  • 1
    The line that is giving the NPE in Jtable is `public String getColumnName(int column) { return columnNames[column].toString(); }` . Need to use notNull names for columns. – Ezequiel Oct 27 '14 at 21:33