简体   繁体   English

空指针异常从另一个类调用String []

[英]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. 我试图将字符串声明为特定大小,对所有内容进行排序,但是唯一有效的方法是将字符串放入我使用的主类内的main方法中。 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. 我在moves类中添加了main方法,然后将其移到那里进行尝试,但无济于事。 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! 谢谢!

You never initialize the moves field inside the movesClass . 您永远不会在movesClass内部初始化moves字段。 It is null, the default value for reference fields. 它为null,这是参考字段的默认值。 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. 但是即使这样做,数组的元素仍将为null并且必须将实际的Strings放入数组中。 It seems your useMoves() is made for that, but it is never called. 看来您的useMoves()是为此而设计的,但从未调用过。 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. 当您将数组从一个方法传递给另一方法或从方法返回它们时,Java不会复制数组,因此您可以修改作为参数获取的数组。 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. 如果您总是想用相同的动作来初始化moves字段,那么最好在类的构造函数中执行此操作。 A static method has no access to fields of instances of the class. 静态方法无法访问类实例的字段。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM