0

I have

     PurchaseOrder objectPO = new PurchaseOrder(objectPO.getDateOfOrder(), 
                            "CGL-1234" ,objectPO.getProductType(), 250, 5);
 objectPO.toString();

But I get error that objectPO might not have been initialized

PurchaseOrder class

I have looked through similar posts but couldnt find solution regarding my error. How can I initialize objPO?

Wub
  • 45
  • 11
  • You're using `objectPO` as an argument to initialize `objectPO` – Jerfov2 Mar 18 '17 at 20:55
  • Possible duplicate of [Variable might not have been initialized error](http://stackoverflow.com/questions/2448843/variable-might-not-have-been-initialized-error) – Roma Khomyshyn Mar 18 '17 at 20:55
  • @RomaKhomyshyn I think this is different (at least for that link) because he initialized the variable, but he just did it wrong (i.e. using variable to declare itself) – Jerfov2 Mar 18 '17 at 20:57

1 Answers1

1

The issue is that you can't invoke objectPO.getDateOfOrder() and objectPO.getProductType() methods before even objectPO object is created i.e., you are trying to create objectPO object by using the objectPO itself (which is part of your PurchaseOrder constructor call).

Rather, first create the objects for OrderDate (like orderDateObj) and Product (like productObj) by using the constructors of those classes first as shown below and then pass those objects to create PurchaseOrder object.

OrderDate orderDateObj = new OrderDate(...);//create object for OrderDate
Product productObj = new Product(...);//create object for Product
PurchaseOrder objectPO = new PurchaseOrder(orderDateObj, 
                            "CGL-1234" , productObj, 250, 5);
Vasu
  • 21,832
  • 11
  • 51
  • 67