Results 1 to 10 of 10
  1. #1
    Burnett98 is offline Member
    Join Date
    Jun 2012
    Posts
    13
    Rep Power
    0

    Default Need help with arrays

    In the main class of my project(which happens to be a game) I have an array titled enemyList. The first thing I do in the program is run a method in the main class titled createAllEnemies. In this method, I assigning various spots in the array to different enemies I have programmed in. For example:
    Java Code:
    enemyList[1].creation("your friend", "Slash", "Stab", "", 100, 10, 20, 0, 100, 1, 2, 100, 10);
    My problem is that when I try and call an enemy from this array in a different class eg.
    Java Code:
    Battle battle = new Battle();
    battle.main(Main.enemyList[3], 0);
    I get the last created enemy in the array. So in this case, since it was made last, I'll get number 99 in the array. Why is this happening? More code included at the bottom.

    Main(shortened for relevance):
    Java Code:
    public class Main 
    {
            .
            .
            .
            static Enemy[] enemyList = new Enemy[100];
    	.
            .
            .
    	static Character_Creation creation = new Character_Creation();
            public static void main(String[] args) 
                {
                    createAllEnemies();
                    Character_Creation creation = new Character_Creation();
                    creation.main(args);  
                }
            public static void createAllEnemies()
            {
                //                 Name  At.1nm  At.2nm  At.3nm  Hp  At.1dm  At.2dm At.3dm Exp. Lv. NmAt. MxHp. Sp.
                enemyList[1].creation("your friend", "Slash", "Stab", "", 100, 10, 20, 0, 100, 1, 2, 100, 10);
                enemyList[2].creation("the strange man", "Fireball", "Ice Spear", "Lightning Bolt", 150, 40, 40, 40, 150, 100, 3, 100, 15);
                enemyList[3].creation("the Goblin", "Jab", "Poke", "Club", 150, 20, 30, 40, 100, 5, 3, 150, 10);
                enemyList[20].creation("a bat", "Screach", "Dive Bomb", "", 400, 10, 20, 0, 30, 2, 2, 30, 20);
                enemyList[21].creation("a rat", "Scratch", "Bite", "", 50, 10, 20, 0, 40, 3, 2, 40,25);
                enemyList[22].creation("a wolf", "Scratch", "Bite", "", 40, 20, 30, 0, 60, 2, 2, 50, 15);
                enemyList[23].creation("a boar", "Charge", "Gore", "", 50, 20, 30, 0, 60, 2, 2, 50, 10);
                enemyList[99].creation("the god", "Smite", "", "", 9999, 9999, 0, 0, 9999, 9999, 1, 9999, 9999);
            } 
    }
    Edit: Realized it was not a random result I'm getting. Reflected in edit.
    Last edited by Burnett98; 01-23-2013 at 05:43 AM.

  2. #2
    viren2727in is offline Member
    Join Date
    Jan 2013
    Location
    INDIA
    Posts
    18
    Rep Power
    0

    Default Re: Need help with arrays

    Quote Originally Posted by Burnett98 View Post
    In the main class of my project(which happens to be a game) I have an array titled enemyList. The first thing I do in the program is run a method in the main class titled createAllEnemies. In this method, I assigning various spots in the array to different enemies I have programmed in. For example:
    Java Code:
    enemyList[1].creation("your friend", "Slash", "Stab", "", 100, 10, 20, 0, 100, 1, 2, 100, 10);
    My problem is that when I try and call an enemy from this array in a different class eg.
    Java Code:
    Battle battle = new Battle();
    battle.main(Main.enemyList[3], 0);
    I get the last created enemy in the array. So in this case, since it was made last, I'll get number 99 in the array. Why is this happening? More code included at the bottom.

    Main(shortened for relevance):
    Java Code:
    public class Main 
    {
            .
            .
            .
            static Enemy[] enemyList = new Enemy[100];
    	.
            .
            .
    	static Character_Creation creation = new Character_Creation();
            public static void main(String[] args) 
                {
                    createAllEnemies();
                    Character_Creation creation = new Character_Creation();
                    creation.main(args);  
                }
            public static void createAllEnemies()
            {
                //                 Name  At.1nm  At.2nm  At.3nm  Hp  At.1dm  At.2dm At.3dm Exp. Lv. NmAt. MxHp. Sp.
                enemyList[1].creation("your friend", "Slash", "Stab", "", 100, 10, 20, 0, 100, 1, 2, 100, 10);
                enemyList[2].creation("the strange man", "Fireball", "Ice Spear", "Lightning Bolt", 150, 40, 40, 40, 150, 100, 3, 100, 15);
                enemyList[3].creation("the Goblin", "Jab", "Poke", "Club", 150, 20, 30, 40, 100, 5, 3, 150, 10);
                enemyList[20].creation("a bat", "Screach", "Dive Bomb", "", 400, 10, 20, 0, 30, 2, 2, 30, 20);
                enemyList[21].creation("a rat", "Scratch", "Bite", "", 50, 10, 20, 0, 40, 3, 2, 40,25);
                enemyList[22].creation("a wolf", "Scratch", "Bite", "", 40, 20, 30, 0, 60, 2, 2, 50, 15);
                enemyList[23].creation("a boar", "Charge", "Gore", "", 50, 20, 30, 0, 60, 2, 2, 50, 10);
                enemyList[99].creation("the god", "Smite", "", "", 9999, 9999, 0, 0, 9999, 9999, 1, 9999, 9999);
            } 
    }
    Edit: Realized it was not a random result I'm getting. Reflected in edit.

    try like this

    Java Code:
    Enemy enemy = new Enemy()
    enemy.creation("your friend", "Slash", "Stab", "", 100, 10, 20, 0, 100, 1, 2, 100, 10);
    enemyList[1] = enemy
    
    Enemy enemy1 = new Enemy()
    enemy1.creation("the strange man", "Fireball", "Ice Spear", "Lightning Bolt", 150, 40, 40, 40, 150, 100, 3, 100, 15);
    enemyList[2] = enemy1
    like this....

  3. #3
    Burnett98 is offline Member
    Join Date
    Jun 2012
    Posts
    13
    Rep Power
    0

    Default Re: Need help with arrays

    I just tried that to no avail. It still just brings up number 99. When I've gone back and commented out all creation of 99 it gives me the next lowest number available. Any other ideas on why this is happening?

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,044
    Rep Power
    20

    Default Re: Need help with arrays

    Where do you initialise the individual elements of your array?
    I am guessing you initialise once and apply that same object to all elements in the array creating an array of 100 references all to the same object.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    Burnett98 is offline Member
    Join Date
    Jun 2012
    Posts
    13
    Rep Power
    0

    Default Re: Need help with arrays

    Wait I don't understand what you're asking. What I'm doing is in the main class I have a static array titled enemyList. The first thing that gets executed during the main method is a method that fills this array with different enemies. So I've tried it both where I do something like enemyList[10].creation(...); (the creation method is how I fill the different variables and such inside of the enemy class). I've also tried filling the array by first creation the enemy, and then telling the array that a certain place inside of it should hold this. So,

    Enemy friend = new Enemy();
    friend.creation(...);
    enemyList[1] = friend;

    However, when I call enemyList[1] I will instead be getting the result for the last filled part of the array, so number 99.

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,044
    Rep Power
    20

    Default Re: Need help with arrays

    Java Code:
    enemyList[1].creation();
    That code will give a null pointer exception with the code you posted above.
    I know that is not all your code, because you have a line of dots.
    So therefore somewhere you are populating that entry (enemyList[1]) with an object reference:
    Java Code:
    enemyList[1] = some Enemy reference;
    Where are you doing that?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    Burnett98 is offline Member
    Join Date
    Jun 2012
    Posts
    13
    Rep Power
    0

    Default Re: Need help with arrays

    Well originally all I was doing was that. It was just enemyList[1].creation("your friend", "Slash", "Stab", "", 100, 10, 20, 0, 100, 1, 2, 100, 10); That worked perfectly fine for creating enemies. I know because the battle with the 99th enemy was going fine. It just wasn't the right one. Now I'm doing it differently by creating an instance of the enemy titled friend and assigning friend all these values, and then on the next line saying enemyList[1] = friend;
    Neither of these works to access it later.

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,044
    Rep Power
    20

    Default Re: Need help with arrays

    Argh!
    At the moment, the code you have posted is summarised as this:
    Java Code:
    public class Main  {
       static Enemy[] enemyList = new Enemy[100];
    
       public static void main(String[] args)
       {
          createAllEnemies(); 
       }
    
       private void createAllEnemies() {
          enemyList[1].creation("your friend", "Slash", "Stab", "", 100, 10, 20, 0, 100, 1, 2, 100, 10); 
       }
    }
    This will go *bang* with a NullPointerException as enemyList[1] is null at that point.
    You have not shown where in your code you have populated your array with Enemy objects.

    That is where your problem is.
    Stop faffing about with the code in createAllEnemies() and show the code that populates the enemyList.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  9. #9
    Burnett98 is offline Member
    Join Date
    Jun 2012
    Posts
    13
    Rep Power
    0

    Default Re: Need help with arrays

    if you want this: static Enemy[] enemyList = new Enemy[100]; here you go. That was in my first post. Other than that, there is no other time anything about it gets referenced. And no, I have gotten no null pointer exceptions when it's been run.
    Edit: didn't see the first part of your code. So yes, in essence that's all
    Second edit: Also, that is not the code I'm currently using. It was working the same as my current code, but I'm doing what viren suggested at the moment.

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,044
    Rep Power
    20

    Default Re: Need help with arrays

    Your original code would result in a NullPointerException if enemyList was not being populated somewhere (I'm going to hazard a guess at Character_Creation constructor, if there's nothing else in Main).
    Compile and run the following code, which is logically the same as yours (ie declare an array, then try and execute something on an entry in that array).
    Java Code:
    public class Scratch {
       private static String[] enemyList = new String[100];
    
       public static void main(String args[]) {
          createAllEnemies();
       }
    
       private static void createAllEnemies() {
          enemyList[1].contains("something");
       }
    }
    That throws an NPE.

    Now, it's possible your creation() method is static, in which case:
    1) What does the method do?
    2) You are not writing the call correctly. You call static methods using the classname, not a variable name. This helps to avoid confusion when someone reads your code. Calling it using a variable makes it look like it's a non-static method.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Copying Single Arrays to 2-D Arrays
    By jmscarlet9 in forum New To Java
    Replies: 7
    Last Post: 04-02-2012, 11:17 PM
  2. Casting Enum Type arrays to object type arrays
    By nmvictor in forum Advanced Java
    Replies: 4
    Last Post: 02-17-2012, 12:49 PM
  3. arrays and multidimensional arrays
    By belfast09 in forum New To Java
    Replies: 5
    Last Post: 06-14-2011, 01:28 PM
  4. store array of arrays in array of arrays
    By joost_m in forum New To Java
    Replies: 4
    Last Post: 04-19-2010, 10:32 AM
  5. Arrays.sort... why sorting all arrays in class?
    By innspiron in forum New To Java
    Replies: 6
    Last Post: 03-23-2010, 01:40 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •