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
ksfokAsked:
Who is Participating?
 
objectsCommented:
which is exactly what I already suggested :)

would be better implemented as a new method though, then you simply need:

myBills[i].setPeriodAmount(FormatUtil.getDouble(request.getParameter(myAmount))));


Let me know if you need any help with it :)

0
 
objectsCommented:
no, the array will initially contain nulls

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

don't think they should be static
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

 
ksfokAuthor Commented:
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.
 
0
 
objectsCommented:
whats the error?

0
 
ksfokAuthor Commented:
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
0
 
objectsCommented:
> myBills[i].setPeriodAmount(Double.parseDouble(blanknull(request.getParameter(myAmount))));

looks like myAmount is blank

you should add a condition to check that

0
 
CEHJCommented:
Try the following. Depending on your implementation of 'blanknull', the first if test might not be necessary:
String periodAmount = blanknull(request.getParameter(myAmount));
if (periodAmount != null && (periodAmount = periodAmount.trim()).length() > 0) {
	myBills[i].setPeriodAmount(Double.parseDouble(periodAmount)); 
}
else {
	myBills[i].setPeriodAmount(0.0); 
}
 
// Alternatively
try {
	myBills[i].setPeriodAmount(Double.parseDouble(blanknull(request.getParameter(myAmount))));
}
catch(NumberFormatException e) {
	myBills[i].setPeriodAmount(0.0);
}

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.