0

I have already determined the list is empty as suggested by this link as below.

   <h:outputText value="#{not empty purchasesBean.purchasesList}" />

and showed false,which means the list is empty. I am unable to fix my java code to fill the list, the method is shown below. NOTE:There is code that is commented out just to show different 'data sets' i have tried but did not work.

  import java.io.Serializable;
  import java.math.BigDecimal;
  import java.util.ArrayList;
  import java.util.List;
  import javax.annotation.ManagedBean;
  import javax.annotation.PostConstruct;
  import javax.faces.bean.SessionScoped;

  import javax.faces.bean.ViewScoped;
@ManagedBean
@SessionScoped
//@ViewScoped
public class PurchasesBean  implements Serializable{
   List<Purchases> purchasesList; // = new ArrayList();
   Purchases purchases;


    @PostConstruct
    public void init(){
    
        this.purchasesList = new ArrayList<Purchases>();
    
   //====================== first data set ==============================        
    
        Purchases p1 = new Purchases("Hoop Iron","",new BigDecimal(100),new BigDecimal(100),new BigDecimal(100));
        Purchases p2 = new Purchases("Hacksaw Blades","",new BigDecimal(100),new BigDecimal(100),new BigDecimal(100));
        Purchases p3 = new Purchases("Hacksaw Blades","",new BigDecimal(100),new BigDecimal(100),new BigDecimal(100));
        Purchases p4 = new Purchases("Hacksaw Blades","",new BigDecimal(100),new BigDecimal(100),new BigDecimal(100));
        Purchases p5 = new Purchases("Hacksaw Blades","",new BigDecimal(100),new BigDecimal(100),new BigDecimal(100));
        Purchases p6 = new Purchases("Hacksaw Blades","",new BigDecimal(100),new BigDecimal(100),new BigDecimal(100));
    
       this.purchasesList.add(p1);
       this.purchasesList.add(p2);
       this.purchasesList.add(p3);
       purchasesList.add(p4);
       purchasesList.add(p5);
       purchasesList.add(p6);
    
    
         //======================  second data set  ==========================        
    
 //        
 //        purchasesList.add(new Purchases("Hoop Iron","",new BigDecimal(100),new BigDecimal(100),new BigDecimal(100)));
 //        purchasesList.add(new Purchases("Hacksaw Blades","",new BigDecimal(100),new BigDecimal(100),new BigDecimal(100)));
    
         //=====================  third data set    =========================
    
 //        purchasesList.add(purchases = new Purchases("Hoop Iron","",new BigDecimal(100),new BigDecimal(100),new BigDecimal(100)));
 //        purchasesList.add(purchases = new Purchases("Hacksaw Blades","",new BigDecimal(100),new BigDecimal(100),new BigDecimal(100)));
    

    }

    public List<Purchases> getPurchasesList() {
         System.out.println("purchases list created........");
       return purchasesList;
    }

    public void setPurchasesList(List<Purchases> purchasesList) {
        this.purchasesList = purchasesList;
    }

    public Purchases getPurchases() {
        return purchases;
    }

    public void setPurchases(Purchases purchases) {
        this.purchases = purchases;
    }
    
 }








    import java.math.BigDecimal;

public class Purchases {

    String itemName;
    String itemDescrip;
    BigDecimal itemPrice;
    BigDecimal itemQty;
    BigDecimal total;

    public Purchases(String itemName,String itemDescrip,BigDecimal itemPrice,BigDecimal itemQty,BigDecimal total){

        this.itemName = itemName;
        this.itemDescrip = itemDescrip;
        this.itemPrice = itemPrice;
        this.itemQty = itemQty;
        this.total = total;
    }

    public String getItemName() {
        return itemName;
    }

    public void setItemName(String itemName) {
        this.itemName = itemName;
    }

    public String getItemDescrip() {
        return itemDescrip;
    }

    public void setItemDescrip(String itemDescrip) {
        this.itemDescrip = itemDescrip;
    }

    public BigDecimal getItemPrice() {
        return itemPrice;
    }

    public void setItemPrice(BigDecimal itemPrice) {
        this.itemPrice = itemPrice;
    }

    public BigDecimal getItemQty() {
        return itemQty;
    }

    public void setItemQty(BigDecimal itemQty) {
        this.itemQty = itemQty;
    }

    public BigDecimal getTotal() {
       return total;
    }

    public void setTotal(BigDecimal total) {
        this.total = total;
    }  
  }

Those two above are my java classes, below is my JSF (xhtml) code Fogive the unused facelets library,it was initially a primefaces data table which i changed into a JSFcore data table to just see where the problem might be but still the data put into the List is not showing at all.

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://xmlns.jcp.org/jsf/html"   
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:p="http://primefaces.org/ui">
     >
    
    <h:head>
        <title>Facelet Title</title>
    </h:head>
    <h:body>
        Hello from Facelets
        <br />
        <h:link outcome="welcomePrimefaces" value="Primefaces welcome page" />
          <h:form>
              <h:outputText value="#{not empty purchasesBean.purchasesList}" />
        
        </h:form>
    </h:body>
</html>

Why is the list empty even after adding objects to it? Your help is appreciated.

Kukeltje
  • 12,223
  • 4
  • 24
  • 47
Afroid1000
  • 149
  • 10
  • 2
    What did you debug, breakpoints etc? What are your imports of the annotations? did you run in development mode? Are you sure you are running the code you think you are running (tried a clean deploy)? – Kukeltje Aug 08 '20 at 07:57
  • Sorry, my bad, as a self taught programmer, i have always overlooked the debugger,Time for me to learn to use it.The imports of the annotations are OK, i have edited the question and included the imports. Its definitely on development mode,its a very simple data table JSF table that has been giving me headache till i found out the list is apparently empty. I have cleaned and runned it again severally. – Afroid1000 Aug 08 '20 at 09:53
  • Sure it is development mode? I'm pretty sure you should get errors then since your imports are NOT OK... – Kukeltje Aug 08 '20 at 10:40
  • @Kukeltje.You are right, my imports are NOT OK. The datatable is now working after correcting. Highly appreciate sir. – Afroid1000 Aug 08 '20 at 10:49
  • 1
    Suggestion: switch to cdi managed beans – Kukeltje Aug 08 '20 at 12:45

0 Answers0