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

标签 java nullpointerexception arrays

我的问题就是这样。我尝试将字符串声明为特定大小,并对所有内容进行排序,但唯一有效的是将字符串放入我使用的主类内的 main 方法中。这很令人沮丧,因为我已经尝试了我能想到的一切。当我从另一个类导入元素时,为什么这些元素会变得无效?我在 move 类中添加了一个 main 方法,并将其移到那里进行尝试,但没有成功。 这是第一个程序:

    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);

这就是我所掌握的主要问题,直到问题结束的地方和问题开始的地方。 任何意见将不胜感激。谢谢!

最佳答案

您永远不会初始化 movesClass 内的 moves 字段。它为 null,是引用字段的默认值。如果你想初始化它,你必须这样做:

String[] moves = new String[7];

但即使你这样做,数组的元素仍然是 null 并且你必须将实际的字符串放入数组中。看来您的 useMoves() 就是为此而设计的,但它从未被调用。无论如何,这个方法有点奇怪。您有一个数组作为参数,您用移动填充该数组,然后对其进行排序,但随后返回您已经作为参数获得的相同数组。当您将数组从一个方法传递到另一个方法或从方法返回数组时,Java 不会复制数组,因此您可以修改作为参数获取的数组。如果您总是想用相同的 Action 初始化 moves 字段,最好在类的构造函数中执行此操作。静态方法无法访问类实例的字段。

关于java - 从另一个类调用 String[] 的空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5452570/

相关文章:

c# - 我应该关注 .NET 字典的速度吗?

arrays - 使用 array.sort 时出错

java - 在JTree中显示系统中的所有驱动器

java - 通过java访问oracle数据库

java - 如何统计方法的调用次数?

java - Android Studio : Passing RadioButtons using Intent ERROR NullPointerException

javascript - 对键值对匹配的嵌套对象中的值求和

java - 带有图像的 Libgdx 掩码

android - 从 ViewPager Activity 访问 fragment 的方法

android - 即使对象不为空,也出现空指针异常