1

I've checked everything several time, can't get where I am wrong..

Main class:

    try
    {
        File productData = new File("productData.txt");
        Product [] consideredRange = InputFileData
                                          .readProductDataFile(productData);

        ElectronicsEquipmentSupplier management = 
                   new ElectronicsEquipmentSupplier(1, 12, consideredRange);  


        File customerData = new File("CustomerData.txt");
        Scanner fileScan = new Scanner(customerData);

        while(fileScan.hasNext())
            management.addNewCustomer(InputFileData.
                                                readCustomerData(fileScan));       

        management.addNewPurchaseOrder("21/01/12", "PSC-1235", "kD/9767", 50);            

    }
    catch(Exception e)
    {
        System.out.println(e);
        e.printStackTrace();
    }   

InputFileData class works perfectly. I have created an object of ElectronicsEquipmentSupplier with a consideredRange of products. Also added customers to a customersList.

Here is ElectronicsEquipmentSupplier class:

    public class ElectronicsEquipmentSupplier 
    {
         private int currentMonth;
         private int currentYear;
         private Product [] productRange;
         private CustomerDetailsList customersList;
         private PurchaseOrderList currentYearList;
         private PurchaseOrderList lastYearList;


    public ElectronicsEquipmentSupplier(int currentMonth, int currentYear, Product [] range)
    {
         this.currentMonth = currentMonth;
         this.currentYear = currentYear;
         productRange = new Product[range.length];
         customersList = new CustomerDetailsList();
         currentYearList = new PurchaseOrderList();
         lastYearList = new PurchaseOrderList();
    }

    public void addNewPurchaseOrder(String dateStr, String customerID, 
         String productCode, int qty) throws IncorrectPurchaseOrderException
    {
    // check for positive order quantity
    if(qty < 1)
        throw new IncorrectPurchaseOrderException("Order quantity must be"
                                                            + " positive!");

    // check for the product code in given range and get that product 
    Product foundProduct = null;

    for(int i = 0; i < productRange.length; i++)
    {   
        if(productRange[i].getProductCode().equals(productCode))
        {   
            foundProduct = productRange[i];
            break;
        }
    }
    if(foundProduct == null)
        throw new IncorrectPurchaseOrderException("Product code is not in"
                                                   + " the product range!");
    try
    {   
        // creating OrderDate object and getting appropriate discount
        OrderDate newDate = new OrderDate(dateStr);
        int discount = customersList.findCustomer(customerID).
                                                          getDiscountRate();

        // creating purchase order and adding it to a list
        PurchaseOrder givenOrder = new PurchaseOrder(newDate, customerID, 
                                               foundProduct, qty, discount);

        currentYearList.addPurchaseOrder(givenOrder);

        // updating the record of purchasing customer
        int priceValue = givenOrder.getFullPriceValue();
        customersList.findCustomer(customerID)
                                        .updateTotalOrdersValue(priceValue);
    }
    catch(Exception e)
    {   
        throw new IncorrectPurchaseOrderException("The problem is with: "
                                                                + "\n" + e);
    }
}

It shows that I've got NullPointerException at: if(productRange[i].getProductCode().equals(productCode))

and in the main class at:

management.addNewPurchaseOrder("21/01/12", "PSC-1235", "kD/9767", 50);

Can't get why, as I have all required info..

Thank you!

Update 1:

Added this to the main method to solve first issue:

            for(int i = 0; i < consideredRange.length; i++)
            management.getProductRange()[i] = consideredRange[i];

But now the ID of a customer cannot be found... That's the method in CustomerDetailsList class, which throws exception:

public CustomerDetails findCustomer(String givenID)
                                            throws CustomerNotFoundException
{   
    int i = 0;
    boolean match = false;

    while(!match && i < listOfCustomerDetails.size())
    {
        match = listOfCustomerDetails.get(i).getCustomerID()
                                                           .equals(givenID); 
        i++;
    }

    if(!match)
        throw new CustomerNotFoundException("The provided ID has not been"
                                                                + " found");
    else
        return listOfCustomerDetails.get(i);
} 

Update 2: updated .findCustomer() as SMA suggested

9
  • Looks like productRange[i] would be null. You can't call getProductCode() with it. Commented Mar 28, 2015 at 17:19
  • Why? I think I initialized the array of product range with a constructor, isn't it? Commented Mar 28, 2015 at 17:21
  • 1
    You initialized the array itself but not its elements. So they are still null. Commented Mar 28, 2015 at 17:23
  • 1
    After your edit: the solution you chose is not clean. Since you have a constructor and you pass an array to initialise it, you should do the member init in the constructor, not outside. For the second pb, what is the exception? Commented Mar 28, 2015 at 17:58
  • 1
    That's not your question, but the return value from findCustomer() won't be good and can throw an ArrayindexOutOfBoundException: when you have a match, you should not increment i in order to return the found customer. Commented Mar 28, 2015 at 18:23

3 Answers 3

2

You are trying to initialize product in constructor like

productRange = new Product[range.length];

And then using it like:

if(productRange[i].getProductCode().equals(productCode))

Now you allocated space for your array but individual array elements i.e. products are not initialized and hence you get NullPointerException. To resolve the issue, you could do something like:

productRange[i] = new Product(..);//and then use it
Sign up to request clarification or add additional context in comments.

7 Comments

Thank you! But now I've got another problem with: int discount = customersList.findCustomer(customerID). getDiscountRate();
It throws curtomerNotFoundException, which means that there is a problem with a customersList... Any idea?
I dont see which code throws that exception and where you catch it. This should be a followup question, so i would suggest to close this one and open a new one
loop over your listOfCustomerDetails rather than just checking first element of the list.
you mean in .findCustomer method? The is an incremet i++, or it doesn't work this way?
|
1

Most likely because productRange[i] has not been initialized.

Comments

1

In your constructor you need to fully initialise the productRange array. Right now you are just creating an array of null references.

 public ElectronicsEquipmentSupplier(int currentMonth, 
                                     int currentYear, 
                                     Product [] range) {
     this.currentMonth = currentMonth;
     this.currentYear = currentYear;
     productRange = new Product[range.length];
     for (int i = 0; i < productRange.length; i++) {
        productRange[i] = range[i];
     }
     customersList = new CustomerDetailsList();
     currentYearList = new PurchaseOrderList();
     lastYearList = new PurchaseOrderList();
 }

The above solution build a new array which reference the same objects as the range array passed to the constructor.

You may just want to reference the array without any allocation, e.g.

 public ElectronicsEquipmentSupplier(int currentMonth, 
                                     int currentYear, 
                                     Product [] range) {
     //...
     productRange = range;
     //...
 }

Or do a deep copy of the range array, assuming you have either a Product#clone() method or a Product constructor that takes a Product parameter, e.g.

 public ElectronicsEquipmentSupplier(int currentMonth, 
                                     int currentYear, 
                                     Product [] range) {
     //...
     productRange = new Product[range.length];
     for (int i = 0; i < productRange.length; i++) {
        productRange[i] = new Product(range[i]);
     }
     //...
 }

The choice between these different methods depends on how the ElectronicsEquipmentSupplier class is used.

Comments

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.