Java为什么在实例化我的数组时会出现NullPointerException

时间:2014-03-25 00:29:24

标签: java arrays nullpointerexception

我是编程的新手,并且当我尝试初始化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"));
    }
}

3 个答案:

答案 0 :(得分:1)

  • 代码中的所有内容都是静态的。这意味着您从未调用过您编写的构造函数,并且该数组从未更改过其默认值null。请考虑将构造函数代码更改为静态初始化块。
  • generator永远不会被设置为任何内容,因此它也会为空而且您无法在其上调用nextInt
  • 初始化阵列正在设置arrayMenu[n]而不是arrayMenu[i]

答案 1 :(得分:0)

当您致电insertion(numInt);时,方法public static void insertion(int n)被调用,然后您尝试执行for循环,如for(int i = 1; i < arrayMenu.length; i++)

但是,arrayMenu未初始化,它为空。当您尝试调用长度时,在null时,您将获得NullPointerException。

答案 2 :(得分:0)

您需要添加静态构造函数并使用static int

初始化大小
//set parameter = n
public static int parameter;

static 
{ 
    arrayMenu = new int[parameter];
}