Results 1 to 18 of 18
  1. #1
    geekchick is offline Member
    Join Date
    Aug 2010
    Posts
    25
    Rep Power
    0

    Default Help with getText()

    Hi,

    I'm trying to get the string from my JTextFields and bind them to a JTable. The only problem is I can't even get the values from the Fields! This is probably something simple, but ive been spinning my wheels. On top of that I am getting a NullPointerException error. The error is:

    java.lang.NullPointerException
    at java.awt.Container.addImpl(Container.java:1045)
    at java.awt.Container.add(Container.java:365)
    at CustomerInformation.<init>(Buttons.java:114)
    at Buttons.<init>(Buttons.java:46)
    at Customer.<init>(Buttons.java:64)
    at AirlineReservationDemo.main(AirlineReservationDemo .java:28)

    My code is as follows:

    Java Code:
    import java.awt.Component;
    import java.awt.FlowLayout;
    import java.awt.BorderLayout;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.ArrayList;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    import javax.swing.JLabel;
    import javax.swing.JTextField;
    import javax.swing.table.DefaultTableModel;
    import javax.swing.JTable;
    import javax.swing.JScrollPane;
    import java.awt.event.*;
    import java.awt.*;
    
    
    public class Buttons extends JPanel 
    {
        JPanel panel = new JPanel();
        JFrame frame = new JFrame();
        protected JButton addButton;
        private JButton modifyButton;
        private JButton deleteButton;
        
     
    
        public Buttons()
        {  
            super();
            addButton = new JButton("Add");
            modifyButton = new JButton("Modify");
            deleteButton = new JButton("Delete");
      
            
            panel.setLayout(new FlowLayout());
            panel.add(addButton);
            panel.add(modifyButton);
            panel.add(deleteButton);
            add(panel);
            
           addButton.addActionListener(new CustomerInformation());
    
    
    
        }
    
      
    }
    
    class Customer extends JPanel
    { 
        
        JPanel panel = new JPanel();
        
        public Customer()
        {
                    
            panel.setLayout(new BorderLayout());
            panel.add(new Buttons(), BorderLayout.NORTH);
            panel.add(new CustomerInformation(), BorderLayout.CENTER);
            panel.add(new CustomerDisplay(), BorderLayout.SOUTH);
            
            add(panel);
            
            
        }
        
    
    
    }
    
     class CustomerInformation extends JPanel implements ActionListener
    {
        JPanel panel = new JPanel();
        // Declare JLabels
        private JLabel label1;
        private JLabel label2;
        private JLabel label3;
        private JLabel label4;
        private JLabel label5;
        
        // Declare JTextFields
        private JTextField text1;
        private JTextField text2; 
        private JTextField text3; 
        private JTextField text4; 
        private JTextField text5;
        
        
        
        public CustomerInformation()
        { 
            // Instantiate labels
            label1 = new JLabel("First Name");
            label2 = new JLabel("Last Name");
            label3 = new JLabel("Customer Status");
            label4 = new JLabel("Dependendt Age");
            label5 = new JLabel("Customer ID");
     
            text1 = new JTextField(10);
            text2 = new JTextField(10);
            text3 = new JTextField(10);
            text4 = new JTextField(10);
            text5 = new JTextField(10);
            
           // text1.addActionListener(this);
            //text2.addActionListener(this);
            //text3.addActionListener(this);
           // text4.addActionListener(this);
           // text5.addActionListener(this);
            
            // Set the Grid Layout
            panel.setLayout( new GridLayout(5, 2));
            
            // Add items to grid
            panel.add(label1);
            panel.add(text1);
            panel.add(label2);
            panel.add(text2);
            panel.add(label3);
            panel.add(text3);
            panel.add(label4);
            panel.add(text4);
            panel.add(label5);
            panel.add(text5);
            
            
            add(panel);
            
       
        }
        
          public void actionPerformed(ActionEvent e)
        {
         
                String stringText1 = text1.getText();
                String stringText2 = text2.getText();
                String stringText3 = text3.getText();
                String stringText4 = text4.getText();
                String stringText5 = text5.getText();
             
                System.out.println(stringText1);
     //   }
            
        }  
        
        
    }
    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    public class AirlineReservationDemo extends JPanel {
    private static JButton addButton;
    private static JButton modifyButton;
    private static JButton deleteButton;
    
    
        public static void main(String[] args) 
        {
             JPanel panel = new JPanel();
            // JFrame frame = new JFrame();
    
            addButton = new JButton("Add");
            modifyButton = new JButton("Modify");
            deleteButton = new JButton("Delete");
            
            panel.setLayout(new FlowLayout());
            panel.add(addButton);
            panel.add(modifyButton);
            panel.add(deleteButton);
           // a
            Customer cust = new Customer();
            JTabbedPane tab = new JTabbedPane();
            JFrame frame = new JFrame("Airline");
            tab.add("Customer", cust);
            tab.add("Flight", null);
            tab.add("Reservations", null);
            frame.add(tab, BorderLayout.CENTER);
            frame.setSize(800,500);
            frame.setVisible(true);
            
            new CustomerDisplay();
            new CustomerInformation();
            
        }
    
    }
    Any help will be greatly appreciated.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  3. #3
    geekchick is offline Member
    Join Date
    Aug 2010
    Posts
    25
    Rep Power
    0

    Default

    Customer Display is in a different class, i just didn't post it. I'm not sure what line my code is breaking on...that's what im trying to figure out. But I do know that when i step through the code it's not stepping into:

    text1.addActionListener(this);

    in the CustomerInformation constructor. *sigh* :confused:



    Java Code:
    import java.awt.Component;
    import java.awt.FlowLayout;
    import java.awt.BorderLayout;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.ArrayList;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    import javax.swing.JLabel;
    import javax.swing.JTextField;
    import javax.swing.table.DefaultTableModel;
    import javax.swing.JTable;
    import javax.swing.JScrollPane;
    
    import java.awt.event.*;
    import java.awt.*;
    
    
    public class CustomerDisplay extends JPanel implements ActionListener
    {
        JFrame frame = new JFrame();
        JPanel panel = new JPanel();
        private DefaultTableModel model;
        private JTable table;
        CustomerInformation cust = new CustomerInformation();
        
         private String[] columnNames = {"First Name", "Last Name", "Customer Status", "Dependent Age", "Customer ID"};
        
    public CustomerDisplay()
    {
        super();
        model = new DefaultTableModel();
        model.addColumn("First Name");
        model.addColumn("Last Name");
        model.addColumn("Customer Status");
        model.addColumn("Dependent Age");
        model.addColumn("Customer ID");
        
        
        table = new JTable(model);
        JScrollPane scrollPane = new JScrollPane(table);
        panel.add(scrollPane);
        add(panel);
        
        
    
    }
    
    
      public void actionPerformed(ActionEvent event) {
       String [] customer = {};
      // System.out.println(customer[0]);
       model.addRow(customer);
    
    }
    
    }

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by geekchick View Post
    Customer Display is in a different class, i just didn't post it. I'm not sure what line my code is breaking on...that's what im trying to figure out.
    The error message tells you precisely where the exception occurs:

    java.lang.NullPointerException
    at java.awt.Container.addImpl(Container.java:1045)
    at java.awt.Container.add(Container.java:365)
    at CustomerInformation.<init>(Buttons.java:114)
    at Buttons.<init>(Buttons.java:46)
    at Customer.<init>(Buttons.java:64)

    Line 114 of the Buttons file in the CustomerInformation class, (if the class hasn't yet been changed), which is called by line 46 of Buttons which is called by Customer at line 64 in the Buttons file.

  5. #5
    geekchick is offline Member
    Join Date
    Aug 2010
    Posts
    25
    Rep Power
    0

    Default

    hah! So I figured out why i was getting that null reference exception. Thanks.

    text1.addActionListener(this);

    Was commented out in Customer Information. But I am still confused as to why i cannot get the text from the TextField to write out when I push the Add Button. Here is the correct CustomerInformation class:

    Java Code:
     class CustomerInformation extends JPanel implements ActionListener
    {
        JPanel panel = new JPanel();
        // Declare JLabels
        private JLabel label1;
        private JLabel label2;
        private JLabel label3;
        private JLabel label4;
        private JLabel label5;
        
        // Declare JTextFields
        private JTextField text1;
        private JTextField text2; 
        private JTextField text3; 
        private JTextField text4; 
        private JTextField text5;
        
        
        
        public CustomerInformation()
        { 
            // Instantiate labels
            label1 = new JLabel("First Name");
            label2 = new JLabel("Last Name");
            label3 = new JLabel("Customer Status");
            label4 = new JLabel("Dependendt Age");
            label5 = new JLabel("Customer ID");
     
            text1 = new JTextField(10);
            text2 = new JTextField(10);
            text3 = new JTextField(10);
            text4 = new JTextField(10);
            text5 = new JTextField(10);
            
            text1.addActionListener(this);
            
            // Set the Grid Layout
            panel.setLayout( new GridLayout(5, 2));
            
            // Add items to grid
            panel.add(label1);
            panel.add(text1);
            panel.add(label2);
            panel.add(text2);
            panel.add(label3);
            panel.add(text3);
            panel.add(label4);
            panel.add(text4);
            panel.add(label5);
            panel.add(text5);
            
            
            add(panel);
            
       
        }
        
          public void actionPerformed(ActionEvent e)
        {
         
                String stringText1 = text1.getText();
                System.out.println(stringText1);
     //   }
            
        }  
        
        
    }

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,335
    Rep Power
    25

    Default

    What prints out when you press the Add button?
    What listener method is called?
    Change the println to:
    System.out.println("stringText1=" + stringText1 + "<");
    To show you where and what is happening.
    Your println shows nothing if the text is empty.

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,967
    Rep Power
    19

    Default

    Java Code:
           addButton.addActionListener(new CustomerInformation());
    and

    Java Code:
            panel.add(new CustomerInformation(), BorderLayout.CENTER);
    These are two different CustomerInformation objects.
    One is displayed (the one attached to the tabs) and whose TextFields change as expected. The other (added to the action listener) is not displayed and so the TextFields there remain empty. When the button is pressed this:
    Java Code:
    System.out.println(stringText1);
    will print out an empty string (I expect).

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,335
    Rep Power
    25

  9. #9
    geekchick is offline Member
    Join Date
    Aug 2010
    Posts
    25
    Rep Power
    0

    Default

    Thanks everyone for the replies! Can you offer me some suggestions on how to fix this?

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Don't use two different CustomerInformation objects but instead use one, and then pass references of the one to the other class.

    Also, please read up on the proper etiquette for notifying us about cross-posts here: JavaRanch - Be Forthright When Cross Posting To Other Sites

    Your cooperation on this would be greatly appreciated (and the converse is true!).

    Best of luck.

  11. #11
    geekchick is offline Member
    Join Date
    Aug 2010
    Posts
    25
    Rep Power
    0

    Default

    Ok, thanks i will try that. Sorry about the Cross Posting. I was unaware.

  12. #12
    geekchick is offline Member
    Join Date
    Aug 2010
    Posts
    25
    Rep Power
    0

    Default

    Is it possible to pass the same instance of CustomerInformation to Buttons and Customer when they are two separate classes? This is where i get lost in coding.

  13. #13
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,967
    Rep Power
    19

    Default

    You create one object and pass that reference into the Buttons constructor and into the panel.add() call, rather than doing a new in each one.

  14. #14
    geekchick is offline Member
    Join Date
    Aug 2010
    Posts
    25
    Rep Power
    0

    Default

    Is there anyway you can provide a small code example? I think I understand better but im new to Java. I'm having a hard time finding an example online. Everything I'm reading says that one cannot pass by reference in java.

    Thanks

  15. #15
    geekchick is offline Member
    Join Date
    Aug 2010
    Posts
    25
    Rep Power
    0

    Default

    Also, where would I create the one instance of CustomerInformation and how does that become visible to the Button class?

  16. #16
    geekchick is offline Member
    Join Date
    Aug 2010
    Posts
    25
    Rep Power
    0

    Default

    Or maybe something like this:

    Cat A = new Cat();

    Cat B = A;

  17. #17
    geekchick is offline Member
    Join Date
    Aug 2010
    Posts
    25
    Rep Power
    0

    Default

    Yay!!! I got it to work. Thank you everyone for your help. Is there a SOLVED button here? Here is my code in case someone is having the same problem:

    Java Code:
    class Buttons extends JPanel implements ActionListener
    {
        JPanel panel = new JPanel();
        JFrame frame = new JFrame();
        protected JButton addButton;
        private JButton modifyButton;
        private JButton deleteButton;
        
       //Customer customer = new Customer();
    
        public Buttons()
        {  
          
            super();
            addButton = new JButton("Add");
            modifyButton = new JButton("Modify");
            deleteButton = new JButton("Delete");
      
            
            panel.setLayout(new FlowLayout());
            panel.add(addButton);
            panel.add(modifyButton);
            panel.add(deleteButton);
            add(panel);
            
            CustomerInformation cust1 = Customer.cust;
            
            addButton.addActionListener(cust1);
    
    
    
        }
    
          public void actionPerformed(ActionEvent e)
        {
         
                //String stringText1 = text1.getText();
             
                //System.out.println(stringText1);
      }
            
          
      
    }
    Java Code:
    public CustomerInformation()
        { 
            // Instantiate labels
            label1 = new JLabel("First Name");
            label2 = new JLabel("Last Name");
            label3 = new JLabel("Customer Status");
            label4 = new JLabel("Dependendt Age");
            label5 = new JLabel("Customer ID");
     
            text1 = new JTextField(10);
            text2 = new JTextField(10);
            text3 = new JTextField(10);
            text4 = new JTextField(10);
            text5 = new JTextField(10);
            
           text1.addActionListener(this);
            //text2.addActionListener(this);
            //text3.addActionListener(this);
           // text4.addActionListener(this);
           // text5.addActionListener(this);
            
            // Set the Grid Layout
            panel.setLayout( new GridLayout(5, 2));
            
            // Add items to grid
            panel.add(label1);
            panel.add(text1);
            panel.add(label2);
            panel.add(text2);
            panel.add(label3);
            panel.add(text3);
            panel.add(label4);
            panel.add(text4);
            panel.add(label5);
            panel.add(text5);
            
            
            add(panel);
            
       
        }
        
        
          public void actionPerformed(ActionEvent e)
        {
         
                String stringText1 = text1.getText();
             
                System.out.println(stringText1);
     //   }
            
        }  
        
        
    }

  18. #18
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,335
    Rep Power
    25

    Default

    one cannot pass by reference in java.
    I think the confusion is the usage of the word: reference. The statement would make sense if it said: one can pass a reference in java.

    A reference to an object is another way of saying a pointer to an object. You pass the value of a reference (ie the address of an object).

Similar Threads

  1. getText(); problem
    By The_Sponzy_Paradox in forum Java Applets
    Replies: 5
    Last Post: 04-22-2010, 03:07 AM
  2. What format is a getText()?
    By AJArmstron@aol.com in forum New To Java
    Replies: 1
    Last Post: 04-18-2010, 01:43 AM
  3. gettext problems
    By gisler in forum AWT / Swing
    Replies: 3
    Last Post: 12-01-2009, 07:35 PM
  4. how to getText() properly?
    By javamula in forum New To Java
    Replies: 12
    Last Post: 09-16-2009, 05:45 AM
  5. [SOLVED] new2java- gettext settext
    By obdi in forum New To Java
    Replies: 4
    Last Post: 07-21-2008, 09:28 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •