Stack Exchange
Java NameLengthNotValidException always gets thrown in domainclass
IMPORTANT: Probably a rookie question, but I'm clueless on how to solve it.
I have a UI class which I use to input a name and gender (geslacht) using a scanner (see code below)
 private static void geefNaamEnGeslacht(int aantal) {
    String naam, geslacht;
    for (int i = 0; i < aantal; i++) {
        System.out.print("Wat is de naam van speler " + (i + 1) + " (min 6 tekens, max 12 tekens)? ");
        naam = sc.next();
        naam += sc.nextLine();
        System.out.print("Wat is het geslacht van speler " + (i + 1) + "(man=m vrouw=v)? ");
        geslacht = sc.next();
        System.out.println(naam.length());
        try {
            dc.maakSpelerAan(naam, geslacht.charAt(0));
        } catch (NameLengthNotValidException e) {
            System.out.println(e.getMessage());
            i--;
        }
    }
}

In my domain class I check the length of that name in the setter, but it always throws the exception, no matter what I tried. When debugging, the length of any name I picked checks out to be between 6 and 12 characters strangely enough. (see code below)
public final void setNaam(String naam) {
    int lengte = naam.length();
    if (naam.length() < 6 || naam.length() > 12) {
        throw new NameLengthNotValidException("Je naam moet tussen 6 en 12 tekens liggen!");
    }
    this.naam = naam;

}

Any ideas or tips are apreciated.
EDIT1 code of dc.maakSpelerAan
public void maakSpelerAan(String naam, char geslacht){
    s.maakSpeler(naam, geslacht);   
}

EDIT2 code of s.maakspeler
public void maakSpeler(String naam, char geslacht){
    if (spelCompleet()) {
        throw new ListFilledException("De lijst met spelers is vol !");
    }

    if (!controleerUniekeNaam(naam)) {
        throw new NotUniqueNameException();
    }

    Kaart[] schatkaarten = new Kaart[STARTAANTAL];
    Kaart[] kerkerkaarten = new Kaart[STARTAANTAL];

    for (int i = 0; i > STARTAANTAL; i++) {
        schatkaarten[i] = stapels.get(SCHATKAART).geefBovensteKaart();
        kerkerkaarten[i] = stapels.get(KERKERKAART).geefBovensteKaart();
    }
    try{
    Speler s = new Speler(naam, geslacht);
    s.getKaarten(kerkerkaarten, schatkaarten);
    spelers.add(s);
    }catch (IllegalArgumentException | NameLengthNotValidException e){
       throw e;
   }

    if (spelCompleet()) {
        //UC2
        spelers.get(EERSTESPELERINDEX).setBeurt(true);
    } 


}

EDIT3 REPOST WITH CORRECT IMAGE Image of variable "naam" right before it goes in the IF loop while debugging. MINLENGTHNAAM and MAXLENGTHNAAM are set to 6 and 12 respectively in an interface class.

EDIT4 Code in my interface class for MINLENGTH AND MAXLENGTH
public interface SpelInfo {
final int STARTAANTAL = 2;
final int MAXAANTALPUNTEN = 10;
final int INDEXNAAMDETAILS = 0;
final int INDEXGESLACHTDETAILS = 1;
final int INDEXPUNTENDETAILS = 2;
final int MINAANTALSPELERS = 3;
final int MAXAANTALSPELERS= 6;
final int EERSTESPELERINDEX = 0;
final int MAXLENGTHNAAM = 6;
final int MINLENGTHNAAM = 12;
final int AANTALSTAPELS = 2;
final char MAN = 'm';
final char VROUW = 'v';
final int SCHATKAART = 0;
final int KERKERKAART = 1;

}

Please provide the code of the method dc.maakSpelerAan. It might not pass the correct name to the setter.

Added to the post

Also s.maakSpeler or any method in between until the setter is called.

Were you actually running your code under debugger and observing the value of lengte?

Added code.
yes I did and I'm pretty sure it passed the right name. The value doesn't show up in the debugger as it's an unused variable I suppose.

You might want to just run your IDE in debug mode and step through the method calls until you find exactly what is throwing the exception and why. It'll probably be faster than having random people on the internet doing an "eyeballed" debugging.

Also if you print the stack trace (e.printStackTrace()) it'll show you what line it was calling from. Right now you're only printing the message which is not that useful.

Understand that this isn't the easiest problem to debug from a far, but I tried debugging my whole project to see where it goes wrong. It jumps through these 3 methods and I can't wrap my head around it.

It seems that you rely on the length of your input and not the actual string passed into your method. If so, this is not debugging.

Added screenshot of the variable right before it enters the IF loop while debugging. It is infact the variable length that should be checked there

you mixed up/switched sides of: MAXLENGTHNAAM and MINLENGTHNAAM;)

it should be: if (naam.length() > MAXLENGTHNAAM || naam.length() < MINLENGHTNAAM) { throw ... }

My mistake, edited it back to the interface final variables when trying some stuff but that still doesn't fix it somehow. Added another screenshot for clarity.

..then it can only be a misunderstanding/-interpretation of the constants/values of MIN/MAXLENGHTNAAM. What are they actually (in debugger)?

..so in the latest screenshot, it must be, that : Or MINLENGTHNAAM >= 12 or MAXLENGTHNAAM <= 12 ...cause naam.length() is obviously = 11.

Even if I hardcode replace them with 6 and 12 it still throws the exception. It’s frustrating. When I debug MIN or MAX it says variable is unknown because it’s not relevant or something

final int MAXLENGTHNAAM = 6; final int MINLENGTHNAAM = 12; !(?)

Irrelevant since if I hardcode < 6 || > 12 it still doesn't work.

In "Edit4" you need to change the green line into:
if(naam.length() > MAXLENGTHNAAM || naam.length() < MINLENGTHNAAM) {

Just a little logical issue :)

it should be length()

MIN and MAX switched spots, still same result.. (Last EDIT on OP)
