Results 1 to 9 of 9
  1. #1
    aizen92 is offline Senior Member
    Join Date
    Nov 2010
    Posts
    155
    Rep Power
    5

    Default graphics null pointer exception

    Hello,

    Iam trying to make a program that displays 2 eyes, and according to the mouse movement to look up, down, left, or right
    i am first drawing the eyes, here is my code
    Java Code:
    import java.awt.*;
    import javax.swing.*;
    import javax.swing.event.*;
    
    public class EyeGUI
    {
        private JFrame frame;
    
        private JPanel eyePanel;
    
        public EyeGUI ()
        {
            frame = new JFrame("The eyes have it");
    
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setSize(new Dimension(280, 200));
    
            eyePanel = new JPanel ();
            eyePanel.setBackground(Color.GRAY);
            Graphics g = eyePanel.getGraphics();
            
            g.setColor(Color.BLACK);
            g.drawOval(40, 50, 100, 100);
            g.drawOval(140, 50, 100 ,100);
            
            frame.setVisible(true);
        }
    
        public static void main (String[] args)
        {
            new EyeGUI ();
        }
    }
    the thing im having is it is highlighting on the first g. statement, fromg.setColor(..) i removed it and then it highlighted over g.drawOval(...) with a null pointer exception

    what could be my problem, did i miss to call a method i should have or what?

    Thanks in advance

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,770
    Blog Entries
    7
    Rep Power
    21

    Default

    Extend a JPanel and override its paintComponent(Graphics g) method; do all your drawing in that method. That way the EDT (Event Dispatch Thread) decides when your component needs to be drawn and it creates the correct Graphics object for you.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    aizen92 is offline Senior Member
    Join Date
    Nov 2010
    Posts
    155
    Rep Power
    5

    Default

    Okay,
    Thanks Jos for the help

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default

    And never use getGraphics() of a Component in client code.

    db

  5. #5
    aizen92 is offline Senior Member
    Join Date
    Nov 2010
    Posts
    155
    Rep Power
    5

    Default

    @Jos: should I like create a new class for constructing the shapes, and extend it JPanel and then create its constructor in my code?
    If yes, in the class which extends JPanel i should only write the codes for the paintComponent, right?

    @db: Lol, i just learnt my lesson now
    Thnx for the help

  6. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,770
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by aizen92 View Post
    @Jos: should I like create a new class for constructing the shapes, and extend it JPanel and then create its constructor in my code?
    If yes, in the class which extends JPanel i should only write the codes for the paintComponent, right?
    Just extend a JPanel as follows:

    Java Code:
    public class EyePanel extends JPanel {
    
       // your constructors go here
    
       // override this method:
       public void paintComponent(Graphics g) {
          // draw the eye here
       }
    }
    ... and instantiate an EyePanel where you're now instantiating an ordinary JPanel.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    aizen92 is offline Senior Member
    Join Date
    Nov 2010
    Posts
    155
    Rep Power
    5

    Default

    I did as Jos said, and heres my code

    Java Code:
    import java.awt.*;
    import java.awt.image.*;
    import javax.swing.*;
    import javax.swing.event.*;
    
    public class EyeGUI extends JPanel
    {
        private JFrame frame;
    
        private JPanel eyePanel;
        
        private Graphics g;
    
        public EyeGUI ()
        {
            frame = new JFrame("The eyes have it");
    
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setSize(new Dimension(280, 200));
    
    //         BufferedImage image = new BufferedImage (280, 200, BufferedImage.TYPE_INT_ARGB);
    //         g = image.getGraphics();
    //         g.setColor(Color.WHITE);
    //         g.fillOval(25, 35, 115, 115);
    //         g.fillOval(140, 35, 115, 115);
    //         g.setColor(Color.BLACK);
    //         g.drawOval(25, 35, 115, 115);
    //         g.drawOval(140, 35, 115, 115);
    //         g.setColor(Color.BLACK);
    //         g.fillOval(70, 80, 20, 20);
    //         g.fillOval(170, 80, 20, 20);
    //         
    //         JLabel lbl = new JLabel ();
    //         lbl.setIcon(new ImageIcon(image));
    //         
             eyePanel = new JPanel ();
    //         eyePanel.add(lbl);
            Graphics g = eyePanel.getGraphics();
            paintComponent(g);
            frame.add(eyePanel);
            
            frame.pack();
            
            frame.setVisible(true);
        }
        
        public void paintComponent (Graphics g)
        {
            g.setColor(Color.WHITE);
             g.fillOval(25, 35, 115, 115);
             g.fillOval(140, 35, 115, 115);
             g.setColor(Color.BLACK);
             g.drawOval(25, 35, 115, 115);
             g.drawOval(140, 35, 115, 115);
        }
    
        public static void main (String[] args)
        {
            new EyeGUI ();
        }
    }
    but still im getting he null pointer error at the g.setColor(...) in the paintComponent method

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    You're still using getGraphics, what Darryl told you not to do! And you're calling paintComponent directly, something you should never do. Please read the graphics tutorials as they will show you how to do this correctly since your assumptions are incorrect (as all of ours are when we start), and you need to start from ground up to learn Swing graphics (we all do).

    For e.g., please check out:

    Painting in Swing
    2D Graphics

    Click on the links above for two good tutorials on this subject.
    Last edited by Fubarable; 03-27-2011 at 05:07 PM.

  9. #9
    aizen92 is offline Senior Member
    Join Date
    Nov 2010
    Posts
    155
    Rep Power
    5

    Default

    ohhhh, i misread Jos example code, i though of it as my main gui class, i didnt notice whats wrong with it until now.
    @Fubarable, i'll read them and then continue the project
    Thnx

Similar Threads

  1. Null Pointer Exception
    By jonytek in forum New To Java
    Replies: 5
    Last Post: 03-02-2011, 08:16 AM
  2. Null pointer exception
    By samuel.roshni in forum Java Servlet
    Replies: 14
    Last Post: 01-22-2011, 03:25 PM
  3. Null pointer exception
    By talha06 in forum JDBC
    Replies: 5
    Last Post: 07-14-2009, 02:12 AM
  4. Null Pointer Exception
    By andre1011 in forum Advanced Java
    Replies: 4
    Last Post: 02-07-2009, 04:30 AM
  5. getting a null pointer exception
    By Rjava in forum XML
    Replies: 4
    Last Post: 07-16-2008, 06:56 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
  •