0

I have wrote a simple program just to draw an image, I cannot get it to work at all. It should just show 1 picture within a pane within a frame. p.s. there are prolly imports i do not need, i tried many different things.

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Panel;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import javax.swing.*;


import javax.imageio.ImageIO;

public class ShowImage {
private Graphics g;
private BufferedImage lionImage=null;
private JFrame frame;
private JPanel totalGUI,values;

 public static void main(String[] args) {
       SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }

   });
   }
 public JPanel createContentPane(){
     totalGUI = new JPanel();


        totalGUI.setLayout(null);

        values = new JPanel();
        values.setLayout(null);
        values.setLocation(10, 10);
        values.setSize(490, 290);
        values.setBackground(Color.WHITE);
        totalGUI.add(values);
        getImage();
        Graphics g = values.getGraphics();

        g.drawImage(lionImage,100,100,null);

        totalGUI.setOpaque(true);
        return totalGUI;
 }


    private static void createAndShowGUI() {
        JFrame.setDefaultLookAndFeelDecorated(true);
        JFrame frame = new JFrame("Calculator");

        ShowImage demo = new ShowImage();
        frame.setContentPane(demo.createContentPane());

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(500, 300);
        frame.setVisible(true);

}

void getImage(){
    try{
    lionImage =ImageIO.read(new File("imgres.jpg"));// *see note
    }catch (IOException e){}
    }

}

I get the error. Which I have no idea what the problem is.

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at ShowImage.createContentPane(ShowImage.java:43)
at ShowImage.createAndShowGUI(ShowImage.java:55)
at ShowImage.access$0(ShowImage.java:50)
at ShowImage$1.run(ShowImage.java:23)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
user1817988
  • 237
  • 2
  • 5
  • 11
  • Where is the image stored? If its within the jar see [this answer](http://stackoverflow.com/questions/13796331/jar-embedded-resources-nullpointerexception/13797070#13797070). Also see [here](http://stackoverflow.com/questions/12996501/how-to-display-an-image-in-a-frame/12996718#12996718) for how to paint image to JPanel.BTW Where is your `ex.printStackTrace()` within catch block? always use it – David Kroukamp Feb 11 '13 at 16:11

2 Answers2

1

This is because your g is null. This happens when the path to the JPanel is not yet displayed on screen.

In order to draw an image inside a JPanel, you would usually create a new class that extends JPanel and override its paintComponent(Graphics g) method. See this for an example, just that you would need to call the drawImage() method.

I suggest you never catch an Exception and don't handle it in any way. You do this in the getImage() method where you try to load the image.

Community
  • 1
  • 1
Dan D.
  • 32,246
  • 5
  • 63
  • 79
  • I am using the method suggested to me by my lecturer. How do I fix the path? – user1817988 Feb 11 '13 at 12:42
  • It is either g or lionImage null. – Dan D. Feb 11 '13 at 12:59
  • 1
    @user1817988 Get a new lecturer. You should never use the getGraphics method directly. Instead create a custom component, override its paintComponent method and render the image within it. Alternatively, use a ImageIcon and apply it to a JLabel – MadProgrammer Feb 11 '13 at 20:01
1
public static void main (String [] args) throws Exception 
{
    final BufferedImage lionImage = ImageIO.read (new File ("imgres.jpg"));

    JComponent image = new JComponent () 
    {
        @Override
        protected void paintComponent (Graphics g) 
        {
            super.paintComponent (g);

            g.drawImage (lionImage, 0, 0, null);
        }

        @Override
        @Transient
        public Dimension getPreferredSize () 
        {
            return new Dimension (lionImage.getWidth (), lionImage.getHeight ());
        }
    };
    image.setOpaque (true);

    JFrame frame = new JFrame ("Image");
    frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
    frame.getContentPane ().setLayout (new BorderLayout ());
    frame.getContentPane ().add (image, BorderLayout.CENTER);
    frame.pack ();
    frame.setVisible (true);
}
Mikhail Vladimirov
  • 13,572
  • 1
  • 38
  • 40
  • A `JLabel` (with `ImageIcon` displaying the image) could do all that `JComponent` seems to be doing. `g.drawImage (lionImage, 0, 0, null);` It would also tend to paint the image using an image observer, like `this` e.g. `g.drawImage (lionImage, 0, 0, this);`. – Andrew Thompson Feb 11 '13 at 21:19