Welcome to Dream.In.Code
Become a Java Expert!

Join 150,043 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,617 people online right now. Registration is fast and FREE... Join Now!




JTable Null exception...Stumped once again.

2 Pages V  1 2 >  
Reply to this topicStart new topic

JTable Null exception...Stumped once again.

reCoded
30 May, 2008 - 12:10 PM
Post #1

D.I.C Head
**

Joined: 25 Feb, 2008
Posts: 166

Hi All,

I am sure you all probably hate me by now with all the questions. I hope this is my last one. This is my first time using JTabbedPane, JTable and all that jazz.

So to my question. I hope I can explain this well and you all can understand. I have a class TabMain. This is where I create the JFrame (in a method) and also add the JTabbedPane and of course every other tab as well (Same method as the JFrame creation...CreateAndShowGUI). Now when I create the other tabs, I place a JTable in each TabPanel. Each of the tabs are created in their own class (inside a method (CreateTab3) so basically its nice and neat. Now, when I click a button on the frame I want it to populate a certain table. Lets say for this example that it populates the third tab which is called (Tab3). Now since Tab3 is created in its own class I call the "UpdateTable" method inside the Tab3 class. For some reason I get a null exception error. Mind you the JTable, JScrollPane...etc whatever is inside the Tab3 is created in its own class.

What I am thinking is maybe the reason I get the JTable is null is because I create the JTable in the Tab3 class? Should I create each table in the TabMain and pass the JTable as a parameter?



If anyone can help me out it would be much appreciated,
reCoded.

User is offlineProfile CardPM
+Quote Post

mensahero
RE: JTable Null Exception...Stumped Once Again.
30 May, 2008 - 12:24 PM
Post #2

c0mput3rz Are Only Human
Group Icon

Joined: 26 May, 2008
Posts: 664



Thanked: 17 times
Dream Kudos: 75
My Contributions
hello.. nice introduction though... well probably as others would say.. could you please post your code.. to better understand what you are trying to accomplish..

well.. Honestly.. IDK how to use JTable.. but I think I have a slight Idea bout the nullexception error.. if my guess is correct.. it is an error that occur when passing a null value into an object that doesn't accept null..

so its safe to check if your actually passing real values to your table..
another thing.. you are probably updating a Table that doesnt contain anything..

well goodluck..
User is offlineProfile CardPM
+Quote Post

pbl
RE: JTable Null Exception...Stumped Once Again.
30 May, 2008 - 07:43 PM
Post #3

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,587



Thanked: 233 times
Dream Kudos: 75
My Contributions
Show us the code of Tab3
You sure you do not just create the table and put it in the JPanel... the JTable will have to be an instance variable of Tab3
User is online!Profile CardPM
+Quote Post

reCoded
RE: JTable Null Exception...Stumped Once Again.
31 May, 2008 - 06:36 AM
Post #4

D.I.C Head
**

Joined: 25 Feb, 2008
Posts: 166

So I should create an instance variable of Tab3 in the Tab3 class? Or in the class where I create the jframe and where the button is?
User is offlineProfile CardPM
+Quote Post

pbl
RE: JTable Null Exception...Stumped Once Again.
31 May, 2008 - 12:00 PM
Post #5

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,587



Thanked: 233 times
Dream Kudos: 75
My Contributions
QUOTE(reCoded @ 31 May, 2008 - 07:36 AM) *

So I should create an instance variable of Tab3 in the Tab3 class? Or in the class where I create the jframe and where the button is?

You are the architect... your choice... depends how you perceive it
Both have advantages and disadvantages

I would let the GUI in the class of the JFrame and make Tab3 the data handler

User is online!Profile CardPM
+Quote Post

reCoded
RE: JTable Null Exception...Stumped Once Again.
1 Jun, 2008 - 06:42 PM
Post #6

D.I.C Head
**

Joined: 25 Feb, 2008
Posts: 166

PBL so your saying I should have in the main class where the JFrame is created something like, private Tab3 tab3...and when I go to add something to that table when the button is click, tab3.??? where the ??? are is should be a method call that is like a getTableTab3()?

I have all the code in one class right now so it will take me some work to split it back up into their own classes and again and I want to make sure I understand what you are saying before I do so.

Thanks,
reCoded
User is offlineProfile CardPM
+Quote Post

pbl
RE: JTable Null Exception...Stumped Once Again.
1 Jun, 2008 - 06:50 PM
Post #7

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,587



Thanked: 233 times
Dream Kudos: 75
My Contributions
QUOTE(reCoded @ 1 Jun, 2008 - 07:42 PM) *

PBL so your saying I should have in the main class where the JFrame is created something like, private Tab3 tab3...and when I go to add something to that table when the button is click, tab3.??? where the ??? are is should be a method call that is like a getTableTab3()?

I have all the code in one class right now so it will take me some work to split it back up into their own classes and again and I want to make sure I understand what you are saying before I do so.

Thanks,
reCoded

If everything fits in one class righ now and that all the code is about 2 hundred lines no need to really split in in multiple classes. If it exceed this it would worth to built a class Tab3 that extends JTable

So in the the class that builds the JFrame you can

Tab3 t1, t2, t3;
t1 = new Tab3(.....);
add(t1);
t2 = new Tab3(.....);
add(t2);
t3 = new Tab3(.....);
add(t3);

And class Tab3 extends JTable
will manage the data and the JTable selection with the appropriate modelS
So you won't even have to take care which tab of the JTabbedPane is showed

This post has been edited by pbl: 1 Jun, 2008 - 07:07 PM
User is online!Profile CardPM
+Quote Post

reCoded
RE: JTable Null Exception...Stumped Once Again.
2 Jun, 2008 - 04:31 AM
Post #8

D.I.C Head
**

Joined: 25 Feb, 2008
Posts: 166

Snippet for my gui creation

public class Tabs extends DefaultTableModel{

public Tabs(){


}

public static void main(String[] args){


final Tabs gui= new Tabs();

gui.createAndShowGUI();


}


CODE
public void guiCreation(){

        String[] columnNames =
        {
                                   "Col1", "Col2", "Col3"
        
        };

        
        
        addColumns(columnNames);
        
        JFrame.setDefaultLookAndFeelDecorated(true);

        
        Tab2 tab2 = new Tab2();
        tab2.createTab2();


        JFrame frame = new JFrame("Tabs");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);



        
        JPanel topPanel = new JPanel();


  
        tabModel = new DefaultTableModel();
        tabModel.setDataVector(rows,columns);

        JPanel tabPanel = new JPanel();
        
        JTable jt = new JTable(tabModel);
        jt.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        jt.setColumnSelectionAllowed(false);
        
        
        //Set the size of the column headers
          for (int i=0; i<columnNames.length;i++){
          TableColumn col = jt.getColumnModel().getColumn(i);
            int width = 130;
           col.setPreferredWidth(width);
          }

        jt.getTableHeader().setFont(new Font("Arial", Font.PLAIN, 14));

        JScrollPane jsp = new JScrollPane(jt);

        Container contents = new Container();

        jtp = new JTabbedPane();
        
        jtp.addTab("Tab1", jsp);
        jtp.addTab("Tab2", tab2.getjspTab2());


        jtp.setFont(new Font("Arial", Font.PLAIN, 18));
        
        contents.add(jtp);

        JPanel panel = new JPanel();
        panel.setLayout(new BorderLayout());
  
        panel.add(jtp, BorderLayout.CENTER);
        panel.add(topPanel, BorderLayout.NORTH);
    
        frame.getContentPane().add(panel);

        frame.pack();
        frame.setSize(798,500);
        frame.setLocation(400,200);
        frame.setVisible(true);

    }



Now this is the code for the Tab2 class.


CODE

public class Tab2 {
      DefaultTableModel tabModelR;
      JTable jtR;
      JTabbedPane jtpR;
      JPanel rPanel;
      Vector rowsR = new Vector();
      Vector columnsR = new Vector();
      JScrollPane jspR;
      String[] mySQLData;


    
    public TabBarcode(){
        

    }

    public void createTab2(){
        String[] columnNamesR =
        {
        "Col 1",
        "Col 2",
        "Col 3"
                };
        
        addColumns2(column2);
        
        JPanel rPanel = new JPanel();
        rPanel.setLayout(new BorderLayout());
        JPanel topPanelR = new JPanel();

        
  
        tabModelR = new DefaultTableModel();
        tabModelR.setDataVector(rowsR,columnsR);
        
        JPanel tabPanelR = new JPanel();
        
        JTable jtR = new JTable(tabModelR);
        jtR.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        jtR.setColumnSelectionAllowed(true);
        
        //Set the size of the column headers
          
          TableColumn col2 = jtR.getColumnModel().getColumn(2);
          int width = 130;
          col2.setPreferredWidth(width);
          TableColumn col4 = jtR.getColumnModel().getColumn(4);
          col4.setPreferredWidth(width);
          
          
          jtR.getTableHeader().setFont(new Font("Arial", Font.PLAIN, 14));
        setjtR(jtR);

        JScrollPane jspR = new JScrollPane(jtR);
        
        Container contentsR= new Container();
        jtpR = new JTabbedPane();
      
        contentsR.add(jtpR);

        tabPanelR.add(contentsR,BorderLayout.SOUTH);
        
        rPanel.add(topPanelR, BorderLayout.NORTH);  
        rPanel.add(jtpR, BorderLayout.CENTER);
        setRPanel(rPanel);
        setjspR(jspR);
        setDefaultTableModel(tabModelR);
}

public JPanel getRPanel(){
     return rPanel;
}

public void setRPanel(JPanel jp){
     rPanel = jp;
}

public void setjtR(JTable jtable){
     jtR = jtable;
}

public JTable getRTable(){
     return jtR;
}

public void addColumnsR(String[] colName)//Table Columns
{
     for(int i=0;i<colName.length;i++)
         columnsR.addElement((String) colName[i]);
}

public void setjspR(JScrollPane jsPane){
     jspR = jsPane;
}

public JScrollPane getjspR(){
     return jspR;
}
    private void addRowR(String[] str){

        tabModelR.insertRow(0,str);


    }

CODE IS HERE TO ADD THE ROWS TO THIS TABLE.
retrieveRows is the method name!
    }




I have a button also that when click calls the method retrieveRows from the Tab2 class.

I do it like this...

Tab2 tab2 = new Tab2();
tab2.retrieveRows();

inside the retrieveRows method there is a call to the addRows method that is inside the Tab2 class and it does not add the rows but instead gives me a null pointer.

Thanks and hope someone can help me tackle this problem,
reCoded

This post has been edited by reCoded: 2 Jun, 2008 - 04:34 AM
User is offlineProfile CardPM
+Quote Post

reCoded
RE: JTable Null Exception...Stumped Once Again.
2 Jun, 2008 - 05:53 AM
Post #9

D.I.C Head
**

Joined: 25 Feb, 2008
Posts: 166

CODE

public class Tab2 extends DefaultTableModel {
      DefaultTableModel tabModelR;
      JTable jtR;
      JTabbedPane jtpR;
      JPanel rPanel;
      Vector rowsR = new Vector();
      Vector columnsR = new Vector();
      JScrollPane jspR;
      String[] mySQLData;


    
    public TabBarcode(){
        String[] columnNamesR =
        {
        "Col 1",
        "Col 2",
        "Col 3"
                };
        
        addColumns2(column2);
        
        JPanel rPanel = new JPanel();
        rPanel.setLayout(new BorderLayout());
        JPanel topPanelR = new JPanel();

        
  
        tabModelR = new DefaultTableModel();
        tabModelR.setDataVector(rowsR,columnsR);
        
        JPanel tabPanelR = new JPanel();
        
        JTable jtR = new JTable(tabModelR);
        jtR.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        jtR.setColumnSelectionAllowed(true);
        
        //Set the size of the column headers
          
          TableColumn col2 = jtR.getColumnModel().getColumn(2);
          int width = 130;
          col2.setPreferredWidth(width);
          TableColumn col4 = jtR.getColumnModel().getColumn(4);
          col4.setPreferredWidth(width);
          
          
          jtR.getTableHeader().setFont(new Font("Arial", Font.PLAIN, 14));
        setjtR(jtR);

        JScrollPane jspR = new JScrollPane(jtR);
        
        Container contentsR= new Container();
        jtpR = new JTabbedPane();
      
        contentsR.add(jtpR);

        tabPanelR.add(contentsR,BorderLayout.SOUTH);
        
        rPanel.add(topPanelR, BorderLayout.NORTH);  
        rPanel.add(jtpR, BorderLayout.CENTER);
        setRPanel(rPanel);
        setjspR(jspR);
        setDefaultTableModel(tabModelR);


    }



public JPanel getRPanel(){
     return rPanel;
}

public void setRPanel(JPanel jp){
     rPanel = jp;
}

public void setjtR(JTable jtable){
     jtR = jtable;
}

public JTable getRTable(){
     return jtR;
}

public void addColumnsR(String[] colName)//Table Columns
{
     for(int i=0;i<colName.length;i++)
         columnsR.addElement((String) colName[i]);
}

public void setjspR(JScrollPane jsPane){
     jspR = jsPane;
}

public JScrollPane getjspR(){
     return jspR;
}
    private void addRowR(String[] str){

        tabModelR.insertRow(0,str);


    }

    private void addRowR(String[] str){
        
        tabModel.insertRow(0,str);


    }

retrieveRows is the method name!
{
String[] strsR= {"asdf","asdf","asdf","asdf"};
addRowR(strsR);
    }



I changed it around to that and it works. When I add the row I don't get a null pointer error anymore. This time it does not populate the table. I am still trying to get this to work. PBL thanks for your help, if you know what the problem might be let me know. Should I use that fire method after I add the rows? I am guessing I am missing something simple.

Thanks,
reCoded

This post has been edited by reCoded: 2 Jun, 2008 - 05:56 AM
User is offlineProfile CardPM
+Quote Post

pbl
RE: JTable Null Exception...Stumped Once Again.
2 Jun, 2008 - 07:49 AM
Post #10

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,587



Thanked: 233 times
Dream Kudos: 75
My Contributions
Ouf !!! you make it complicated but it might work. You have decided to extend JTable not DefaultTableModel but it is your choice.

Few remarks:

You don't have a constructor for Tab2

CODE

    DefaultTableModel tabModelR;


Why would you need a different DefaultTableModel ? Tab2 IS the DefaultTableModel.

CODE

    public TabBarcode(){


What is TabBarcode ? A int, a void, a double ? I guess this what should be your constructor
So public Tab2() ?

CODE

        String[] columnNamesR =
        {
                "Col 1",
                "Col 2",
                "Col 3"
        };

        addColumns2(column2);

I presume you mean addColumns2(columnNamesR);

addColumns2 is not defined should look like

CODE

    void addColumns2(String[] str) {
        for(int i = 0; i < str.length; i++)
            addColumn(str[i]);
    }


CODE

        tabModelR = new DefaultTableModel();
        tabModelR.setDataVector(rowsR,columnsR);


Tab2 IS a DefaultTableModel you don't need one so

CODE

        setDataVector(rowsR,columnsR);


Same thing
CODE

replace
        JTable jtR = new JTable(tabModelR);
by
        JTable jtR = new JTable(this);


The container is the JFrame in the main Class you don't need one here
If you want something to organize your panels use a JPanel a lot much lighter

CODE

        JPanel contentsR= new JPanel(new BorderLayout());

Also your JTabbedPane will have only 1 pane ?

I thing that all that should go into the class that builds the JFrame keep data and GUI separated

This does not make sense
CODE

        setDefaultTableModel(tabModelR);
or
        setDefaultTableModel(this);

setDefaultTableModel() applies to a JTable not to a DefaultTableModel which Tab2 is.

You have a duplicate addRowR methods and both are wrong

CODE

    private void addRowR(String[] str){

        tabModelR.insertRow(0,str);
    }

    private void addRowR(String[] str){

        tabModel.insertRow(0,str);
    }


As Tab2 is the DefaultTableModel it should look like this

CODE

    private void addRowR(String[] str){

        insertRow(0,str);
    }









User is online!Profile CardPM
+Quote Post

reCoded
RE: JTable Null Exception...Stumped Once Again.
2 Jun, 2008 - 08:17 AM
Post #11

D.I.C Head
**

Joined: 25 Feb, 2008
Posts: 166

Thanks pbl,

I am really frusterated with this. It still will not add the row to the JTable that is inside of Tab2. The weird thing is I am doing the samething I did when the code was all in the same class. This is starting to annoy me and I am starting to code very sloppy because I had enough with this.

I really appreciate you helping me out.
User is offlineProfile CardPM
+Quote Post

pbl
RE: JTable Null Exception...Stumped Once Again.
2 Jun, 2008 - 10:22 AM
Post #12

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,587



Thanked: 233 times
Dream Kudos: 75
My Contributions
That should put you on track.... I hope

java

import javax.swing.*;
import java.awt.*;
public class Frame extends JFrame {

final static String[] tab1ColName = {"Col 1", "Col 2", "Col 3"};
final static String tab1LabelName = "This is the header of Tab 1";
final static String[] tab2ColName = {"Col A", "Col B", "Col C", "Col D"};
final static String tab2LabelName = "This is the header of Tab 2";
final static String[] tab3ColName = {"First Col", "Second col"};
final static String tab3LabelName = "This is the header of Tab 3";
final static String[] tabName = {"Tab one", "Tab two", "Tab three"};


JTabbedPane pane;
Tab tab[];

Frame() {
super("reCoded Application");
setLayout(new BorderLayout());
setSize(400, 300);
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

JLabel label = new JLabel("This is the main header of the application");
label.setHorizontalAlignment(SwingConstants.CENTER);
add(label, BorderLayout.NORTH);

pane = new JTabbedPane();
tab = new Tab[3];
tab[0] = new Tab(tab1ColName, tab1LabelName);
pane.add(tabName[0], tab[0]);
tab[1] = new Tab(tab2ColName, tab2LabelName);
pane.add(tabName[1], tab[1]);
tab[2] = new Tab(tab3ColName, tab2LabelName);
pane.add(tabName[2], tab[2]);
add(pane, BorderLayout.CENTER);
}

// to do the test
void test() {
String[] abc = {"Col1", "Col2", "Col3", "Col4"};
// add a row
tab[1].addRow(abc);
// show it
pane.setSelectedIndex(1);
String[] str = tab[1].getRow(0);
System.out.println("Rows 0 of tab2 contains: ");
for(int i = 0; i < str.length; i++)
System.out.print(" " + str[i]);
System.out.println();

}
public static void main(String[] arg) {
Frame f = new Frame();
f.setVisible(true);

// ok now test
f.test();
}
}


java


import javax.swing.*;
import javax.swing.table.*;
import java.awt.*;

public class Tab extends JPanel {

JTable table;
DefaultTableModel model;

Tab(String[] colName, String labelName) {
super(new BorderLayout());
add(new JLabel(labelName), BorderLayout.NORTH);
model = new DefaultTableModel();
for(int i = 0; i < colName.length; i++)
model.addColumn(colName[i]);
table = new JTable(model);
add(new JScrollPane(table), BorderLayout.CENTER);
}

void addRow(String[] str) {
model.addRow(str);
}

String[] getRow(int n) {
if(n >= model.getRowCount())
throw new IllegalStateException("getRow for " + n + " exceeds numner of rows");
int c = model.getColumnCount();
String[] str = new String[c];
for(int i = 0; i < c; i++)
str[i] = (String) model.getValueAt(n, i);
return str;
}
}


I think must of the stuff is there
User is online!Profile CardPM
+Quote Post

2 Pages V  1 2 >
Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 09:57PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month