Hello,
I am trying to complete an online gui programming tutorial at CS124, Fall 2009, Lab 11
The code below compiles but produces these runtime errors:
java.lang.NullPointerException
at ChatRoom.<init>(ChatRoom.java:68)
at ChatRoom.main(ChatRoom.java:36)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Native MethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(De legatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:616)
at edu.rice.cs.drjava.model.compiler.JavacCompiler.ru nCommand(JavacCompiler.java:271)
line 68: sendButton.addActionListener(this);
line 36: ChatRoom room = new ChatRoom();
The code runs when line 68 is commented out.
The applicable section in the tutorial reads:
"As a final step in setting up event-handling, you have to "wire" the object that produces the event to the object that will handle the event. This is part of setting up the GUI and should be done in the constructor. To make sure that action events from the sendButton get to the ChatRoom, you can add the following line to the constructor (some time after you have created the button object):
sendButton.addActionListener(this);
In this command, "this" refers to the object that the constructor is creating, that is to the ChatRoom object itself.
You can add similar lines to set up event-handling for messageInput, sendPrivateButton, and privateMessageInput.
If you've done all this, you should be able to run the program, type something in the message input box, click the "Send" button, and see the text that you typed in the transcript."
I wonder if I have not created "button object" properly or if something else is a problem
Thank you for your help.
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class ChatRoom extends JPanel implements ActionListener { private JTextArea transcript; private JTextField messageInput; private JButton sendButton; private JTextField privateMessageInput; private JTextField recipientInput; private JButton sendPrivateButton; private JMenuItem closeCommand; //private JMenuItem clearCommand; to do //private JMenuItem getUserListCommand; to do public void actionPerformed(ActionEvent evt) { Object source = evt.getSource(); if (source == sendButton || source == messageInput) { transcript.append(">> " + messageInput.getText() + "\n"); messageInput.selectAll(); messageInput.requestFocus(); } } private JMenuBar createMenuBar() { JMenu control = new JMenu("Control"); closeCommand = new JMenuItem("Close and Quit"); closeCommand.addActionListener(this); control.add(closeCommand); JMenuBar menus = new JMenuBar(); menus.add(control); return menus; } public static void main(String[] args) { JFrame window = new JFrame("Chat Room"); ChatRoom room = new ChatRoom(); window.setContentPane(room); window.setJMenuBar(room.createMenuBar()); window.pack(); window.setLocation(20,50); window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); window.setVisible(true); } public ChatRoom () { setLayout( new BorderLayout(3,3) ); setBackground(Color.DARK_GRAY); JPanel top = new JPanel(); // subpanel for top of main panel privateMessageInput = new JTextField(35); // space for 35 chars top.add(privateMessageInput); sendPrivateButton = new JButton("Send To:"); top.add(sendPrivateButton); recipientInput = new JTextField(8); // space for 8 characters top.add(recipientInput); add(top,BorderLayout.NORTH); // add subpanel to main panel JPanel bottom = new JPanel(); // subpanel for top of main panel privateMessageInput = new JTextField(35); // space for 35 chars bottom.add(privateMessageInput); sendPrivateButton = new JButton("Send To:"); bottom.add(sendPrivateButton); recipientInput = new JTextField(8); // space for 8 characters bottom.add(recipientInput); add(bottom,BorderLayout.SOUTH); // add subpanel to main panel sendButton.addActionListener(this); transcript = new JTextArea(30,60); // 30 lines, 60 columns transcript.setWrapStyleWord(true); transcript.setLineWrap(true); transcript.setEditable(false); // user can't edit transcript add(new JScrollPane(transcript), BorderLayout.CENTER); } //end of constructor } //end of public class