Results 1 to 9 of 9
  1. #1
    natdizzle's Avatar
    natdizzle is offline Nathan Nelson
    Join Date
    Jan 2009
    Posts
    105
    Rep Power
    0

    Default Null pointer exception on initialized variables

    -SOLVED-

    Take a look at this screenshot... my java integrated devlopment envirnment is telling me I have a null pointer on a line with three variables ALL which were initialized.

    I thought a null pointer was called when a variable doesn't get initialized.

    Null pointer exception on initialized variables-nullpointeroninitialized.png

    Edit: Here is the java file and resources being used:
    http://nathanscomputerknowledge.com/...ndareatest.zip

    Nathan
    Last edited by natdizzle; 10-10-2014 at 12:28 AM.

  2. #2
    natdizzle's Avatar
    natdizzle is offline Nathan Nelson
    Join Date
    Jan 2009
    Posts
    105
    Rep Power
    0

    Default Re: Null pointer exception on initialized variables

    Haha,

    I knew it was going to be some silly little syntax problem.

    I guess what happend was I had two rectangle array's, with similar names, one was rectangles, one was Rectangles, and I was using the one I put on there earlier and decided not to use so it was never initialized, but the one I thought I was using was, so I need to switch to the other one.

    Really confusing, they looked identicial I don't remember having two, just one.

    Thanks a lot, Nathan

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    25

    Default Re: Null pointer exception on initialized variables

    Do you have some weird reason for using Integer and not int for the variable count? And do you have some still more weird reason for assigning it a value of new Integer(0)?

    Also, JFrame does have a setSize(int, int) method, and the Graphics2D reference should be a local variable; it's never going to be used outside the painting method override.

    Then, most fields should be private or protected (apart from any public static final constants). It would be unusual for all classes in the same package to need access to all those fields at the top of your screenshot.

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

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Null pointer exception on initialized variables

    Good points Darryl. But imho, you missed the real big one (which I know you have caught in older posts). The OP is calling repaint from inside
    paintComponent(). Ouch!! @OP, you should never do that. You will overflow the call stack (or event queue).

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    25

    Default Re: Null pointer exception on initialized variables

    Quote Originally Posted by jim829 View Post
    you missed the real big one. The OP is calling repaint from inside paintComponent().
    You're right, I did miss that.

    Quote Originally Posted by jim829 View Post
    Ouch!! @OP, you should never do that.
    Never. Ever. Do. That.

    Quote Originally Posted by jim829 View Post
    You will overflow the call stack (or event queue).
    No, the coalescing of pending paint requests will prevent that, but it will still put a heavy workload on the processor, which will likely make the application (and anything else that's running) less responsive.

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

  6. #6
    natdizzle's Avatar
    natdizzle is offline Nathan Nelson
    Join Date
    Jan 2009
    Posts
    105
    Rep Power
    0

    Default Re: Null pointer exception on initialized variables

    Yeah the weird reason is initializing with Integer vesus int is it's more my style... like uniform resource locator vesus url.

    More weird reason is because I don't want to get a null pointer so I make sure I initialize.

    Last year I had a class that rotated or that used g2d's rotation method and I had to have two g2d's in my paint method for separate rotations.

    Yes I agree I would like to set fields to public / private if they can, but I never know if I'm going to use it inside or outside of the class.

    Nathan

    Edit: I'm looking at repainting with a swing timer at 77 miliseconds, works great, but a little slower. Maybe I will write a class for that.
    Last edited by natdizzle; 10-10-2014 at 04:48 AM.

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    25

    Default Re: Null pointer exception on initialized variables

    Note that assigning two G2D variables to the same Graphics doesn't create two G2D objects.

    And while premature optimization is evil, style is no excuse for grossly inefficient programming. Every time you use an Integer like an int requires boxing/unboxing.

    I don't want to get a null pointer so I make sure I initialize.
    Primitives are never null.

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

  8. #8
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Null pointer exception on initialized variables

    Quote Originally Posted by DarrylBurke View Post
    No, the coalescing of pending paint requests will prevent that, but it will still put a heavy workload on the processor, which will likely make the application (and anything else that's running) less responsive.
    Of course. I forgot about consolidating paint requests.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  9. #9
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Null pointer exception on initialized variables

    Quote Originally Posted by natdizzle View Post
    Yes I agree I would like to set fields to public / private if they can, but I never know if I'm going to use it inside or outside of the class.
    They can be declared private either way. Just use getters() and setters(). Sometimes, even using getters() and setters() can be
    beneficial inside the class if there is special handling involved.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Null pointer exception
    By gevin20 in forum New To Java
    Replies: 2
    Last Post: 03-26-2014, 12:21 PM
  2. Why I am getting null pointer exception?
    By ashvini in forum New To Java
    Replies: 6
    Last Post: 04-03-2013, 06:02 PM
  3. null pointer exception
    By Herah in forum New To Java
    Replies: 1
    Last Post: 12-01-2011, 08:44 AM
  4. Null Pointer exception (Again !!)
    By mobosecomin in forum New To Java
    Replies: 6
    Last Post: 03-29-2011, 05:04 PM
  5. Null Pointer Exception
    By demiser55 in forum New To Java
    Replies: 1
    Last Post: 09-22-2008, 06:33 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •