| | |
instantiating array objects
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
i have this code.
but this gives me error:
what's wrong? i want to instantiate an array object named Player with two elements.
java Syntax (Toggle Plain Text)
numberPlayers = 2; players = new Player[numberPlayers]; setName(); setHand(); private static void setName() { for(int i=0;i<numberPlayers;i++) { out.print("Player "+ i + " name: "); players[i].setName(in.nextLine()); out.println(); } } private static void setHand() { for(int i=0;i<numberPlayers;i++) { out.print("Player "+ i+1 + " hand: "); players[i].setName(in.nextLine()); out.println(); } }
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
a joke.
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.
Edit: The default values for the array elements are as follows:
Java Syntax (Toggle Plain Text)
String[] array = new String[5]; for (int i = 0; i < array.length; i++) { array[i] = ""; }
Edit: The default values for the array elements are as follows:
Java Syntax (Toggle Plain Text)
boolean[] --> false int[], short[], float[], double[], long[], char[], byte[] --> 0 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
----------------------------------------------
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
![]() |
Similar Threads
- binarySearch on array of objects (Java)
- array of objects (C++)
- how to map objects to array (VB.NET)
- sorting through an array of objects (Java)
- Instantiating class objects?? (C++)
Other Threads in the Java Forum
- Previous Thread: plz help.
- Next Thread: Java Encryption and Decryption
| Thread Tools | Search this Thread |
-xlint actionlistener android api applet application array arrays automation binary blackberry block bluetooth character chat class classes client code compile component consumer database desktop developmenthelp eclipse error exception fractal freeze ftp game gameprogramming givemetehcodez graphics gui html ide image input integer j2me j2seprojects java javac javaee javaprojects jmf jni jpanel julia lego linked linux list loop loops mac map method methods mobile netbeans newbie notdisplaying number online page print printf problem program programming project properties recursion researchinmotion rotatetext rsa scanner screen server set singleton size sms sort sql string swing template textfields threads time title tree tutorial-sample update windows working






