【问题标题】:Java Why am I getting a NullPointerException while instantiating my arrayJava 为什么在实例化我的数组时会出现 NullPointerException
【发布时间】:2014-03-25 00:29:24
【问题描述】:

我是编程新手,不明白为什么当我尝试初始化 n、numInt 和 arrayMenu 时程序给我 NullPointerException 的运行时错误。这些似乎都不起作用。该程序的工作是收集一组随机整数以存储在一个数组中,并允许用户选择要选择的排序。感谢阅读。

import java.util.Scanner;
import java.util.Random;
public class VariousSortsHS
{
    private static int[] arrayMenu;
    private static Random generator;

    /**
     * Constructor for objects of class VariousSortsHS.
     */
    public VariousSortsHS(int n) //The error starts here
    {                                
        arrayMenu = new int[n];  //I don't get why it says null in the array when
                                 //i am initializing the length of the array to n
        /*Assigns a random number between 0 too 100.*/
        for(int i = 0; i < n; i++) 
        {
            int temp = generator.nextInt(100);
            arrayMenu[n] = temp;
        }       
    }

    /**
     * Selection Sort method.
     */
    public static void selection(int n)
    {
        for(int i = 0; i < arrayMenu.length - 1; i++)
        {
            int minPos = i;
            for(int j = i + 1; j < arrayMenu.length; j++)
            {
                if(arrayMenu[j] < arrayMenu[minPos]) minPos = j;
            }
            int temp = arrayMenu[i];
            arrayMenu[i] = arrayMenu[minPos];
            arrayMenu[minPos] = temp;
            System.out.print(temp + " ");
        }
    }

    /**
     * Insertion Sort method.
     */
    public static void insertion(int n)
    {
        for(int i = 1; i < arrayMenu.length; i++)
        {
            int next = arrayMenu[i];
            int j = i;
            while(j > 0 && arrayMenu[j - 1] > next)
            {
                arrayMenu[j] = arrayMenu[j - 1];
                j--;
            }
            arrayMenu[j] = next;
            System.out.print(next + " ");
        }
    }

    /**
     * Quick Sort method.
     */
    public static void quick(int n)
    {
        int pivot = arrayMenu[0];
        int i = 0 - 1;
        int j = n + 1;
        while(i < j)
        {
            i++; while(arrayMenu[i] < pivot) i++;
            j++; while(arrayMenu[j] > pivot) j++;
            if(i < j)
            {
                int temp = arrayMenu[i];
                arrayMenu[i] = arrayMenu[j];
                arrayMenu[j] = temp;
                System.out.print(temp + " ");
            }
        }
    }

    /**
     * Main method that allows user to input data. 
     */
    public static void main(String[] args)
    { 
        Scanner in = new Scanner(System.in);
        System.out.println("Do you wish to sort random integers? (Yes or No) ");
        String answer = in.next();
        String answer2 = answer.toLowerCase();

        do
        {
            /*Prompts for array length.*/
            System.out.println("How many random integers do you wish to sort?");
            int numInt = in.nextInt();
            /*Promps for sort selection choice.*/
            System.out.println("Select a sort to use: \n\t1)Selection\n\t2)Insertion\n\t3)Quick");
            String sort = in.next();
            String sort2 = sort.toLowerCase();
            if(sort2.equals("selection"))
            {
                selection(numInt);
            }
            else if(sort2.equals("insertion"))
            {
                insertion(numInt);
            }
            else if(sort2.equals("quick"))
            {
                quick(numInt);
            }
            else
            {
                System.out.println("You have entered the wrong input.");
            }
        } while(!answer2.equals("no"));
    }
}

【问题讨论】:

  • generator 可能评估为空。使用调试器和/或更好地阅读跟踪。
  • 你似乎不明白static这个词是什么意思。您正在调用引用您从未初始化的静态数组的静态方法。
  • 请粘贴堆栈跟踪...

标签: java arrays nullpointerexception


【解决方案1】:
  • 代码中的所有内容都是静态的。这意味着您编写的构造函数永远不会被调用,并且数组从未改变其默认值null。请考虑将您的构造函数代码改为静态初始化块。
  • generator 从未设置为任何内容,因此它也是 null 并且您不能在其上调用 nextInt
  • 初始化数组是设置arrayMenu[n]而不是arrayMenu[i]

【讨论】:

    【解决方案2】:

    当您调用insertion(numInt); 时,方法public static void insertion(int n) 被调用,然后您尝试像for(int i = 1; i &lt; arrayMenu.length; i++) 那样执行for 循环

    但是,arrayMenu 没有初始化,它为空。当您尝试在 null 上调用长度时,您会得到 NullPointerException。

    【讨论】:

      【解决方案3】:

      您需要添加一个静态构造函数并使用静态 int 初始化大小

      //set parameter = n
      public static int parameter;
      
      static 
      { 
          arrayMenu = new int[parameter];
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-10-14
        • 2015-02-07
        • 2011-09-30
        • 2021-08-01
        • 1970-01-01
        • 2012-03-26
        • 2019-09-07
        相关资源
        最近更新 更多