Link to home
Start Free TrialLog in
Avatar of ksfok
ksfok

asked on

Constructor / Accessor / Mutator

Given the following declaration:

ExpenseItem[] myBills = new ExpenseItem[4];

and this related class:

public class ExpenseItem {
//========================================= instance variables
    private static String myItem = "";
    private static int myPeriod = 0;
    private static double myPeriodAmount = 0.0;

Is the object array ExpenseItem[4] automatically instantiated and initialized with the above default values without calling explicit constructor and accessor/mutator code? If not, how should it be instantiated and initialized? Please provide code sample.

Thanks
Avatar of Mick Barry
Mick Barry
Flag of Australia image

no, the array will initially contain nulls

for (int i=0; i
   private static String myItem = "";
    private static int myPeriod = 0;
    private static double myPeriodAmount = 0.0;

don't think they should be static
Avatar of ksfok
ksfok

ASKER

Here is my object code:

/**
 * The ExpenseItem class implements a bill object
 */
public class ExpenseItem {
//========================================= instance variables
    private String myItem = "";
    private int myPeriod = 0;
    private double myPeriodAmount = 0.0;

 public void setItem(String item){
  this.myItem=item;
 }
 public void setPeriod(int period){
  this.myPeriod=period;
 }
 public void setPeriodAmount(double pAmount){
  this.myPeriodAmount=pAmount;
 }
 public String getItem() {
  return this.myItem;
 }
 public int getPeriod() {
  return this.myPeriod;
 }
 public double getPeriodAmount() {
  return this.myPeriodAmount;
 }
}
Why the following code triggers accessor exception?

// Initialize object array
for (int i=0; i < 4; i++){
 ExpenseItem obj = new ExpenseItem();
 myBills[i] = obj;
 myItem = "Item" + i;
 myBills[i].setItem(blanknull(request.getParameter(myItem)));
 myAmount = "Amount" + i;
 myBills[i].setPeriodAmount(Double.parseDouble(blanknull(request.getParameter(myAmount))));
}
 
Thanks.
 
whats the error?

Avatar of ksfok

ASKER

HTTP Status 500 -
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception org.apache.jasper.JasperException: empty String org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:372) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:292) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:236) javax.servlet.http.HttpServlet.service(HttpServlet.java:802) sun.reflect.GeneratedMethodAccessor146.invoke(Unknown Source) sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) java.lang.reflect.Method.invoke(Method.java:585) org.apache.catalina.security.SecurityUtil$1.run(SecurityUtil.java:239) java.security.AccessController.doPrivileged(Native Method) javax.security.auth.Subject.doAsPrivileged(Subject.java:517) org.apache.catalina.security.SecurityUtil.execute(SecurityUtil.java:266) org.apache.catalina.security.SecurityUtil.doAsPrivilege(SecurityUtil.java:157)

root cause java.lang.NumberFormatException: empty String sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:994) java.lang.Double.parseDouble(Double.java:482) org.apache.jsp.testfiles.FOK_005fKAM_005f1621.Asgn_005f5a_005fFok_jsp._jspService(Asgn_005f5a_005fFok_jsp.java:105) org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:94) javax.servlet.http.HttpServlet.service(HttpServlet.java:802) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:324) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:292) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:236) javax.servlet.http.HttpServlet.service(HttpServlet.java:802) sun.reflect.GeneratedMethodAccessor146.invoke(Unknown Source) sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) java.lang.reflect.Method.invoke(Method.java:585) org.apache.catalina.security.SecurityUtil$1.run(SecurityUtil.java:239) java.security.AccessController.doPrivileged(Native Method) javax.security.auth.Subject.doAsPrivileged(Subject.java:517) org.apache.catalina.security.SecurityUtil.execute(SecurityUtil.java:266) org.apache.catalina.security.SecurityUtil.doAsPrivilege(SecurityUtil.java:157)

note The full stack trace of the root cause is available in the Apache Tomcat/5.0.27 logs. Apache Tomcat/5.0.27
> myBills[i].setPeriodAmount(Double.parseDouble(blanknull(request.getParameter(myAmount))));

looks like myAmount is blank

you should add a condition to check that

SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial