'Return to the original value, getter setter, OOP, Java [duplicate]

I hope you doing well =)

I'm new to Java and currently, I started learning OOP not a long time ago.

I have a task with which I have a problem.

Add a setter for the name field so that if someone typed an empty value in the name field, or a value greater than 100 characters, then this call to the setter would be ignored, and the old value would be typed in the name field.

The first time I did this with the following code everything was fine when I ran it in Eclipse.

But when I ran it in the test app, it failed because for example the setName method on the input "dyqu" is setting the value of the "Walker" name field, and it should have been set to "dyqu".

public class SpaceShip {
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        if(this.name == null)
        this.name = name;
    }

    //Test output
    public static void main(String[] args) {
        SpaceShip ship = new SpaceShip();
        ship.setName("Walker");
        System.out.println(ship.getName()); //Should be Walker

        ship.setName("");
        System.out.println(ship.getName()); //Should be Walker, empty value ignored

        ship.setName("Voyager ".repeat(100));
        System.out.println(ship.getName()); //Should be Walker, too long value ignored
    }
}

Then I tried to change the code to this one:

   public void setName(String name) {
        if(this.name.length() < 0 && this.name.length() > 100)
        this.name = name;
    }

I got this error:

Exception in thread "main" java.lang.NullPointerException
    at SpaceShip.setName(SpaceShip.java:9)
    at SpaceShip.main(SpaceShip.java:16)

Unfortunately, I can't solve the issue on lines 9 and 16.

The input data for line 16 may change. I would be glad for a hint or advice. Thank you!

I solved this issue with the next code, thanks, everyone!

public void setName(String name) {
    if (!name.isBlank() && name.length() < 100) {
    this.name = name;
    }
}


Solution 1:[1]

Your implementation:

 public void setName(String name) {
        if(this.name.length() < 0 && this.name.length() > 100)
        this.name = name;
    }

references this.name which is the currently stored value within the SpaceShip-object. However when an object is created without assigning default values either directly or by setting them in a constructor they'll be assigned a default value.

For object types like String this default value is null. You can't invoke methods (or anything really) on null.

What you should do instead is validating the input of the method by omitting the this in front of name:

 public void setName(String name) {
        if(name.length() < 0 && name.length() > 100)
        this.name = name;
    }

Then it's validating the method-argument-variable 'name'.


To iterate on the logical issue of the check. Your 'if' is only allowing a value to be set of it's either smaller than 0 or greater than 100 in length. A String which is not null can never be smaller than 0 and it's the other way around, the value should only be set when the value is inbetween the desired lengths: if( name != null && !name.isEmpty() && !name.length() > 100)

Solution 2:[2]

if(this.name.length() < 0 && this.name.length() > 100)

in your setName method is referring to

private String name;

in your class, the instance variable.
That's null right now.
That's why you're getting a null exception.

Also

this.name.length() < 0

doesn't make sense. A string will never have a length less than 0.

You want it like this

public void setName(String name) {
        if(name.length() == 0 || name.length() > 100)
        this.name = name;
    }
//name.length() == 0 checks for an empty string
// || means OR 

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 javastudent