Results 1 to 18 of 18
Thread: Help with getText()
- 08-05-2010, 10:59 AM #1
Member
- Join Date
- Aug 2010
- Posts
- 25
- Rep Power
- 0
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); // } } }Any help will be greatly appreciated.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(); } }
-
Your code is missing the CustomerDisplay class. Also, which line throws the exception?
- 08-05-2010, 04:14 PM #3
Member
- Join Date
- Aug 2010
- Posts
- 25
- Rep Power
- 0
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); } }
-
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.
- 08-05-2010, 04:45 PM #5
Member
- Join Date
- Aug 2010
- Posts
- 25
- Rep Power
- 0
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); // } } }
- 08-05-2010, 05:10 PM #6
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.
- 08-05-2010, 05:17 PM #7
Moderator
- Join Date
- Apr 2009
- Posts
- 10,466
- Rep Power
- 16
andJava Code:addButton.addActionListener(new CustomerInformation());
These are two different CustomerInformation objects.Java Code:panel.add(new CustomerInformation(), BorderLayout.CENTER);
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:
will print out an empty string (I expect).Java Code:System.out.println(stringText1);
- 08-05-2010, 06:58 PM #8
Cross posted at Help with the getText() method - CodeGuru Forums
- 08-05-2010, 10:02 PM #9
Member
- Join Date
- Aug 2010
- Posts
- 25
- Rep Power
- 0
Thanks everyone for the replies! Can you offer me some suggestions on how to fix this?
-
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.
- 08-05-2010, 10:23 PM #11
Member
- Join Date
- Aug 2010
- Posts
- 25
- Rep Power
- 0
Ok, thanks i will try that. Sorry about the Cross Posting. I was unaware.
- 08-06-2010, 05:14 AM #12
Member
- Join Date
- Aug 2010
- Posts
- 25
- Rep Power
- 0
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.
- 08-06-2010, 08:48 AM #13
Moderator
- Join Date
- Apr 2009
- Posts
- 10,466
- Rep Power
- 16
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.
- 08-06-2010, 05:19 PM #14
Member
- Join Date
- Aug 2010
- Posts
- 25
- Rep Power
- 0
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
- 08-06-2010, 05:25 PM #15
Member
- Join Date
- Aug 2010
- Posts
- 25
- Rep Power
- 0
Also, where would I create the one instance of CustomerInformation and how does that become visible to the Button class?
- 08-06-2010, 05:30 PM #16
Member
- Join Date
- Aug 2010
- Posts
- 25
- Rep Power
- 0
Or maybe something like this:
Cat A = new Cat();
Cat B = A;
- 08-06-2010, 05:35 PM #17
Member
- Join Date
- Aug 2010
- Posts
- 25
- Rep Power
- 0
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); // } } }
- 08-06-2010, 05:36 PM #18
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.one cannot pass by 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
-
getText(); problem
By The_Sponzy_Paradox in forum Java AppletsReplies: 5Last Post: 04-22-2010, 03:07 AM -
What format is a getText()?
By AJArmstron@aol.com in forum New To JavaReplies: 1Last Post: 04-18-2010, 01:43 AM -
gettext problems
By gisler in forum AWT / SwingReplies: 3Last Post: 12-01-2009, 07:35 PM -
how to getText() properly?
By javamula in forum New To JavaReplies: 12Last Post: 09-16-2009, 05:45 AM -
[SOLVED] new2java- gettext settext
By obdi in forum New To JavaReplies: 4Last Post: 07-21-2008, 09:28 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks