My Turn To Ask For Java Help

ByekaByeka Name changed from Freak83Toronto Join Date: 2003-03-13 Member: 14484Members, Constellation
<div class="IPBDescription">game applet</div> This is an applet to create the game Yum. It's getting some crazy error and I can't seem to figure out why. Basically what happens right now is it starts up with the original screen with the word "YUM" going across it. You click on the screen then it shows a picture asking how many players you want, with 3 pictures of buttons to choose from. That's where it gets this error

<!--c1--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>CODE</b> </td></tr><tr><td id='CODE'><!--ec1-->
java.lang.NullPointerException
       at sun.java2d.pipe.DrawImage.copyImage(DrawImage.java:48)
       at sun.java2d.pipe.DrawImage.copyImage(DrawImage.java:715)
       at sun.java2d.pipe.ValidatePipe.copyImage(ValidatePipe.java:147)
       at sun.java2d.SunGraphics2D.drawImage(SunGraphics2D.java:2782)
       at sun.java2d.SunGraphics2D.drawImage(SunGraphics2D.java:2772)
       at Yum_Main.DrawDice(Yum_Main.java:262)
       at Yum_Main.DiceRoll(Yum_Main.java:127)
       at Yum_Main.GetPlayers(Yum_Main.java:102)
       at Yum_Main.mouseDown(Yum_Main.java:176)
       at java.awt.Component.handleEvent(Component.java:5245)
       at java.awt.Component.postEvent(Component.java:3806)
       at java.awt.Component.dispatchEventImpl(Component.java:3542)
       at java.awt.Container.dispatchEventImpl(Container.java:1437)
       at java.awt.Component.dispatchEvent(Component.java:3367)
       at java.awt.EventQueue.dispatchEvent(EventQueue.java:445)
       at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:190)
       at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:144)
       at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
       at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:130)
       at java.awt.EventDispatchThread.run(EventDispatchThread.java:98)
<!--c2--></td></tr></table><div class='postcolor'><!--ec2-->

The only line that's different is this one:

<!--c1--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>CODE</b> </td></tr><tr><td id='CODE'><!--ec1-->
click 2 it reads:   at Yum_Main.GetPlayers(Yum_Main.java: 90)
click 3 it reads:   at Yum_Main.GetPlayers(Yum_Main.java: 97)
click 4 it reads:   at Yum_Main.GetPlayers(Yum_Main.java: 102)<!--c2--></td></tr></table><div class='postcolor'><!--ec2-->

This is the program so far:

<!--c1--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>CODE</b> </td></tr><tr><td id='CODE'><!--ec1-->
// The "Yum_Main" class.
import java.applet.*;
import java.awt.*;

public class Yum_Main extends Applet
{

   static int PlayerNumber = 2;
   static int PlayerTotalScore [];
   static int PlayerSlotScore [] [];
   static int PlayerSubTotal [];
   static final String TitleScreen = "Title.gif";
   static final String GetPlayerScreen = "NumberOfPlayers.gif";
   final String Dice1 = "Dice 1.GIF";
   final String Dice2 = "Dice 2.GIF";
   final String Dice3 = "Dice 3.GIF";
   final String Dice4 = "Dice 4.GIF";
   final String Dice5 = "Dice 5.GIF";
   final String Dice6 = "Dice 6.GIF";
   //static final String Layout
   Image GetPlayers;
   Image Title;
   Image DiceImage [] = new Image [6];
   //boolean showTitle = true;
   static int X = -10, Y = -10; //the x and the y co-ordinates of the mouse click location.(bad grammer?)
   static int GameStages = 0; //(The number value of this integer will act as a boolean and a guide...
   int Dice [];
   boolean ContinueChoosing; //this boolean determines whether the user is happy with the dice he rolled.

   //---------------------------------------------------------------------------------
   public void init ()
   {
       Graphics g;
       g = getGraphics ();
       Title = getImage (getCodeBase (), TitleScreen);
       GetPlayers = getImage (getCodeBase (), GetPlayerScreen);

       DiceImage [0] = getImage (getCodeBase (), Dice1);
       DiceImage [1] = getImage (getCodeBase (), Dice2);
       DiceImage [2] = getImage (getCodeBase (), Dice3);
       DiceImage [3] = getImage (getCodeBase (), Dice4);
       DiceImage [4] = getImage (getCodeBase (), Dice5);
       DiceImage [5] = getImage (getCodeBase (), Dice6);
       MediaTracker tracker = new MediaTracker (this);
       tracker.addImage (Title, 0);
       tracker.addImage (DiceImage [0], 1);
       tracker.addImage (DiceImage [1], 2);
       tracker.addImage (DiceImage [2], 3);
       tracker.addImage (DiceImage [3], 4);
       tracker.addImage (DiceImage [4], 5);
       tracker.addImage (DiceImage [5], 6);
       //Loads the images

       try
       {
           //Start downloading the images. Wait until they're loaded.
           tracker.waitForAll ();
       }
       catch (InterruptedException e)
       {
       }

   } // init method


   //----------------------------------------------------------------------------------------------

   public void paint (Graphics g)
   {
       g.drawImage (Title, 0, 0, null);
   }


   //-----------------------------------------------------------------------------------------------

   public void GetPlayers ()
       //gets the picture and imports it on to the applet.
       //then sees where the mouse is clicked.
       //if the mouse is clicked in the range of either 2, 3, or 4.
       //the number of players is determined.
   {
       Graphics g = getGraphics ();
       MediaTracker tracker = new MediaTracker (this);
       tracker.addImage (GetPlayers, 1);
       g.drawImage (GetPlayers, 0, 0, null);
       //the user how many people are playing...
       if (X > 109 && X < 172 && Y > 240 && Y < 294)
       {
           PlayerNumber = 2;
           DiceRoll ();
           tracker.removeImage (GetPlayers, 1);
       }
       else if (X > 264 && X < 327 && Y > 240 && Y < 294)
       {

           PlayerNumber = 3;
           DiceRoll ();
       }
       else if (X > 413 && X < 476 && Y > 240 && Y < 294)
       {
           PlayerNumber = 4;
           DiceRoll ();
       }
       else
       {
       }
   }


   //-----------------------------------------------------------------------------------------------

   public void DiceRoll ()  //PENDING
       //rolls the dice and draws the dice and then makes
       //it possible for the user to see what they have rolled.
       //then they can choose which dice are not good rolls and which ones are.
       //then re-roll up to 2 times.
   {
       //gets the random dice...
       Graphics g = getGraphics ();
       g.clearRect (0, 0, 640, 480);
       Dice = new int [5];
       Dice [0] = (int) ((Math.random () * 6) + 1);
       Dice [1] = (int) ((Math.random () * 6) + 1);
       Dice [2] = (int) ((Math.random () * 6) + 1);
       Dice [3] = (int) ((Math.random () * 6) + 1);
       Dice [4] = (int) ((Math.random () * 6) + 1);
       DrawDice ();
   }


   //----------------------------------------------------------------------------------------
   public void DiceChoose ()
       //this is hard to imagine without actually be able to draw it... but, the code will be
       //done with respect to anynumbers and the range will be set later.
   {
       while (ContinueChoosing = true)
       {
           if (X > 360 && X < 420 && Y > 55 && Y < 117)
           {
               Dice [0] = 0;
           }
           else if (X > 538 && X < 598 && Y > 55 && Y < 117)
           {
               Dice [1] = 0;
           }
           else if (X > 446 && X < 506 && Y > 141 && Y < 203)
           {
               Dice [2] = 0;
           }
           else if (X > 360 && X < 420 && Y > 223 && Y < 285)
           {
               Dice [3] = 0;
           }
           else if (X > 541 && X < 600 && Y > 221 && Y < 283)
           {
               Dice [4] = 0;
           }
       }

   }


   //-------------------------------------------------------------------------------------------------
   public boolean mouseDown (Event e, int x, int y)
       //this is the method that makes it all easy and hard at the same time...
       //this method will get the values of where the mouse is clicked.
       //that is used to determine the choice of the player.
   {
       //gets the X, and the Y co-ordinates..
       X = x;
       Y = y;
       //right now, it just draws where the mouse is clicked.. but later when the
       //images can be put in the applet, the mouse clicks will mae the game flow..

       if (GameStages == 0)
           GetPlayers ();

       return true;
   } // mouseDown method


   //-------------------------------------------------------------------------------------------------
   public void ReRoll ()
       //this method sees which dice are removed be the user...
       //then it rerolls it again...
       //after this the user gets to choose which dice to re-roll again...
       //then his turn ends and the next user does the same thing.
   {
       for (int count = 0; count <= 4; count++)
       {
           if (Dice [count] == 0)
           {
               Dice [count] = (int) ((Math.random () * 6) + 1);
           }
       }
   }


   //--------------------------------------------------------------------------------------------

   public void WinnerDeclare ()
   {
       int MaxScore = -100;
       int WinnerIndex = 0; //the winners number. eg if the index is 1 then player 1 wins.
       for (int count = 1; count <= PlayerNumber; count++)
       {
           if (MaxScore > PlayerTotalScore [count])
           {
               MaxScore = PlayerTotalScore [count];
               WinnerIndex = count;
           }
       }
       Graphics G = getGraphics ();
       G.clearRect (0, 0, 640, 480);
       G.drawString ("" + "Player " + WinnerIndex + " wins!!!", 100, 100);
   }


   //----------------------------------------------------------------------------------------------
   public void ScoreSlotChoose ()
   {
   }


   //-----------------------------------------------------------------------------------------------

   public void DrawDice ()
   {
       Graphics g = getGraphics ();
       MediaTracker tracker = new MediaTracker (this);
       //sets the images...

       for (int count = 0; count <= 4; count++)
       {
           if (Dice [count] == 1)
           {
               DiceImage [count] = getImage (getCodeBase (), Dice1);
           }
           else if (Dice [count] == 2)
           {
               DiceImage [count] = getImage (getCodeBase (), Dice2);
           }
           else if (Dice [count] == 3)
           {
               DiceImage [count] = getImage (getCodeBase (), Dice3);
           }
           else if (Dice [count] == 4)
           {
               DiceImage [count] = getImage (getCodeBase (), Dice4);
           }
           else if (Dice [count] == 5)
           {
               DiceImage [count] = getImage (getCodeBase (), Dice5);
           }
           else if (Dice [count] == 6)
           {
               DiceImage [count] = getImage (getCodeBase (), Dice6);
           }
       }

       tracker.addImage (DiceImage [0], 3);
       g.drawImage (DiceImage [0], 64, 67, null);
   }
} // Yum_Main class
<!--c2--></td></tr></table><div class='postcolor'><!--ec2-->

help?

Comments

  • SoulSkorpionSoulSkorpion Join Date: 2002-04-12 Member: 423Members
    I've never used awt, but just tracing your compiler error messages back I'd guess that the problem is

    <!--c1--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>CODE</b> </td></tr><tr><td id='CODE'><!--ec1-->
    public void DrawDice ()
    {
     //...
     g.drawImage (DiceImage [0], 64, 67, null); //<-here
    }
    <!--c2--></td></tr></table><div class='postcolor'><!--ec2-->
  • JHunzJHunz Join Date: 2002-11-15 Member: 8815Members, Constellation
    edited June 2005
    There's either a problem with g, or with DiceImage[0].

    A few things to check:
    Are you sure getGraphics is returning a non-null context?
    Also, if somehow Dice[count] happened to be less than 1 or more than 6, DiceImage[count] would be undefined. I'd set up a couple checks to find out which is going wrong.

    [Edit]: You should also probably check on whether getImage is returning the values you expect.
  • saichaitanyasaichaitanya Join Date: 2005-06-13 Member: 53827Members
    edited June 2005
    <!--QuoteBegin-JHunz+Jun 13 2005, 10:28 PM--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>QUOTE</b> (JHunz @ Jun 13 2005, 10:28 PM)</td></tr><tr><td id='QUOTE'><!--QuoteEBegin--> There's either a problem with g, or with DiceImage[0].

    A few things to check:
    Are you sure getGraphics is returning a non-null context?
    Also, if somehow Dice[count] happened to be less than 1 or more than 6, DiceImage[count] would be undefined.  I'd set up a couple checks to find out which is going wrong.

    [Edit]: You should also probably check on whether getImage is returning the values you expect. <!--QuoteEnd--></td></tr></table><div class='postcolor'><!--QuoteEEnd-->
    if the DiceImage has 5 variables in the array instead of 6 theres an error occuring...
    and it is true that there is a problem here


    public void DrawDice ()
    {
    g.drawImage (DiceImage [0], 64, 67, null); //<-here
    }

    its just that fixing it is a problem..
Sign In or Register to comment.