1

I am trying to add buttons over the images in the panels. I am also trying to switch between the panels. The program is running but when I am clicking the "instructions" button it gives a huge list of errors in the cmd. What is the issue?

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.JButton;
import javax.swing.JPanel;
public class htw10 extends JFrame
{
    final JFrame f=new JFrame("Hunt The Wumpus");
    private static final String FIRST_PANEL="first panel";
    private static final String SECOND_PANEL="second panel";
    private CardLayout cardLayout=new CardLayout();
    private JPanel content;

    public void start()
    {

        // Create a new panel, make 'content'  refer to it
    content = new JPanel();

    // Set the content pane of the window to the panel we just created
    f.setContentPane(content);

    // Create a button group and some buttons


    // Set the layout of the content panel and add buttons
    content.setLayout(new FlowLayout());

    // Create and add the intro panel and instruction panel to the content panel
    content.add(introPanel(),FIRST_PANEL);
    content.add(instructionPanel(),SECOND_PANEL);

    f.setSize(750,500);
    f.setDefaultCloseOperation(EXIT_ON_CLOSE);
    f.setVisible(true);


}


    private JPanel instructionPanel()
        {
            JPanel secondPanel=new JPanel();
            ImageIcon icon=new ImageIcon("img2.jpg");
            JLabel pic2 = new JLabel(icon);
            secondPanel.add(pic2);
        JButton b1=new JButton("Back");
        content.add(b1);
        b1.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    cardLayout.show(content,FIRST_PANEL);
                }
            });
        secondPanel.repaint();
            return secondPanel;
        }
        public JPanel introPanel()
    {
            JPanel iPanel=new JPanel();
        ImageIcon icon=new ImageIcon("img1.jpg");
            JLabel picLabel = new JLabel(icon);
            iPanel.add(picLabel);

        ButtonGroup group=new ButtonGroup();
            JButton b1=new JButton("Instructions");
            JButton b2=new JButton("Play");
            JButton b3=new JButton("Exit");
        picLabel.add(b1);
        //f.getContentPane().add(picLabel,BorderLayout.SOUTH);
        content.add(b1);
            content.add(b2);
            content.add(b3);
        // Add a listener to the 'Instructions' button
            // so that the cardLayout is shown when the button is clicked
        b1.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    cardLayout.show(content,SECOND_PANEL);
                }
            });
        iPanel.repaint(); 
            return iPanel;
    }
    public static void main(String args[])throws Exception
    {
        htw10 obj=new htw10();
        obj.start();
    }

}
Community
  • 1
  • 1
Megha Verma
  • 141
  • 1
  • 3
  • 12
  • Carefully look at the stack trace of the `NullPointerException`. It tells you exactly where in your code the exception happens. Go to that line of code and figure out what is `null` there. Note that you get a `NullPointerException` if you try to call a method on a variable that is `null`. – Jesper Oct 13 '16 at 14:11
  • `c:\jsdk>java htw10 Exception in thread "main" java.lang.NullPointerException at htw10.start(htw10.java:25) at htw10.main(htw10.java:69)` i am not able to find anything null... – Megha Verma Oct 13 '16 at 14:15
  • If line 25 is the line: `content.setLayout(new FlowLayout());` then that means that `content` is `null`. Now it's your job to find out why. – Jesper Oct 13 '16 at 14:20
  • Looking at your code, you are not initializing `content` anywhere. In line 14 you declare the member variable but it is `null` because you did not initialize it. – Jesper Oct 13 '16 at 14:21
  • how will i initialize content? – Megha Verma Oct 13 '16 at 14:23
  • actually i am new to java swing and i am getting very confused... – Megha Verma Oct 13 '16 at 14:23

2 Answers2

0

I don't think Jpanel has drawImage method which you are trying to call in below code.

public JPanel introPanel()
{
    JPanel iPanel=new JPanel();
    ImageIcon icon=new ImageIcon("img1.jpg");
    iPanel.drawImage(icon, 0, 0,getWidth(),getHeight(),this);
    return iPanel;
}

You need a graphics(java.awt.Graphics) object to call drawImage method.

More-over you can try other methods like

public JPanel introPanel()
{
    JPanel iPanel=new JPanel();
    ImageIcon icon=new ImageIcon("img1.jpg");
    JLabel picLabel = new JLabel(icon);
    iPanel.add(picLabel);
    iPanel.repaint();
    return iPanel;
}
WitVault
  • 23,445
  • 19
  • 103
  • 133
  • hey your answer worked....but now its giving a runtime error...... c:\jsdk>javac htw10.java c:\jsdk>java htw10 Exception in thread "main" java.lang.NullPointerException at htw10.start(htw10.java:25) at htw10.main(htw10.java:70) – Megha Verma Oct 05 '16 at 17:47
  • Ya...the program and the image files are kept in the same folder – Megha Verma Oct 07 '16 at 17:06
  • `c:\jsdk>java htw10 Exception in thread "main" java.lang.NullPointerException at htw10.start(htw10.java:26) at htw10.main(htw10.java:76)` plzzz help me with this error....... – Megha Verma Oct 08 '16 at 17:43
  • @MeghaVerma can you please update the question with new code where you are getting error. – WitVault Oct 12 '16 at 13:36
0

In line 14, you are declaring a member variable content, but you are not initializing it. Member variables will be automatically initialized to null if you do not initialize them yourself:

private JPanel content;   // is automatically set to null

In line 25 you are calling the method setLayout on content:

content.setLayout(new FlowLayout());

This will cause a NullPointerException because content is null.

To learn more about what a NullPointerException is and why it happens, see: What is a NullPointerException, and how do I fix it?

You need to set content to something. It appears that this is supposed to refer to the content pane. Furthermore, you are calling the introPanel() method multiple times, causing multiple instances of this panel to be created. That is not what you want. That panel should be created only once, and then you should use that one. Don't call introPanel() multiple times. Your start() method should look something like this:

public void start()
{
    // Create a new panel, make 'content'  refer to it
    content = new JPanel();

    // Set the content pane of the window to the panel we just created
    f.setContentPane(content);

    // Create a button group and some buttons
    ButtonGroup group=new ButtonGroup();
    JButton b1=new JButton("Instructions");
    JButton b2=new JButton("Play");
    JButton b3=new JButton("Exit");

    // Set the layout of the content panel and add buttons
    content.setLayout(new FlowLayout());
    content.add(b1);
    content.add(b2);
    content.add(b3);

    // Create and add the intro panel and instruction panel to the content panel
    content.add(introPanel(),FIRST_PANEL);
    content.add(instructionPanel(),SECOND_PANEL);

    f.setSize(750,360);
    f.setDefaultCloseOperation(EXIT_ON_CLOSE);
    f.setVisible(true);

    // Add a listener to the 'Instructions' button
    // so that the cardLayout is shown when the button is clicked
    b1.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            cardLayout.show(content,SECOND_PANEL);
        }
    }); 
}
Community
  • 1
  • 1
Jesper
  • 202,709
  • 46
  • 318
  • 350
  • your suggestion worked.....but now i want to place the buttons over the image in introPanel()....i have changed the code a little bit....but either the buttons come beside the image or they disappear.... – Megha Verma Oct 13 '16 at 17:06
  • `public JPanel introPanel() { JPanel iPanel=new JPanel(); ImageIcon icon=new ImageIcon("img1.jpg"); JLabel picLabel = new JLabel(icon); iPanel.add(picLabel); ButtonGroup group=new ButtonGroup(); JButton b1=new JButton("Instructions"); JButton b2=new JButton("Play"); JButton b3=new JButton("Exit"); picLabel.add(b1); iPanel.repaint(); return iPanel; }` – Megha Verma Oct 13 '16 at 17:10