0

I am in the midst of building a Java program that allows the changing of an image based on a radial button click in the menu bar. I believe that most of the code is accurate, however, I have hit a speed bump. How can I find out why I'm hitting so many errors?

import java.awt.*;
import java.awt.event.*;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.ButtonGroup;
import javax.swing.JMenuBar;
import javax.swing.KeyStroke;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.*;

public class MenuAssignment extends JFrame implements ActionListener {

    // Images, taken from the official Java Documentation
    static String catString = "Cat";
    static String dogString = "Dog";
    
    // Setup label
    JLabel picture;
    ImageIcon image;
    
    // Radio buttons
    private JRadioButtonMenuItem catSelect;
    private JRadioButtonMenuItem dogSelect;

    public MenuAssignment() {
        
        // Start with cat image
        image = new ImageIcon("images/" + catString + ".gif");
        picture.setIcon(image);
    }
    
    private void createMenuBar() {
        JMenuBar menuBar;
        JMenu menu, submenu;
        JMenuItem menuItem;
        JRadioButtonMenuItem rbMenuItem;

        //Create the menu bar.
        menuBar = new JMenuBar();
        
        //Build the first menu.
        menu = new JMenu("Animals");
        menuBar.add(menu);
        
        image = new ImageIcon("images/Dog.gif");       
        picture = new JLabel(image);
        add(picture);
        
        // Create group of radio buttons
        ButtonGroup group = new ButtonGroup();
        
        // Cat Button
        catSelect = new JRadioButtonMenuItem("Cat");
        catSelect.setActionCommand("Cat");
        catSelect.setSelected(true);
        
        // Dog Button
        dogSelect = new JRadioButtonMenuItem("Dog");
        dogSelect.setActionCommand("Dog");
        
        // Listen for Option
        catSelect.addActionListener(this);
        dogSelect.addActionListener(this);
        
        // return menuBar;
    }
    
    public void actionPerformed(ActionEvent e) {
      image = new ImageIcon("images/" + e.getActionCommand() + ".gif");
      picture.setIcon(image);
      System.out.println(e.getActionCommand());
  }
  
    public static void main(String[] args) {

    EventQueue.invokeLater(new Runnable() {
            public void run() {
                MenuAssignment start = new MenuAssignment();
                start.setVisible(true);
            }
        });
    }
}

Errors:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at MenuAssignment.<init>(MenuAssignment.java:33)
    at MenuAssignment$1.run(MenuAssignment.java:82)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:715)
    at java.awt.EventQueue.access$400(EventQueue.java:82)
    at java.awt.EventQueue$2.run(EventQueue.java:676)
    at java.awt.EventQueue$2.run(EventQueue.java:674)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:86)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:685)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:296)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:211)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:196)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:188)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
halfer
  • 19,824
  • 17
  • 99
  • 186
JDev
  • 5,168
  • 6
  • 40
  • 61
  • *"I have hit a speed bump. Could someone please help me out with my program and why I'm hitting so many errors"* ... like what (errors)? – MadProgrammer Apr 16 '15 at 02:37
  • MadProgrammer: errors added – JDev Apr 16 '15 at 02:41
  • possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – Dennis Meng Apr 16 '15 at 05:02

1 Answers1

2
  1. Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException caused by the fact the picture is not initialised before you use it...
  2. createMenuBar is never called, so nothing is added to the screen
  3. menuBar is never assigned to the frame (setJMenuBar), so it won't appear
  4. The menu and catSelect and dogSelect are never added to menu
  5. The "cat" menu item is selected by default, which doesn't match with what's on the screen
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366