Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: problem completing beginners' gui tutorial

  1. #1
    Junior Member
    Join Date
    Mar 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default solved: problem completing beginners' gui tutorial

    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
    Last edited by mechnik; March 18th, 2011 at 03:55 PM.


  2. #2
    Junior Member
    Join Date
    Mar 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: problem completing beginners' gui tutorial

    problem solved by prepending
    sendButton = new JButton();
    before
    sendButton.addActionListener(this);

Similar Threads

  1. Replies: 21
    Last Post: December 27th, 2013, 02:33 AM
  2. [SOLVED] Problem with a tutorial program(Adding the answer of two squared numbers together)
    By Melawe in forum What's Wrong With My Code?
    Replies: 20
    Last Post: April 7th, 2010, 09:03 AM
  3. Tutorial requests
    By Json in forum The Cafe
    Replies: 9
    Last Post: August 1st, 2009, 03:14 PM
  4. Store data in database using JSP
    By abhi4jdk in forum Java Servlet
    Replies: 1
    Last Post: May 18th, 2009, 04:42 AM
  5. [SOLVED] Books and sources for Java beginners
    By chronoz13 in forum Java Theory & Questions
    Replies: 1
    Last Post: April 15th, 2009, 08:36 AM