0

this is the class for the objects that fill my array

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

and my call to sort the array

public static void sortByName(InventoryItem[] array){
    Arrays.sort(array);
}

this gives me the errors

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)

I'm not quite experienced enough to know how to fix this or really what I'm doing wrong.

edit: array being built:

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

the file being read is as follows:

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
2
  • Where is your array in the code? I don't see any arrays initialized in your code.. Commented Nov 12, 2015 at 20:08
  • updated with more details Commented Nov 12, 2015 at 20:12

4 Answers 4

2

You are creating an array with 100 elements but you only creating a few items. So when you sort it, it will also sort these null objects. You should use a List for this purpose.

You could also try to add a null-ptr check:

public int compareTo(InventoryItem other){
if (other != null){
    return getName().compareTo(other.getName());
}
return 0
}

A dirty fix would be to get the size of the elements and sort only the added elements: (Better way would be to use a list from the start)

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);
}
Sign up to request clarification or add additional context in comments.

2 Comments

@JoshPeel, this is not supposed to fix the underlying issue. This is simply checking if the values within the array are not null..
@ryekayo actually it should fix the issue...the null elements should be at the end of the array when done.
0

Arrays.sort(Object[]) expects that all elements in the array are != null since it demands that

all elements in the array must be mutually comparable (that is, e1.compareTo(e2) ...

What you see is the result of null.compareTo(something).

Your InventoryItem[] array has presumable 100 slots, but not all of them are filled.

To fix that, you could

1 Comment

THANK YOUUUUU. We have to use arrays due to it being to my professors guidelines for the assignment so using a list wasnt an option unfortunately. its dumb i know. but using the index values made it work flawlessly
0

Exception in thread "main" java.lang.NullPointerException

Probably means a null is having properties accessed on it. Probably in here:

public int compareTo(InventoryItem other){
  return getName().compareTo(other.getName());
}

So one of the name is null. So probably here:

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

Comments

0

The InventoryItem array is initialized as 100, and if you don't read in 100 rows, then you'll have null pointers for the non-existent indexes. You'll need to use a List, rather than a java array.

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

then when you add them do something like this,

inventoryItems.add(new InventoryItem(name, value, quantity));

1 Comment

its initialized at -1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.