Jar McC
Jar McC

Reputation: 1

Null Pointer Exception calling String[] from another class

My problem is just that. I've tried to declare the string as a specific size, I sort and everything, but the only thing that's worked is putting the string in the main method inside the main class that I use. It's frustrating, because I've tried everything that I can think of. Why are the elements becoming void when I import them from another class? I added a main method to the moves class and moved it there to try it too, but to no avail. Here's the first program:

    import java.util.Arrays;
public class movesClass {
    public String[] getMoves() {
        return moves;
    }
    String[] moves;
    public static String[] useMove(String[] moves) {
        moves[0] = "punch";
        moves[1] = "kick";
        moves[2] = "defend";
        moves[3] = "run";
        moves[4] = "inventory";
        moves[5] = "rickroll";
        moves[6] = "heal";
        Arrays.sort(moves);
        return moves;
    }
}
And the body of the main one:

    import java.io.*;
import java.util.*;
public class practiceBattle {
    public static void main(String[] args) {

        Scanner scan = new Scanner(System.in);
        Random rand = new Random();
        inventoryClass quant = new inventoryClass();
        movesClass mov = new movesClass();
        String[] moves = mov.getMoves();

        int hp = 0;
        int playerHp = 200;

        String input = "default";

        int damage = 0;

        int hitDif = 50; //default
        int kickChance1 = 0; //default -- goes into hitChance
        int kickChance = kickChance1 - hitDif; //chance to hit
        int hitChance1 = 0;
        int hitChance = hitChance1 - hitDif;

        int runDif = 50; //default
        int runChance1 = 0; //default -- goes into runChance
        int runChance = runChance1 - runDif; //chance to run

        int enemyMinAttack = 0;
        int enemyAttack = 0;

        int index = 0;

        int[] levelArray = {1, 2, 3};
        String[] monsterArray = {"GNOLL", "TROLL", "DRAGON"};
        String monster = monsterArray[rand.nextInt(monsterArray.length)];
        int level = rand.nextInt(levelArray.length) + 1;
        if(level == 1)
        {
            System.out.println("YOUR OPPONENT IS A BABY " + monster + "!");
        }
        else
        {
            System.out.println("YOUR OPPONENT IS A LEVEL " + level + " " + monster + "!");
        }
        if(level == 1)
        {
            hp = rand.nextInt(20) + 41;
            enemyAttack = rand.nextInt(5) + 1;
        }
        else if(level == 2)
        {
            hp = rand.nextInt(20) + 91;
            enemyAttack = rand.nextInt(6) + 5;
        }
        else if(level == 3)
        {
            hp = rand.nextInt(20) + 141;
            enemyAttack = rand.nextInt(7) + 10;
        }
        enemyMinAttack = rand.nextInt(enemyAttack) + 1;

        System.out.println("YOUR OPPONENT'S HP IS: " + hp + "\n");
        int permEnemyAttack = enemyAttack;
        int permEnemyMinAttack = enemyMinAttack;







    ext:
        while(hp > 0)
        {
            enemyAttack = permEnemyAttack;
            enemyMinAttack = permEnemyMinAttack;

            do
            {
                if(playerHp == 0)
                {
                    System.out.println("YOU HAVE DIED. GAME OVER!\n\n\n");
                    break ext;
                }
                else
                {
                    System.out.println("Choose an action:\nkick\npunch\nrun\ndefend\n");
                    input = scan.nextLine();


                    index = Arrays.binarySearch(moves, input);
                    System.out.println(index);
                    if(index < 0)
                    {
                        System.out.println("\n\n\nINVALID INPUT -- TRY AGAIN\n\n\n");
                    }
                }

            } while(index < 0);

That's what I have of the main one, up to where the problem ends and from where it begins. Any inpu would be greatly appreciated. Thank you!

Upvotes: 0

Views: 900

Answers (1)

Reboot
Reboot

Reputation: 1744

You never initialize the moves field inside the movesClass. It is null, the default value for reference fields. If you want to intialize it you have to do:

String[] moves = new String[7];

But even if you do that, the elements of the array will still be null and you have to put the actual Strings in the array. It seems your useMoves() is made for that, but it is never called. The method is kind of odd anyway. You have an array as an argument that you fill with the moves and then sort it, but then you return the same array, you already got as the parameter. Java does not copy arrays when you pass them from one method to another or return them from a method, so you modify the array you get as a parameter. If you always want to initialize the moves field with the same moves, it would be better to do this in the constructor of the class. A static method has no access to fields of instances of the class.

Upvotes: 3

Related Questions