Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Constructor / Accessor / Mutator

Posted on 2009-02-19
8
Medium Priority
?
815 Views
Last Modified: 2012-05-06
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
0
Comment
Question by:ksfok
  • 5
  • 2
8 Comments
 
LVL 92

Expert Comment

by:objects
ID: 23688430
no, the array will initially contain nulls

for (int i=0; i
0
 
LVL 92

Expert Comment

by:objects
ID: 23688437
   private static String myItem = "";
    private static int myPeriod = 0;
    private static double myPeriodAmount = 0.0;

don't think they should be static
0
 

Author Comment

by:ksfok
ID: 23688546
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 92

Expert Comment

by:objects
ID: 23688561
whats the error?

0
 

Author Comment

by:ksfok
ID: 23688585
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
 
LVL 92

Expert Comment

by:objects
ID: 23688745
> myBills[i].setPeriodAmount(Double.parseDouble(blanknull(request.getParameter(myAmount))));

looks like myAmount is blank

you should add a condition to check that

0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 600 total points
ID: 23689640
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
 
LVL 92

Accepted Solution

by:
objects earned 900 total points
ID: 23689657
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

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses
Course of the Month15 days, 10 hours left to enroll

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question