Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

W10.2d Enforcing referential integrity suggestion #2

Open
papataco14 opened this issue Nov 8, 2023 · 1 comment
Open

W10.2d Enforcing referential integrity suggestion #2

papataco14 opened this issue Nov 8, 2023 · 1 comment

Comments

@papataco14
Copy link

image

In section 10.2d of the notes, we want to model the above relationship, without violating referential integrity, and the following code is suggested:

public class Woman {
    private Man boyfriend;

    public void setBoyfriend(Man m) {
        if (boyfriend == m) {
            return;
        }
        if (boyfriend != null) {
            boyfriend.breakUp();
        }
        boyfriend = m;
        m.setGirlfriend(this);   # Here
    }

    public void breakUp() {
        boyfriend = null;
    }
    // ...
}

However, if null is passed into the setBoyfriend method, the last line of the method will throw a NullPointerException.
I believe changing it to the following will fix it:

if (m != null) {
    m.setGirlfriend(this);
}

Similarly for the Man class

@papataco14 papataco14 changed the title W10.2d Enforcing referential integrity bug W10.2d Enforcing referential integrity suggestion Nov 8, 2023
@damithc
Copy link
Contributor

damithc commented Nov 8, 2023

Good catch @papataco14
Yes, I believe you are right.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants