• Post Reply Bookmark Topic Watch Topic
  • New Topic

Accessing fields in a bean within a bean  RSS feed

 
Michael Piaser
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am going to try to narrow the code down so I can illustrate my question without confusing the issue by the thousands of lines of code.

I have a bean called PackBean - I have removed all but a couple of the fields (id, name and charges).
Charges refers to another bean (ElementChargesBean) which is an extention of ChargeBean.
ChargeBean has a field I want to access (amountChargedToEndUser)
I am trying to access the amountChargedToEndUser field and print it's value. The code snipper is below followed by the output. (the beans are below the sample output)



08:42:59,451 INFO [stdout] (http--127.0.0.1-8080-6) OrderManager.java: packagePricing, packBean=com.voxportal.web.device.PackageBean@2873fa43
08:42:59,452 INFO [stdout] (http--127.0.0.1-8080-6) OrderManager.java: packagePricing, charges=[com.voxportal.impl.byo.ElementChargesBean@56335974]
08:42:59,452 INFO [stdout] (http--127.0.0.1-8080-6) OrderManager.java: packagePricing, packBean.retailPrice=0.0
08:42:59,453 INFO [stdout] (http--127.0.0.1-8080-6) com.voxportal.impl.byo.ElementChargesBean@56335974
08:42:59,453 INFO [stdout] (http--127.0.0.1-8080-6) ------------------------- ENTER POPUPPACKAGEPRICE





 
Arun Kumarr
Ranch Hand
Posts: 662
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm sorry, but what is your question and what is that you are trying to achieve, like what do you want to print.
I want to help you, but I'm a little confused from your mail. Can you rephrase your questions and post the code which is not working. I see you have already commented much of the code.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16059
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As I've said before in an answer to one of your other questions, you should implement a toString() method in PackageBean and ElementChargesBean. For example:

 
Michael Piaser
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the question is how do I access the amountChargedToEndUser which is part of Charge Bean which is extended by elementChargesBean which is included in packBean. I can get fields such as retailPrice which is in packBean but I don't know how to get a field which is a bean in a bean (amountChargedToEndUser)
 
Michael Piaser
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper,

I know you said this before but I didn't understand it. Thanks for jumping in and posting more details. In your example, you are using the "id" field which is one that I can get directly without doing anything funny. I don't know how to implement the toString override for a field which is a bean in a bean.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16059
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just call getAmountChargedToEndUser() on your ElementChargesBean. Since it extends ChargeBean, it inherits this method from ChargeBean.
 
Arun Kumarr
Ranch Hand
Posts: 662
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Michael Piaser wrote:the question is how do I access the amountChargedToEndUser which is part of Charge Bean which is extended by elementChargesBean which is included in packBean. I can get fields such as retailPrice which is in packBean but I don't know how to get a field which is a bean in a bean (amountChargedToEndUser)


The method - "getAmountChargedToEndUser" is declared public and should be available to your ElementChargesBean?

Step 1: get the enclosing bean (i assume this is packbean).
Step 2: inside the enclosing bean (pack bean) - get the list of elementChargesBean(s).
Step3: for each elementChargesbean in the list, call the "getAmountChargedToEndUser" method.

This is the easiest explanation i can give. Does that answer your query?
Where are you stuck exactly?
 
Michael Piaser
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper,

When I tried various ways of accessing getAmountChargedtoEndUser(), I get a syntax error so obviously I am not doing something right.

System.out.println("OrderManager.java: packagePricing, packBean.EndUserTest1="+mkp.getAmountChargedToEndUser());
System.out.println("OrderManager.java: packagePricing, packBean.EndUserTest2="+packageBean.getCharges().getAmountChargedToEndUser());
System.out.println("OrderManager.java: packagePricing, packBean.EndUserTest3="+packageBean.getAmountChargedToEndUser());

[ERROR] \VOX\voxware-1.1.14\voxportal\voxportal-web\src\main\java\com\voxportal\web\device\OrderManager.java:[1684,88] error: cannot find symbol
[ERROR] \VOX\voxware-1.1.14\voxportal\voxportal-web\src\main\java\com\voxportal\web\device\OrderManager.java:[1685,109] error: cannot find symbol
[ERROR] \VOX\voxware-1.1.14\voxportal\voxportal-web\src\main\java\com\voxportal\web\device\OrderManager.java:[1686,96] error: cannot find symbol


Arun,

When I try to cycle thru the list for ElementChargesBean, I get a syntax error.

List<ElementChargesBean> mkp = packageBean.getCharges();
for (list obj : mkp) {System.out.println("List="+obj.getAmountChargedToEndUser());};

ERROR] \VOX\voxware-1.1.14\voxportal\voxportal-web\src\main\java\com\voxportal\web\device\OrderManager.java:[1682,10] error: cannot find symbol
 
Arun Kumarr
Ranch Hand
Posts: 662
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[ERROR] \VOX\voxware-1.1.14\voxportal\voxportal-web\src\main\java\com\voxportal\web\device\OrderManager.java:[1684,88] error: cannot find symbol
[ERROR] \VOX\voxware-1.1.14\voxportal\voxportal-web\src\main\java\com\voxportal\web\device\OrderManager.java:[1685,109] error: cannot find symbol
[ERROR] \VOX\voxware-1.1.14\voxportal\voxportal-web\src\main\java\com\voxportal\web\device\OrderManager.java:[1686,96] error: cannot find symbol

- You definitely need to break down your class into modular pieces and move them out of your existing 1686+ lines class file.

Keeping that aside, can you clean up your entire code (delete all class files), recompile them and try running it again.
Again are you running it as a standalone or packaging your code into some kind of jar/war and deploying it and you get this error at run time.
Also, if your project dependencies set correctly if you are packaging it.

What I'm suspecting is that your code is having some kind of compilation error or dependency error.
 
Michael Piaser
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The compile process is huge - the output is a war file which is deployed to jboss. Everything else is compiling fine (at least hundreds of .java files - probably thousands). It is only this line which leads me to think I actually have syntax error that I am not seeing:

List<ElementChargesBean> mkp = packageBean.getCharges();
System.out.println("OrderManager.java: packagePricing, charges="+mkp);

for (list obj : mkp) {System.out.println("List="+obj.getAmountChargedToEndUser());};
 
Arun Kumarr
Ranch Hand
Posts: 662
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you have syntax error, then you cannot compile and build a war.
THe possibility is very high that the OrderManager class is not present in the classpath.
Can you check open the war file and check if the OrderManager class is actually present in the built war file.
Also check if the Order Manager is present in the correct package structure.
 
Michael Piaser
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've tried to look for OrderManager in the WAR file. The WAR file is 40 MB so it is not easy to look for things in it. I opened it with 7-zip. I can't find OrderManager but it could simply be that I don't know where to look. It seems to me that OrderManager needs to be there as this is a small part of a large system and except for when I have this compile error, everything compiles and runs file. My SYSTEM.OUT statements are in OrderManager and they are showing up on the JBOSS console, so it must be there.
 
Arun Kumarr
Ranch Hand
Posts: 662
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
" ....and except for when I have this compile error, everything compiles and runs file."

Are you saying your code is not compiling?

Your import statements for Order Manager should contain the directory path where your OrderManager file resides.
Can you open the zip file and open it to check the particular directory.

--------------------------------------------------------------------------------------------
My SYSTEM.OUT statements are in OrderManager and they are showing up on the JBOSS console, so it must be there.

May be it is an older class file which is present and not the current/latest one. Can you check the version of the class file which is compiled and present inside your war file is the latest.
 
Michael Piaser
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know how to do this "Can you check the version of the class file which is compiled and present inside your war file is the latest. "

doesn't it have to be the right version if I change the text of the SYSTEM.OUT statement, recompile and deploy and the new text shows up?
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16059
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Michael Piaser wrote:
System.out.println("OrderManager.java: packagePricing, packBean.EndUserTest1="+mkp.getAmountChargedToEndUser());
System.out.println("OrderManager.java: packagePricing, packBean.EndUserTest2="+packageBean.getCharges().getAmountChargedToEndUser());
System.out.println("OrderManager.java: packagePricing, packBean.EndUserTest3="+packageBean.getAmountChargedToEndUser());

Watch carefully what you are doing.

mkp is a List<ElementChargesBean>, and packageBean.getCharges() also returns a List<ElementChargesBean>. You cannot call getAmountChargedToEndUser() on that, because interface List does not have this method. You want to call this on an ElementChargesBean object, not on a list of ElementChargesBean objects.

You also cannot call getAmountChargedToEndUser() on packageBean, because class PackageBean does not have this method.

When you get an error, please make sure that you understand exactly why that error occurs. Just trying random things without really knowing what you're doing is not an effective way to write a program.

So, what do you want to do with the list of ElementChargesBean objects that are in the PackageBean object? Do you want to display the result of getAmountChargedToEndUser() for each of the ElementChargesBean objects? Then you'll have to loop over the list that packageBean.getAmountChargedToEndUser() returns, and call getAmountChargedToEndUser() on each element in the list.
 
Arun Kumarr
Ranch Hand
Posts: 662
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the new text show up - then yes it is the newer version which is used.
Is your code compiling? You never answered that.


If it is compiling, are there more than one OrderManager class in your war. It is easy to search a war file.
Are you using windows or unix OS?
 
Michael Piaser
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The good news is that using the following statement: for (ElementChargesBean obj : mkp) {System.out.println("List="+obj.getAmountChargedToEndUser());};
I am getting values. The bad news is that they are the wrong numbers but that is likely some other problem.
 
Arun Kumarr
Ranch Hand
Posts: 662
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay. Please keep only that code and remove all other System.out.println statements from your code. (especially the ones mentioned by Campbell Jesper. Don't comment it. Remove it.
Now it can give wrong values,
1. May be that is what is present in the database.
2. The place where you are looking for values and the place where the values are fetched are different.
3. May be the table/column where the data is fetched and the table/column where the data you are looking is the same, but you are looking at different rows and what is displayed is from a different row. (I hope you are using primary key to identify the row). Apologies, if that was a dumb suggestion.
 
Michael Piaser
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All good suggestions and all hard to verify. This system is very large and difficult to understand. Now I have to go to some meetings so i'll have to continue forward later.
Thanks for all your thoughts and help.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!