tags:

views:

103

answers:

2

I want to change the image a JLabel is viewing during runtime, but I keep getting NullPointerExceptions or nothing happens when I press the magic button that's supposed to do things. Is it even possible in Java?

Here is my code in its entirety:

    import java.text.*;
    import javax.swing.text.*;
    import java.util.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.event.*;

    public class Shell implements ActionListener, MenuKeyListener
    {
        JFrame frame;
        JWindow window;
        JButton PSubmit;
        JPanel pane1, pane2;
        JRadioButton R1, R2, R3;
            ButtonGroup PGroup;
        JTabbedPane layout;

        String result;
        String border = "Border.png";
        String DF = "Frame.png";
        String list [];
        Driver driver;

        public Shell()
        {
            driver = new Driver();
            list = new String [6];
        }

        public void setFrame()
        {
            frame = new JFrame("Pokemon Program 3 by Systems Ready");
            frame.setSize(600, 600);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true);
            frame.getContentPane().setLayout(new BorderLayout());
        }
        public void frameLayout()
        {
            layout = new JTabbedPane();
            JPanel pane1 = new JPanel();
            JPanel pane2 = new JPanel();
            JLabel label = new JLabel("Please choose the restrictions:");
            JLabel imgLabel1 = new JLabel(new ImageIcon(border));
            JLabel notiLabel1 = new JLabel("The Pokemon chosen with these restrictions are:       ");
            JLabel notiLabel2 = new JLabel("'No Restrictions': No restrictions for the kind of Pokemon chosen based on species or items.");
            JLabel notiLabel3 = new JLabel("'Battle Revolution': All Pokemon must have unique items.");
            JLabel notiLabel4 = new JLabel("'Battle Tower': All Pokemon must have unique items, Uber and Event Legendaries banned.");
            JLabel label2 = new JLabel("Please choose possible Pokemon:");
            pane1.add(label);
            pearlButtons();
            pane1.add(R1);
            pane1.add(R2);
            pane1.add(R3);
            pane1.add(PSubmit);
            pane1.add(notiLabel2);
            pane1.add(notiLabel3);
            pane1.add(notiLabel4);
            pane1.add(imgLabel1);
            pane1.add(notiLabel1);
            JLabel pokeLabel1 = new JLabel(new ImageIcon(DF));
            JLabel pokeLabel2 = new JLabel(new ImageIcon(DF));
            JLabel pokeLabel3 = new JLabel(new ImageIcon(DF));
            JLabel pokeLabel4 = new JLabel(new ImageIcon(DF));
            JLabel pokeLabel5 = new JLabel(new ImageIcon(DF));
            JLabel pokeLabel6 = new JLabel(new ImageIcon(DF));
            pane1.add(pokeLabel1);
            pane1.add(pokeLabel2);
            pane1.add(pokeLabel3);
            pane1.add(pokeLabel4);
            pane1.add(pokeLabel5);
            pane1.add(pokeLabel6);
            pane2.add(label2);
            layout.add("Pearl Version", pane1);
            layout.add("SoulSilver Version", pane2);
            frame.add(layout);
        }
        public void pearlButtons()
        {
            PGroup = new ButtonGroup();
            R1 = new JRadioButton("No Restrictions", true);
            R1.setActionCommand("N");
            R1.setVisible(true);
            R2 = new JRadioButton("Battle Revolution");
            R2.setActionCommand("BR");
            R2.setVisible(true);
            R3 = new JRadioButton("Battle Tower");
            R3.setActionCommand("B");
            R3.setVisible(true);
            PGroup.add(R1);
            PGroup.add(R2);
            PGroup.add(R3);
            PSubmit = new JButton("Submit");
            PSubmit.setActionCommand("pstart");
            PSubmit.setVisible(true);
            PSubmit.addActionListener(this);
        }
        public void pearlProcessing()
        {
                    //The "list" array has a bunch of string names that get .png affixed to them (and I named the image files as such when I name them)
            String file1 = list[0] + ".png";
            String file2 = list[1] + ".png";
            String file3 = list[2] + ".png";
            String file4 = list[3] + ".png";
            String file5 = list[4] + ".png";
            String file6 = list[5] + ".png";
    /*-------------------------------------------------------------------------------//
                    This is where the method's supposed to go to change the image...
                    I've tried pokeLabel = new JLabel(new ImageIcon(file1));, but that yields a NullPointerException.
//-----------------------------------------------------------------------------------*/
        }
        public static void main(String[] args)
        {
            Shell test = new Shell();
            test.setFrame();
            test.frameLayout();
            test.frame.setVisible(true); 
        }
        public void actionPerformed(ActionEvent e)
        {
            if ("pstart".equals(e.getActionCommand()))
            {
                result = PGroup.getSelection().getActionCommand();
                if (result.equals("N"))
                {
                    list = driver.Prandom();
                    pearlProcessing();
                }
                else
                    System.out.println("Not done yet! ;)");
            }
        } 

        public void menuKeyPressed(MenuKeyEvent e)  
        {
            System.out.println("pressed");
        }
        public void menuKeyReleased(MenuKeyEvent e)
        {
            System.out.println("menuKeyReleased");
        }
        public void menuKeyTyped(MenuKeyEvent e)
        {
            System.out.println("menuKeyTyped");
        }

    }
+2  A: 

Without knowing if this is the cause, I would change

 result = PGroup.getSelection().getActionCommand();
 if (result.equals("N")) {

to

 ButtonModel selection = PGroup.getSelection();
 result = (selection==null ? null : selection.getActionCommand()); 
 if ("N".equals(result)) {
 // etc...

This guards against likely null pointers.

mdma
A: 

I highly suggest whenever you get any exception to always look at the stack trace and see where the actual line the exception occurred at was.

But if I were to speculate, you call PGroup.getSelection() in your actionPerformed method. This method may return null if there is no button selected in your radio button group. I also noticed that you do not set an initially selected radio button (call setSelected(true) on the radio button). Thus, if the selected radio button is null calling getActionCommand will result in a NullPointerException.

Avrom