1

I am trying to create a dynamically populated list using the AutoPopulatingList. I also have a form object as shown in the code below:

public class CDNoteForm{

    private AbstractCDNote cdnote;
    private List<? extends AbstractInvoiceItem> invoiceItemList ;


    public CDNoteForm() {
        super();
    }
    public CDNoteForm(String type){
        super();
        if(type.equals("cdnote")){
            this.invoiceItemList =  new AutoPopulatingList<CDNoteItem>(CDNoteItem.class);
        }
        else if(type.equals("vendorcdnote")) {          
            this.invoiceItemList =  new AutoPopulatingList<VendorCDNoteItem>(VendorCDNoteItem.class);
        }

    }

    public AbstractCDNote getCdnote() {
        return cdnote;
    }
    public void setCdnote(AbstractCDNote cdnote) {
        this.cdnote = cdnote;
    }
    public List<? extends AbstractInvoiceItem> getInvoiceItemList() {
        return invoiceItemList;
    }
    public void setInvoiceItemList(List<? extends AbstractInvoiceItem> invoiceItemList) {
        this.invoiceItemList = invoiceItemList;
    }
}

The form object is being declared in a method in my controller as shown below.

@RequestMapping(value ="/cdnote/create"  , method = RequestMethod.GET)
    public String getCreateCDNotePage(Model model){

    CDNoteForm cdnoteForm = new CDNoteForm("cdnote");
    model.addAttribute("CDNoteForm" , cdnoteForm);
    return "AddCDNote";
}

The 'invoiceItemList' is being referred in the jsp like this

<form:input type="text" id="Quantity0"  path="invoiceItemList[0].quantity" class="form-control"></form:input>                       

The two classes 'CDNoteItem' and 'VendorCDNoteItem' extend the abstract class 'AbstractInvoiceItem'. Now I don't get any error on compilation but when I try to run it and visit the page I get the following error on console.

SEVERE: Servlet.service() for servlet [SpringGST] in context with path [/SpringGST] threw exception [org.springframework.beans.InvalidPropertyException: Invalid property 'invoiceItemList[0]' of bean class [com.gst.FormObjects.CDNoteForm]: Illegal attempt to get property 'invoiceItemList' threw exception; nested exception is org.springframework.beans.NullValueInNestedPathException: Invalid property 'invoiceItemList' of bean class [com.gst.FormObjects.CDNoteForm]: Could not instantiate property type [com.gst.models.AbstractInvoiceItem] to auto-grow nested property path: org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.gst.models.AbstractInvoiceItem]: Is it an abstract class?; nested exception is java.lang.InstantiationException] with root cause
org.springframework.beans.NullValueInNestedPathException: Invalid property 'invoiceItemList' of bean class [com.gst.FormObjects.CDNoteForm]: Could not instantiate property type [com.gst.models.AbstractInvoiceItem] to auto-grow nested property path: org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.gst.models.AbstractInvoiceItem]: Is it an abstract class?; nested exception is java.lang.InstantiationException

I don't understand why it is trying to instantiate the abstract class 'AbstractInvoiceItem' when the 'invoiceItemlist' was initialized with a reference to the autopopulating list of its child class

2 Answers2

0

Can you use invoiceItemList.get(0) instead of invoiceItemList[0] and try

  • That doesn't work. I get this error. "org.springframework.beans.NotReadablePropertyException: Invalid property 'invoiceItemList.get(0)' of bean class [com.gst.FormObjects.CDNoteForm]: Bean property 'invoiceItemList.get(0)' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?" – Devashish Khattar Dec 11 '17 at 10:52
0

Since invoiceItemList is private, you are getting the exception. You need to either make it public(not recommended) or you need to use getters and setters to access it.

private List<? extends AbstractInvoiceItem> invoiceItemList ;

public List<? extends AbstractInvoiceItem> getInvoiceItemList(){
  return invoiceItemList;
}
public setInvoiceItemList(List<? extends AbstractInvoiceItem> list){
invoiceItemList= list;
}

Now you can access like this:

<form:input type="text" id="Quantity0"  
path="cdnoteForm.getInvoiceItemList().get(0).quantity" class="form-control">
</form:input>        
  • I am getting the following error: "org.springframework.beans.NotReadablePropertyException: Invalid property 'CDNoteForm' of bean class [com.gst.FormObjects.CDNoteForm]: Bean property 'CDNoteForm' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter? " My spring form start tag reads like this : " " – Devashish Khattar Dec 11 '17 at 11:18
  • in place of cdnoteForm use the name of your form bean – Shiksha Verma Dec 11 '17 at 11:25
  • In place of "cdnoteForm" I did use "CDNoteForm" which is the name of my form bean. – Devashish Khattar Dec 11 '17 at 12:14