Skip to content
Advertisement

Java Skipping switch statement using getter as expression

Hey Sorry for the what for you guys must seem like a stupid question but I’ve been stuck on this for a while now, im using a getter from a different class as the switch expression in another class, my program keeps skipping the entire switch alltogether.

Thank you for your time!

First class

JavaScript

Second class

JavaScript

main

JavaScript

The first menu print is just a pintblock without anything else, when running the program I do get the int I enter back.

Advertisement

Answer

Your program isn’t skipping over the switch statement. Look at the Menus.loginChoiceCase() method. It’s creating a new instance of InputRequests. Which means inp.getLoginRole() is null, and your switch statement doesn’t have a default case to catch nulls.

Add a default case to the switch statement

JavaScript

Ideally what you would want to do is have your loginChoiceCase() method take in an int, and then use that in you case statement. With loginChoiceCase() able to take in an int you can then use your getter. Like below.

InputRequests

JavaScript

Menus

JavaScript

Main

JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement