为什么不能在第38行输入很多itemNumber

时间:2019-02-17 10:03:39

标签: java netbeans-8

当用户应该为itemNumber输入唯一的编号时,我的代码就会中断,但是NetBeans中没有显示错误,有人可以帮我解决这个问题吗?

package pre_release;
import java.util.*; 

public class Pre_Release {

    public static void main(String[] args) {
        int numberOfItems = 0;
        int number;
        int bidNumber;
        double highestBid = 0;
        Scanner userInput = new Scanner (System.in);
        Scanner userString = new Scanner (System.in); 
        Scanner userDouble = new Scanner (System.in); 
        Scanner userBuyer = new Scanner (System.in); 
        Scanner userInt = new Scanner (System.in);
        Scanner userItem = new Scanner (System.in);
        do{
            System.out.println("Please enter the the amount of items for the auction. Needs to be more than or equal to 10"); 
            numberOfItems = userInput.nextInt();
        } while(numberOfItems<10);
        String[] description = new String[numberOfItems];  
        double[] reservePrice = new double[numberOfItems];
        double[] bid = new double[numberOfItems]; 
        int[] itemNumber;
        itemNumber = null;
        int[] buyNumber = new int[numberOfItems]; 
        for(int count=0;count<numberOfItems;count++){
            System.out.println("Input a number for each item number");
            itemNumber[count] = userInt.nextInt();
        }
        for(int count=0;count<numberOfItems;count++){
            bidNumber=0;
            System.out.println("Please give your item a reserved price");
            reservePrice[count] = userDouble.nextDouble(); 
            System.out.println("Please describe your item");
            description[count] = userString.nextLine();
        }
        System.out.println("Please enter the desired item you wish to see");
        number = userInt.nextInt(); 
        for(int count2=0;count2<numberOfItems;count2++){
            if(itemNumber[count2] == number){
                System.out.println("The reserved price is" + reservePrice[numberOfItems]);
                System.out.println(description[numberOfItems]);
            }
            System.out.println("Would you like to put a bid on this item? Needs to be more than" + reservePrice[numberOfItems]);
        }
    }
}

1 个答案:

答案 0 :(得分:1)

还没有看到您的错误消息,但可能是因为数组引用为空而出现了错误

int[] itemNumber;
itemNumber = null

为了解决此问题,请用以下一行替换这些行:

int[] itemNumber = new int[numberOfItems];