• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Prevent Infinite Looping between Objects

 
Ranch Hand
Posts: 2259
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is my vendor object. As shown one of the properties is LastShippingAddress.






This creates an infinite loop unless I change  public Vendor lastShippingAddress = new VendorV8(); to public VendorV8 lastShippingAddress = null;
Is there another way to do this without setting to null?
 
Saloon Keeper
Posts: 10929
87
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have confusing variable names for lastShippingAddress, one place it's of type LastShippingAddress and the other it's Vendor. Fix that first.
 
Carey Brown
Saloon Keeper
Posts: 10929
87
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't edit your first post anymore now that it has been responded to.
 
Marshal
Posts: 8960
646
Mac OS X Spring VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Steve Dyke wrote:This creates an infinite loop



Do you have an idea why would that happen?
 
Steve Dyke
Ranch Hand
Posts: 2259
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Liutauras Vilda wrote:

Steve Dyke wrote:This creates an infinite loop



Do you have an idea why would that happen?



Yes I understand why it is looping.
I am just curious is there a way to do this without having to set private Vendor lastShippingAddress = new Vendor(); to null
I am trying to move away from setting any object properties to null.
 
Steve Dyke
Ranch Hand
Posts: 2259
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:You have confusing variable names for lastShippingAddress, one place it's of type LastShippingAddress and the other it's Vendor. Fix that first.



Not sure what to do to fix this.
The LastShippingAddress Object has a property of Vendor()
Vendor has a property of LastShippingAddress
 
Carey Brown
Saloon Keeper
Posts: 10929
87
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is what you have stripped down to the bare bones. On line 13 you don't have a 'lastShippingAddress' you have a 'vendor'.

It is not clear at all what you are trying to do, you obviously have a circular dependency. So, when you create a new Vendor, what should its lastShiippingAddress be if not null?
 
Sheriff
Posts: 4641
582
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is the relationship between Vendor and LastShippingAddress?

 
Carey Brown
Saloon Keeper
Posts: 10929
87
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You could use 'Optional' instead of null, e.g. Optional<Vendor> lastShippingAddress = Optional.empty();. Now instead of checking for null you have to check for isEmpty() or isPresent().
 
Steve Dyke
Ranch Hand
Posts: 2259
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:

I am sorry I can't describe this more precisely.

 
Carey Brown
Saloon Keeper
Posts: 10929
87
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Any reason why 'lastShippingAddress' is not the same type as 'vendorAdd1', i.e. 'String'?
Like...
 
Bartender
Posts: 303
12
IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ron McLeod wrote:What is the relationship between Vendor and LastShippingAddress?



This is where I would start. This is more of a design and data model topic than a "How can I code monkey this thing to make it work?" thing. I suspect there's a broader data modeling issue, and we need a better high level view of the overall data model.

A Vendor is pretty obvious, but what is a "LastShipppingAddress" referring to? (And why isn't it just an "Address" type with a field named lastShippingAddress? After all an address is an address, right?) Is it the last shipping address for a customer/buyer/user? It doesn't seem like it belongs inside Vendor at all.
 
Steve Dyke
Ranch Hand
Posts: 2259
2
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:Any reason why 'lastShippingAddress' is not the same type as 'vendorAdd1', i.e. 'String'?


Sorry, but on my initial code I did not list all of the Vendor properties.

The Vendor records are pulled into my application from a remote data source as a list of Vendor objects.
There is a property for Vendor Code, Vendor Name, Street Address 1, Street Address 2, City, State, Zip, etc. This all represents the Vendor default data.
Therefor the Vendor address is made up of several properties.
Some times we will ship to an alternate address, maybe several times. Instead of our Purchasing agent retyping this alternate address every time I save the LastShipedAddress into a remote data file with a field that links back to the original Vendor record.
So both a remote Vendor record has the same structure as a LastShippedAddress record.
When a Vendor record is selected the Purchasing agent can leave as default address of chose the alternate LastShippedAddress.
 
Carey Brown
Saloon Keeper
Posts: 10929
87
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So, in your original Vendor record you could link to the alternate Vendor record or vice versa. Technically I don't see this as a 'lastAddress'.

What's your objection to using 'null' to indicate that there's no such link if one doesn't exist?
 
Carey Brown
Saloon Keeper
Posts: 10929
87
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Steve Dyke wrote:The Vendor records are pulled into my application from a remote data source as a list of Vendor objects....


Thank you. That's a good description.
 
Enjoy the full beauty of the english language. Embedded in this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic