Results 1 to 3 of 3

Thread: GUI Image error

  1. #1
    HarleyRowland is offline Member
    Join Date
    Feb 2013
    Posts
    26
    Rep Power
    0

    Default GUI Image error

    Hello, keep getting this error:

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException

    with this part of my code
    Java Code:
      public void playGame()
        {   
            frame.remove(mainPanel);
            
            JPanel gamePanel = new JPanel();
            JPanel paneGameCenter;
            JLabel gameLabel;
            JLabel imageLabel;
            JPanel paneWest;
            JPanel paneEast;
            JPanel gap;
            ImageIcon image = new ImageIcon(getClass().getResource("image.jpg"));
            imageLabel = new JLabel(image);
            
            JButton menuButton = new JButton();
            
            bareFrame();
            
            Container game = frame.getContentPane();
            game.setLayout(new BorderLayout());
            
            game.add(gamePanel);
            gamePanel.add(paneGameCenter = new JPanel(new GridLayout(7,1)), BorderLayout.CENTER);
                paneGameCenter.add(imageLabel);
            
           
            menuButton.addActionListener(new ActionListener() {
                                   public void actionPerformed(ActionEvent e) { 
                                        frame.setVisible(false);
                                        mainMenu();
                                    }
                               });
            
            frame.pack();
            frame.setVisible(true); 
        }
    I think its to do with the image.jpg part, any ideas what i'm doing wrong?

    Thanks

  2. #2
    HarleyRowland is offline Member
    Join Date
    Feb 2013
    Posts
    26
    Rep Power
    0

    Default Re: GUI Image error

    Java Code:
    public void playGame()
        {   
            frame.remove(mainPanel);
            
            JPanel gamePanel = new JPanel();
            JPanel paneGameCenter;
            JLabel gameLabel;
            JLabel imageLabel;
            JPanel paneWest;
            JPanel paneEast;
            JPanel gap;
            ImageIcon image = new ImageIcon(picture(100,200,"image.jpg"));
            imageLabel = new JLabel(image);
            
            JButton menuButton = new JButton();
            
            bareFrame();
            
            Container game = frame.getContentPane();
            game.setLayout(new BorderLayout());
            
            game.add(gamePanel);
            gamePanel.add(paneGameCenter = new JPanel(new GridLayout(7,1)), BorderLayout.CENTER);
                paneGameCenter.add(imageLabel);
            
           
            menuButton.addActionListener(new ActionListener() {
                                   public void actionPerformed(ActionEvent e) { 
                                        frame.setVisible(false);
                                        mainMenu();
                                    }
                               });
            
            frame.pack();
            frame.setVisible(true); 
        }
        
        public Image picture(int x, int y, String filename) {
            ImageIcon icon = new ImageIcon(filename);
            Image image = icon.getImage();
            return image;
        }
    Also tried this, it still doesn't work, I don't get an image now but no image appears on the GUI

    Thanks

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,777
    Rep Power
    5

    Default Re: GUI Image error

    Since I can't see all your code, here is an example of how to display an image on a
    JLabel which is in a JPanel.

    Java Code:
    public class Snippet {
       public static void main(String[] args) {
          JFrame frame = new JFrame();
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.setPreferredSize(new Dimension(400, 400));
          ImageIcon icon = new ImageIcon("path_to_your_image_here");
          JLabel label = new JLabel(icon);
          JPanel panel = new JPanel();
          panel.add(label);
          frame.add(panel);
          frame.pack();
          frame.setLocationRelativeTo(null);
          frame.setVisible(true);
       }
    }
    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Error displaying an Image!
    By javabeginner29 in forum Java Applets
    Replies: 4
    Last Post: 02-15-2012, 06:21 PM
  2. Error: cannot load image .
    By iswan in forum Java Applets
    Replies: 1
    Last Post: 09-29-2011, 02:26 AM
  3. image insertion error
    By KiranA in forum New To Java
    Replies: 16
    Last Post: 07-20-2011, 05:33 PM
  4. Error in saving an Image????
    By LankanSniper in forum Java 2D
    Replies: 4
    Last Post: 11-07-2009, 04:48 AM
  5. error while loading the image from the server
    By testtest in forum CLDC and MIDP
    Replies: 1
    Last Post: 09-23-2008, 05:16 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •