Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Graphics2D Exception

  1. #1
    Junior Member
    Join Date
    Mar 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Graphics2D Exception

    Graphics2D  graphics;
     
    graphics.fillOval(x1,y1,x2,y2);

    The .java program compiles with no errors.
    However. when I try to run it I get a long list of exceptions when it comes to the above code,
    headed by

    Exception in thread "AWT-EventQueue-0" java.lang Null PointerException .... and much more.

    Any help will be appreciated.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,372
    Thanks
    183
    Thanked 836 Times in 779 Posts
    Blog Entries
    5

    Default Re: Graphics2D Exception

    Not enough information to pinpoint the issue aside from a) given that graphics is not instantiated in the two lines of code you posted, it is assigned the default value of null b) if the graphics object is null it is suggestive that you are doing custom painting incorrectly. My advice a) add some println's to figure out the variable that is null, and then backtrack to determine why it is null b) post an SSCCE so your code can be more thoroughly evaluated.

  3. #3
    Junior Member
    Join Date
    Mar 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Graphics2D Exception

    Quote Originally Posted by copeg View Post
    Not enough information to pinpoint the issue aside from a) given that graphics is not instantiated in the two lines of code you posted, it is assigned the default value of null b) if the graphics object is null it is suggestive that you are doing custom painting incorrectly. My advice a) add some println's to figure out the variable that is null, and then backtrack to determine why it is null b) post an SSCCE so your code can be more thoroughly evaluated.
    Herewith relevant code.
     
    class Jexp3 extends JPanel implements MouseListener
    {
      int  mouseX, mouseY;
      int width, height;
      Graphics2D graphics;
      Color color;
      public Jexp3()
      {
            super();
            setLayout(null);
            addMouseListener(this);
      }        
      public void mouseClicked(MouseEvent m){
          mouseX = m.getX();
          mouseY = m.getY();
          color = Color.red;
          width = (int)getSize().getWidth();
          height = (int)getSize().getHeight();
          graphics.fill (new Ellipse2D.Float (mouseX, mouseY, 25, 25));
          System.out.println("Mouse Clicked "+ mouseX + " " + mouseY);
      }

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,310
    Thanks
    56
    Thanked 2,373 Times in 2,344 Posts

    Default Re: Graphics2D Exception

    Where is the variable: graphics given a value? It's default value is null.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: Graphics2D Exception

    And (besides Norm answer) I add another important issue: the mouseClicked (or any other event handling method) is not the appropriate moment/location where you can make a draw (except situations where you know exactly what are you doing).
    Because the only valid and real paint "context" is (for Swing components) the paintComponent(Graphics) method.
    Andrea, www.andbin.netSCJP 5 (91%) – SCWCD 5 (94%)

    Useful links for Java beginnersMy new project Java Examples on Google Code

  6. #6
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,372
    Thanks
    183
    Thanked 836 Times in 779 Posts
    Blog Entries
    5

    Default Re: Graphics2D Exception

    I recommend reading: Lesson: Performing Custom Painting (The Java™ Tutorials > Creating a GUI With JFC/Swing) for info on how to properly perform custom painting

Similar Threads

  1. Multiline text drawing with Graphics2D?
    By ericmacau in forum Java SE APIs
    Replies: 1
    Last Post: August 20th, 2013, 08:17 AM
  2. Force Sync on Graphics2D?
    By Gerp in forum Java Theory & Questions
    Replies: 2
    Last Post: April 13th, 2011, 10:13 PM
  3. question about transforming Graphics2D
    By gib65 in forum AWT / Java Swing
    Replies: 1
    Last Post: October 7th, 2010, 10:53 PM
  4. java graphics2d issue
    By nana-j13 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: September 15th, 2010, 04:49 PM
  5. Replies: 1
    Last Post: November 14th, 2008, 03:00 PM