Results 1 to 10 of 10
  1. #1
    victoryo is offline Member
    Join Date
    Dec 2011
    Posts
    16
    Rep Power
    0

    Default Nullpointer accessing public int

    Hi all,

    I got a class with a public int.
    In my other class i want to acces that public int to lower it.
    I already made a new instance of the class with the public int from an other class.
    So what i want to do is acces and lower it without making a new instance.

    what i do is like this.

    private Throw throwDart;

    throwDart.dartNumber -= 1;

    because the class name is Throw. But i got a null pointer exceptoin! :(
    Please help me!

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,969
    Rep Power
    8

    Default Re: Nullpointer accessing public int

    What is the value of throwDart when you call throwDart.dartNumber()? In other words, when do you initialize that variable?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    victoryo is offline Member
    Join Date
    Dec 2011
    Posts
    16
    Rep Power
    0

    Default Re: Nullpointer accessing public int

    throwDart.dartNumber -= 1; happens when you undo a throw.
    So in the begin it's 0 but than it't not possible to undo the turn.
    So when it's possible to undo a turn the value = 1 or after 2, 3 turns it's 2 or 3.

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,969
    Rep Power
    8

    Default Re: Nullpointer accessing public int

    You aren't really answering my question. You're talking about dartNumber, which I don't think really matters here.

    What is the value of throwDart? If you don't know, use a debugger (or at least some print statements) to figure it out.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    victoryo is offline Member
    Join Date
    Dec 2011
    Posts
    16
    Rep Power
    0

    Default Re: Nullpointer accessing public int

    throwDart isnt a value. Its an other name to load the class throw.
    if i do throwDart = new Throw(); the nullpointer is gone. But i dont want to make a new Throw...

  6. #6
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,969
    Rep Power
    8

    Default Re: Nullpointer accessing public int

    Quote Originally Posted by victoryo View Post
    throwDart isnt a value. Its an other name to load the class throw.
    if i do throwDart = new Throw(); the nullpointer is gone. But i dont want to make a new Throw...
    Actually, throwDart is a variable that can hold a reference. If you try to dereference it without initializing it first, you'll get a NPE.

    Why do you not want to make a new Throw? It sounds to me like you're pretty confused about the basics of how Objects work. I'd recommend you take another read through the basic tutorials.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    victoryo is offline Member
    Join Date
    Dec 2011
    Posts
    16
    Rep Power
    0

    Default Re: Nullpointer accessing public int

    The reason why i dont want to make a new one.
    is because an other class already did that. So if i do it again
    all values will be reset to the begin state.

  8. #8
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,969
    Rep Power
    8

    Default Re: Nullpointer accessing public int

    Quote Originally Posted by victoryo View Post
    The reason why i dont want to make a new one.
    is because an other class already did that. So if i do it again
    all values will be reset to the begin state.
    I'm really not sure what you mean. Can't you just pass the reference to the original instance then?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,044
    Rep Power
    20

    Default Re: Nullpointer accessing public int

    If you already have a throwDart object, and that is the one you want to decrement the dartNumber on, then you'll have to make that one available to this bit of code, either by passing it in or by increasing the scope of the variable.

    Java does not magically know that throwDart declared here is the same as the throwDart declared somewhere else. Just because they have the same name (to you).

  10. #10
    victoryo is offline Member
    Join Date
    Dec 2011
    Posts
    16
    Rep Power
    0

    Default Re: Nullpointer accessing public int

    Thanks for the feedback. I solved it by giving the constructor of the class a Parameter of throwDart an save it like.
    this.throwDart = throwDart.

    Now it's also clear to me how it works! =D

Similar Threads

  1. NullPointer help
    By moosick in forum AWT / Swing
    Replies: 1
    Last Post: 04-02-2011, 02:35 AM
  2. Got problem on accessing public (global) array
    By smtwtfs in forum New To Java
    Replies: 6
    Last Post: 02-15-2011, 09:02 AM
  3. Replies: 1
    Last Post: 12-04-2010, 05:41 PM
  4. Replies: 4
    Last Post: 10-22-2009, 05:45 PM
  5. Replies: 5
    Last Post: 06-26-2009, 03:03 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
  •