instantiating array objects

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jan 2009
Posts: 43
Reputation: scias23 is an unknown quantity at this point 
Solved Threads: 0
scias23's Avatar
scias23 scias23 is offline Offline
Light Poster

instantiating array objects

 
0
  #1
Sep 5th, 2009
i have this code.

  1. numberPlayers = 2;
  2. players = new Player[numberPlayers];
  3. setName();
  4. setHand();
  5.  
  6. private static void setName() {
  7. for(int i=0;i<numberPlayers;i++) {
  8. out.print("Player "+ i + " name: ");
  9. players[i].setName(in.nextLine());
  10. out.println();
  11. }
  12. }
  13.  
  14. private static void setHand() {
  15. for(int i=0;i<numberPlayers;i++) {
  16. out.print("Player "+ i+1 + " hand: ");
  17. players[i].setName(in.nextLine());
  18. out.println();
  19. }
  20. }

but this gives me error:
ROCK-PAPER-SCISSORS GAME
NOTE: hand input will loop until user(s) input(s) valid hand values
VALID HANDS: R-Rock, P-Paper, S-Scissors

Select your option below:

[1] Player vs Player
[2] Player vs Computer 1
Exception in thread "main" java.lang.NullPointerException
at RPS2.Game.setName(Game.java:61)
at RPS2.Game.main(Game.java:37)
Player 0 name: Java Result: 1
what's wrong? i want to instantiate an array object named Player with two elements.
a joke.
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,402
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 256
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

Re: instantiating array objects

 
0
  #2
Sep 5th, 2009
Initiating an array simply creates "holding spaces" for the elements. Each of those elements is, however, currently "null". If you expect to have a "Player" predefined at every element, then you need to loop over the array and create one in each spot. i.e.

  1. String[] array = new String[5];
  2. for (int i = 0; i < array.length; i++) {
  3. array[i] = "";
  4. }

Edit: The default values for the array elements are as follows:

  1. boolean[] --> false
  2. int[], short[], float[], double[], long[], char[], byte[] --> 0
  3. Object[] --> null
Last edited by masijade; Sep 5th, 2009 at 4:15 am.
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC