I am experiencing when running this code a null pointer exception, but
I am lost to why I am receiving this error? I know what the error
means. But I think I have set everything up right...(apparently
not)....
I have indicated where in the below code I receive the error.
Thanks in advanced,
Peter
-----------------------------------------------------
import java.util.Random;
public class Randy
{
private char[] moves;
private int[] history;
public Randy()
{
char[] moves =
{'-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-'};
int[] history = new int[16];
}
static public void main(String[] args)
{
Randy test = new Randy();
System.out.println(test.start());
}
public String start()
{
boolean valid = false, played = false, wonLast = false;
int temp, timerCheck = 0;
boolean player1 = true;
Random move = new Random();
while(!valid)
{
while(!valid)
{
if(player1 && played && wonLast)
{
moves[history[timerCheck]] = '1';
}
else
{
temp = move.nextInt(16);
if(moves[temp] == '-') //null point exception here?
But why?
{
if(player1)
{
moves[temp] = '1';
history[timerCheck] = temp;
break;
}
else
{
moves[temp] = '2';
break;
}
}
}
}
player1 = !player1;
timerCheck++;
if(timerCheck > 15)
valid = true;
}
return "The Game End: " + moves[0] + "" + moves[1] + "" +
moves[2] + "" + moves[3] + "" + moves[4] + "" + moves[5] + "" +
moves[6] + "" + moves[7] + "" + moves[8] + "" +
moves[9] + "" + moves[10] + "" +
moves[11] + "" + moves[12] + "" + moves[13] + "" + moves[14] + "" +
moves[15] + ". History: " +
history[0] + " " + history[2] + " " +
history[4] + " " + history[6] + " " + history[8] + " " + history[10] +
" " + history[12] + " " + history[14];
}
}
Matt Humphrey - 01 Mar 2008 00:30 GMT
>I am experiencing when running this code a null pointer exception, but
> I am lost to why I am receiving this error? I know what the error
> means. But I think I have set everything up right...(apparently
> not)....
You have declared the "moves" array both as an instance variable and as a
local variable within the constructor. Only the local variable has the
declared array--the instance variable is still null.
Matt Humphrey http://www.iviz.com/
TheBigPJ - 01 Mar 2008 00:41 GMT
I see, I see....*wonders why he made such a mistake*... thanks Matt
for the fast reply.
Peter.
Roedy Green - 01 Mar 2008 18:47 GMT
On Fri, 29 Feb 2008 16:21:31 -0800 (PST), TheBigPJ
<TheBigPJ@gmail.com> wrote, quoted or indirectly quoted someone who
said :
> if(moves[temp] == '-') //null point exception here?
You could put in some ifs or asserts to test the assumption that moves
is null.
Then you need to check where you assign it, and check right after if
it worked.
From past experience, and from recollections of my own bafflement on
this matter in the olden days of Java 1.0, I am pretty sure you don't
understand Java's multistage array initialisation mechanism.
See http://mindprod.com/jgloss/array.html#ARRAYINITGOTCHAS
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
TheBigPJ - 01 Mar 2008 19:06 GMT
> You could put in some ifs or asserts to test the assumption that moves
> is null.
[quoted text clipped - 7 lines]
>
> See http://mindprod.com/jgloss/array.html#ARRAYINITGOTCHAS
Thanks Roedy. That was really useful.
Peter,
Roedy Green - 01 Mar 2008 18:52 GMT
On Fri, 29 Feb 2008 16:21:31 -0800 (PST), TheBigPJ
<TheBigPJ@gmail.com> wrote, quoted or indirectly quoted someone who
said :
> null pointer exception
for generic help in tracking down NullPointerExceptions, see
http://mindprod.com/jgloss/runerrormessages.html#NULLPOINTEREXCEPTION
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com