Results 1 to 5 of 5
  1. #1
    MonkeyMan is offline Member
    Join Date
    Jan 2012
    Posts
    45
    Rep Power
    0

    Default Line2D, getting NullPointerException

    Heya I'm attempting to draw a line using Line2D and Point2D

    Here's the problem part of my code:

    Java Code:
        public void paint(Graphics g){
    
            Graphics2D ga = (Graphics2D)g;
    
            System.out.println(point1.getX()+ " " + point1.getY());
            System.out.println(point2.getX()+ " " + point2.getY());
    
            Line2D line1 = new Line2D.Double(point1, point2);
    
            System.out.println(line1.getX1() + " " + line1.getY1() + " " + line1.getX2() + " " + line1.getY2());
    
            ga.draw(line1);
        }
    And I get this output:

    100.0 100.0
    150.0 150.0

    Exception in thread "main" java.lang.NullPointerException

    100.0 100.0 150.0 150.0

    at test.myClass.paint(myClass.java:65)
    at test.Main.main(Main.java:35)[/CODE]

    I put the print lines to test that the line has all 4 points stored in it before attempting to draw it, yet I keep getting a NullPointerException.

  2. #2
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default Re: Line2D, getting NullPointerException

    which line is line 65?

    Quote Originally Posted by MonkeyMan
    Exception in thread "main" java.lang.NullPointerException
    at test.myClass.paint(myClass.java:65)

  3. #3
    MonkeyMan is offline Member
    Join Date
    Jan 2012
    Posts
    45
    Rep Power
    0

    Default Re: Line2D, getting NullPointerException

    Line 12, the one trying to do the drawing.

  4. #4
    Borkpaladin is offline Member
    Join Date
    Mar 2012
    Posts
    8
    Rep Power
    0

    Default Re: Line2D, getting NullPointerException

    I think your problem is that the Graphics object being passed into the paint method is null, if you call this method just with 'repaint();' it should work.
    To check if this is the problem simply add 'if (g == null)System.out.print("Graphics object Null");'

    EDIT: Actually I'm pretty sure you have to do this: super.paint(g); before you use it to initialise 'ga'
    Last edited by Borkpaladin; 04-03-2012 at 04:16 AM.

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default Re: Line2D, getting NullPointerException

    1. Never use getGraphics() of a Component. Not until you have a very good uncerstainding of AWT and Swing, anyhow.
    2. Never call paint() in code. As Borkpaladin has hinted, the correct way to get a painting method to run is to call repaint().
    3. For Swing components, the method to override is paintComponent(...), not paint(...).

    Recommended reading: Lesson: Performing Custom Painting (The Java™ Tutorials > Creating a GUI With JFC/Swing)

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Replies: 26
    Last Post: 03-12-2012, 05:11 PM
  2. NullPointerexception
    By danewithoutwax in forum New To Java
    Replies: 4
    Last Post: 11-26-2011, 12:51 PM
  3. NullPointerException
    By Diz in forum New To Java
    Replies: 10
    Last Post: 05-13-2011, 02:58 AM
  4. NullPointerException
    By jayragz in forum NetBeans
    Replies: 5
    Last Post: 05-12-2011, 05:19 PM
  5. NullPointerException
    By mjz in forum JDBC
    Replies: 1
    Last Post: 08-06-2009, 11:46 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
  •