9 Replies Latest reply: Jan 6, 2005 9:33 PM by 807596 RSS

    Null Pointer Exception

    807596
      Does anyone understand why I get A null poointer exception on the fill oval line?
      import java.awt.*;
      import javax.swing.*;
      
      public class Main 
      {
          public static void main(String[] args) 
          {    
              JFrame k = new JFrame();
              Graphics g = k.getGraphics();
              k.setSize(500, 500);
              k.setLocation(10,10);
              k.setVisible(true);
              k.setDefaultCloseOperation(k.EXIT_ON_CLOSE);
              g.fillOval(100 , 100 , 5, 5);
          }    
      }
        • 1. Re: Null Pointer Exception
          807596
          try this!!!
          import java.awt.*;
          import javax.swing.*;
           
          public class Main 
          {
              public static void main(String[] args) 
              {    
                  JFrame k = new JFrame();
                  //Graphics g = k.getGraphics();  this is the reason why you have an exception, 
                   //because you need to have a frame first before you get its graphics resources
                  k.setSize(500, 500);
                  k.setLocation(10,10);
                  
                  k.setVisible(true);
                  k.setDefaultCloseOperation(k.EXIT_ON_CLOSE);
                  Graphics g = k.getGraphics(); // that's why you should put this code here,
                  g.fillOval(100 , 100 , 5, 5);
                  
              }    
          }
          • 2. Re: Null Pointer Exception
            807596
            Thanks that works great!
            • 3. Re: Null Pointer Exception
              807596
              > //Graphics g = k.getGraphics(); this is the reason why you have an exception,
              //because you need to have a frame first before you get its graphics resources

              Doesn't he "have a frame" from the previous line - JFrame k = new JFrame();
              • 4. Re: Null Pointer Exception
                794342
                Doesn't he "have a frame" from the previous line -
                JFrame k = new JFrame();
                it has to be visible
                • 5. Re: Null Pointer Exception
                  807596
                  Description on getGraphics() method in the API
                  Creates a graphics context for this component. This method will return null if this component is currently not displayable.
                  • 6. Re: Null Pointer Exception
                    807596
                    > . . . you need to have a frame
                    "It depends to how you define what is is."
                         William Jefferson Clinton
                    • 7. Re: Null Pointer Exception
                      807596
                      try this!!!
                      What you have done is utterly pointless. The OP asked a question, he didn't ask for code. If you simply must post code, and I advise against it, then why not explain what the OP was doing wrong, and how you fixed it. You are not helping this person's learning by giving them the answers.
                      • 8. Re: Null Pointer Exception
                        807596
                        try this!!!
                        If you simply must post code, and I advise against it, then
                        why not explain what the OP was doing wrong, and how
                        you fixed it.
                        Try looking at the code that angeles1016 posted. It's the OP's original code with one line moved plus comments explaining what the OP did wrong and how to fix it
                        • 9. Re: Null Pointer Exception
                          807596
                          I don't like this code. The graphics is accessed from the main thread and not the event dispatch thread.
                          I think you should make this call using SwingUtilities.invokeLater(...) in order to run this code. Maybe I am wrong but I don't think this is a threadsafe method.

                          Mike