Results 1 to 6 of 6
  1. #1
    Kareng is offline Member
    Join Date
    Aug 2009
    Posts
    35
    Rep Power
    0

    Default How do I make reference to CartBean?

    Hi forum experts,

    Due to my poor Java foundation, I'm here once again to seek help on some Java programming problem.

    I have this CartBean which contains method of calculating the OrderTotal - calculateOrderTotal() and setOrderTotal(dblTotal).
    I also have a CartItemBean class where I have the setter and getter in which getTotalCost() returns dblTotalCost.
    So, at my SetExpressService java class, I would like to make reference to this dblTotal.

    What I did is I create a new object and try to make reference to this dblTotal from the calculateOrderTotal() but obviously it's the wrong way of doing thing.

    Object details = new CartBean();
    String amount = details.dblTotal;

    I hope someone can tell me how to achieve the referencing portion.

    Thank you.

  2. #2
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    6

    Default Re: How do I make reference to CartBean?

    Well, your declared type is Object which means that you wont know about any of CartBean's methods or fields without casting. Instead, declare details as a CartBean instead of an object. Secondly, shouldn't you be using getTotalCost() to access dblTotal? Is dblTotal even public? Good OOP states that only the accessor should be used, and then field itself should be private (or protected).

  3. #3
    Kareng is offline Member
    Join Date
    Aug 2009
    Posts
    35
    Rep Power
    0

    Default Re: How do I make reference to CartBean?

    Quote Originally Posted by quad64bit View Post
    Well, your declared type is Object which means that you wont know about any of CartBean's methods or fields without casting. Instead, declare details as a CartBean instead of an object. Secondly, shouldn't you be using getTotalCost() to access dblTotal? Is dblTotal even public? Good OOP states that only the accessor should be used, and then field itself should be private (or protected).
    Thank you for your pointers.

    Now, I have changed it to the following:

    CartItemBean details = new CartItemBean();
    amount = details.getTotalCost();

    but, Netbeans IDE still gives me error : incompatible types.required : java.lang.String...found: double

    Yes. dblTotal or rather dblTotalCost(I missed out the Cost earlier) is public because if I change it to private then the other CartBean class will be affected and it won't be read.


    protected void calculateOrderTotal() {
    double dblTotal = 0;
    for(int counter=0;counter<alCartItems.size();counter++) {
    CartItemBean cartItem = (CartItemBean) alCartItems.get(counter);
    dblTotal+=cartItem.getTotalCost();

    public double getTotalCost() {
    return dblTotalCost;


    How do I resolve problem like this?

    I tried double.parseDouble.details.dblTotalCost()[missed out Cost earlier also] but it seems the syntax is very wrong....

  4. #4
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,746
    Rep Power
    19

    Default Re: How do I make reference to CartBean?

    Please use [code] tags [/code] when posting code.

    What is the exact error you are getting, and what line does it occur on?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,304
    Blog Entries
    7
    Rep Power
    20

    Default Re: How do I make reference to CartBean?

    Probably (most likely) your 'amount' variable has type String, whille your getTotalCost() method returns a double (it's all in the compiler error message).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    Kareng is offline Member
    Join Date
    Aug 2009
    Posts
    35
    Rep Power
    0

    Default Re: How do I make reference to CartBean?

    Quote Originally Posted by JosAH View Post
    Probably (most likely) your 'amount' variable has type String, whille your getTotalCost() method returns a double (it's all in the compiler error message).

    kind regards,

    Jos
    I found the solution : To change to String:

    amount = Double.toString(details.getTotalCost());

Similar Threads

  1. Cannot make a static reference to the non-static method
    By Reskaillev in forum New To Java
    Replies: 4
    Last Post: 08-02-2011, 12:42 AM
  2. Cannot make a static reference...
    By Guy in forum New To Java
    Replies: 12
    Last Post: 07-28-2011, 07:22 AM
  3. Replies: 4
    Last Post: 07-23-2011, 08:08 PM
  4. Replies: 10
    Last Post: 11-14-2010, 07:06 AM
  5. Replies: 7
    Last Post: 09-24-2010, 05:09 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •