似乎无法在java中对对象数组进行排序

似乎无法在java中对对象数组进行排序,java,arrays,sorting,compare,Java,Arrays,Sorting,Compare,这是填充数组的对象的类 class InventoryItem implements Comparable<InventoryItem>{ String name; double price = 0.0; int quantity = 0; int itemCount = 0; public InventoryItem(String newName, double newPrice, int newQuantity){ name = newName; price =

这是填充数组的对象的类

class InventoryItem implements Comparable<InventoryItem>{
String name;
double price = 0.0;
int quantity = 0;
int itemCount = 0;
public InventoryItem(String newName, double newPrice, int newQuantity){
    name = newName;
    price = newPrice;
    quantity = newQuantity;
    itemCount++;
}

public String toString(){
    String output = name + ", at " + Program4.moneyString(price) + " each.";
    output += " We have " + getQuantity() + " of them.\n";
    return output;
}

public String getName(){
    return name;
}

public int getQuantity(){
    return quantity;
}

public double getPrice(){
    return price;
}

public int compareTo(InventoryItem other){
    return getName().compareTo(other.getName());
}
}
这给了我错误

Exception in thread "main" java.lang.NullPointerException
at java.util.ComparableTimSort.binarySort(Unknown Source)
at java.util.ComparableTimSort.sort(Unknown Source)
at java.util.Arrays.sort(Unknown Source)
at CSC120Program4.Program4.sortByName(Program4.java:97)
at CSC120Program4.Program4.processUserChoice(Program4.java:121)
at CSC120Program4.Program4.main(Program4.java:134)
我没有足够的经验知道如何解决这个问题或者我做错了什么

编辑:正在生成的阵列:

static InventoryItem[] inventoryItems = new InventoryItem[100];
public static void readAndStoreInventory(InventoryItem[] items, Scanner input){

    String[] params = new String[3];
    while(input.hasNextLine()){
        String name = "";
        double value = 0.0;
        int quantity = 0;
        params = (input.nextLine().split("#"));
        name = params[0];
        value = Double.parseDouble(params[1]);
        quantity = Integer.parseInt(params[2]);
        items[lastInUse + 1] = new InventoryItem(name, value, quantity);
        lastInUse++;
    }
    input.close();
正在读取的文件如下所示:

wooden baseball bat#3.67#10
rubber mallet#5.50#20
duffel bag#10.20#11
gold bar#9999.99#1
hitman rental#20000.00#99
minivan#15000.21#3
half full cup#2.00#10
half empty cup#10.00#10
lawn chair#12.34#100
used golf ball#0.99#2
cosby sweater#250.00#1
CSC120 cheatsheet#0.01#1
线程“main”java.lang.NullPointerException中出现异常

可能意味着
null
正在其上访问属性。可能在这里:

public int compareTo(InventoryItem other){
  return getName().compareTo(other.getName());
}
因此,
名称
中有一个为空。所以可能在这里:

params = (input.nextLine().split("#"));
name = params[0];

InventoryItem数组初始化为100,如果您没有读取100行,那么对于不存在的索引,您将有空指针。您需要使用列表,而不是java数组

List<InventoryItem> inventoryItems = new List<InventoryItem>();

您正在创建一个包含100个元素的数组,但只创建了几个项目。 因此,当您对它进行排序时,它也会对这些空对象进行排序。为此,您应该使用列表

您还可以尝试添加空ptr检查:

public int compareTo(InventoryItem other){
if (other != null){
    return getName().compareTo(other.getName());
}
return 0
}
一个糟糕的修复方法是获取元素的大小并只对添加的元素进行排序:(更好的方法是从一开始就使用列表)

publicstaticvoidsortbyname(InventoryItem[]数组){
int计数器=0;
for(int i=0;i
数组。排序(对象[])
要求数组中的所有元素都是
!=空
,因为它要求

数组中的所有元素必须相互可比(即e1.compareTo(e2)

您看到的是
null的结果。比较(某物)

您的
InventoryItem[]array
可能有100个插槽,但并非所有插槽都已填满

要解决这个问题,你可以

  • 将非空数据复制到大小正确的数组中
  • 仅通过对填充的子集进行排序(使用
    0,lastinexused
    作为参数)
  • 提供一个可以处理
    null
    比较器
    。使用该比较器,算法不再是
    null。比较到(某物)
    而是
    比较器。比较(null,某物)
    不会产生NPE。请参阅
  • 使用
    列表

您的数组在代码中的什么位置?我看不到任何数组在您的代码中初始化。更新了更多详细信息。在-1@JoshPeel,这不应该解决基础问题。这只是检查数组中的值是否不为null。@ryekayo实际上应该解决问题。
null
元素应该是完成后,
array
就结束了。谢谢。我们必须使用array,因为这是我教授的作业指导原则,所以很遗憾,使用列表不是一个选项。我知道这很愚蠢。但是使用索引值使它工作得完美无瑕
inventoryItems.add(new InventoryItem(name, value, quantity));
public int compareTo(InventoryItem other){
if (other != null){
    return getName().compareTo(other.getName());
}
return 0
}
public static void sortByName(InventoryItem[] array){
    int counter = 0;
    for (int i = 0; i < array.length; i ++)
            if (array[i] != null){
            counter ++;
            break;
        }
    Arrays.sort(array, 0, counter-1);
}