Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / March 2008

Tip: Looking for answers? Try searching our database.

I know this is something stupid....

Thread view: 
TheBigPJ - 01 Mar 2008 00:21 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)....

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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.