reset password
Author Message
harry_520
Posts: 76
Posted 15:24 Feb 13, 2013 |

I have a class called PledgesEntry, in this class I made getter and setter for the Integer variable pledged. And under the projects entry class, I made a list called List<PledgesEntry> pledge. I just cannot figure out how to I calculate the amount pledged inside the projects entry class. Can anyone help me? What I did below brought me error.

    public Integer getAmountPledged()
    {

       Integer amount = 0;
       for (PledgesEntry pledged: pledge)
         amount += pledged.getPledged();
       return amount;
    }

Last edited by harry_520 at 15:26 Feb 13, 2013.
cysun
Posts: 2935
Posted 15:40 Feb 13, 2013 |

This is the right way to do it. What error did you get?

harry_520
Posts: 76
Posted 16:45 Feb 13, 2013 |

Dr. Sun. This is the error message I got.

 

HTTP Status 500 -


type Exception report

message

description The server encountered an internal error that prevented it from fulfilling this request.

exception

java.lang.NullPointerException
	cs320Starter.model.ProjectsEntry.getAmountPledged(ProjectsEntry.java:113)
	cs320Starter.servlet.Projects.doGet(Projects.java:72)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:728)

 

note The full stack trace of the root cause is available in the Apache Tomcat/7.0.34 logs.

cysun
Posts: 2935
Posted 17:12 Feb 13, 2013 |

Maybe your pledge list is not initialized. Remember that you need to do "new ArrayList<PledgeEntry>()" to create an empty list before using it for anything.

harry_520
Posts: 76
Posted 17:32 Feb 13, 2013 |

Thanks, Dr. Sun. I'll try it.