Results 1 to 10 of 10
Thread: Nullpointer accessing public int
- 01-10-2012, 07:26 PM #1
Member
- Join Date
- Dec 2011
- Posts
- 16
- Rep Power
- 0
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!
- 01-10-2012, 07:30 PM #2
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!
- 01-10-2012, 07:32 PM #3
Member
- Join Date
- Dec 2011
- Posts
- 16
- Rep Power
- 0
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.
- 01-10-2012, 07:48 PM #4
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!
- 01-10-2012, 08:27 PM #5
Member
- Join Date
- Dec 2011
- Posts
- 16
- Rep Power
- 0
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...
- 01-10-2012, 08:43 PM #6
Re: Nullpointer accessing public int
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!
- 01-10-2012, 09:05 PM #7
Member
- Join Date
- Dec 2011
- Posts
- 16
- Rep Power
- 0
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.
- 01-10-2012, 10:12 PM #8
Re: Nullpointer accessing public int
How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 01-11-2012, 09:58 AM #9
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
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).
- 01-11-2012, 10:16 AM #10
Member
- Join Date
- Dec 2011
- Posts
- 16
- Rep Power
- 0
Similar Threads
-
NullPointer help
By moosick in forum AWT / SwingReplies: 1Last Post: 04-02-2011, 02:35 AM -
Got problem on accessing public (global) array
By smtwtfs in forum New To JavaReplies: 6Last Post: 02-15-2011, 09:02 AM -
What is the difference between Public Static Void and Public Void?
By whateverme in forum New To JavaReplies: 1Last Post: 12-04-2010, 05:41 PM -
Accessing non-static public variables from another class
By ribbs2521 in forum New To JavaReplies: 4Last Post: 10-22-2009, 05:45 PM -
Err:java.io.IOException: public key ring doesn't start with public key tag
By Deepa in forum New To JavaReplies: 5Last Post: 06-26-2009, 03:03 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks