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
ExpenseItem[] myBills = new ExpenseItem[4];
and this related class:
public class ExpenseItem {
//========================
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
private static String myItem = "";
private static int myPeriod = 0;
private static double myPeriodAmount = 0.0;
don't think they should be static
private static int myPeriod = 0;
private static double myPeriodAmount = 0.0;
don't think they should be static
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=pAmoun t;
}
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(blanknu ll(request .getParame ter(myItem )));
myAmount = "Amount" + i;
myBills[i].setPeriodAmount (Double.pa rseDouble( blanknull( request.ge tParameter (myAmount) )));
}
Thanks.
/**
* The ExpenseItem class implements a bill object
*/
public class ExpenseItem {
//========================
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=pAmoun
}
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(blanknu
myAmount = "Amount" + i;
myBills[i].setPeriodAmount
}
Thanks.
whats the error?
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.JasperEx ception: empty String org.apache.jasper.servlet. JspServlet Wrapper.se rvice(JspS ervletWrap per.java:3 72) org.apache.jasper.servlet. JspServlet .serviceJs pFile(JspS ervlet.jav a:292) org.apache.jasper.servlet. JspServlet .service(J spServlet. java:236) javax.servlet.http.HttpSer vlet.servi ce(HttpSer vlet.java: 802) sun.reflect.GeneratedMetho dAccessor1 46.invoke( Unknown Source) sun.reflect.DelegatingMeth odAccessor Impl.invok e(Delegati ngMethodAc cessorImpl .java:25) java.lang.reflect.Method.i nvoke(Meth od.java:58 5) org.apache.catalina.securi ty.Securit yUtil$1.ru n(Security Util.java: 239) java.security.AccessContro ller.doPri vileged(Na tive Method) javax.security.auth.Subjec t.doAsPriv ileged(Sub ject.java: 517) org.apache.catalina.securi ty.Securit yUtil.exec ute(Securi tyUtil.jav a:266) org.apache.catalina.securi ty.Securit yUtil.doAs Privilege( SecurityUt il.java:15 7)
root cause java.lang.NumberFormatExce ption: empty String sun.misc.FloatingDecimal.r eadJavaFor matString( FloatingDe cimal.java :994) java.lang.Double.parseDoub le(Double. java:482) org.apache.jsp.testfiles.F OK_005fKAM _005f1621. Asgn_005f5 a_005fFok_ jsp._jspSe rvice(Asgn _005f5a_00 5fFok_jsp. java:105) org.apache.jasper.runtime. HttpJspBas e.service( HttpJspBas e.java:94) javax.servlet.http.HttpSer vlet.servi ce(HttpSer vlet.java: 802) org.apache.jasper.servlet. JspServlet Wrapper.se rvice(JspS ervletWrap per.java:3 24) org.apache.jasper.servlet. JspServlet .serviceJs pFile(JspS ervlet.jav a:292) org.apache.jasper.servlet. JspServlet .service(J spServlet. java:236) javax.servlet.http.HttpSer vlet.servi ce(HttpSer vlet.java: 802) sun.reflect.GeneratedMetho dAccessor1 46.invoke( Unknown Source) sun.reflect.DelegatingMeth odAccessor Impl.invok e(Delegati ngMethodAc cessorImpl .java:25) java.lang.reflect.Method.i nvoke(Meth od.java:58 5) org.apache.catalina.securi ty.Securit yUtil$1.ru n(Security Util.java: 239) java.security.AccessContro ller.doPri vileged(Na tive Method) javax.security.auth.Subjec t.doAsPriv ileged(Sub ject.java: 517) org.apache.catalina.securi ty.Securit yUtil.exec ute(Securi tyUtil.jav a:266) org.apache.catalina.securi ty.Securit yUtil.doAs Privilege( SecurityUt il.java:15 7)
note The full stack trace of the root cause is available in the Apache Tomcat/5.0.27 logs. Apache Tomcat/5.0.27
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception org.apache.jasper.JasperEx
root cause java.lang.NumberFormatExce
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.pa rseDouble( blanknull( request.ge tParameter (myAmount) )));
looks like myAmount is blank
you should add a condition to check that
looks like myAmount is blank
you should add a condition to check that
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
for (int i=0; i