Estou recebendo uma InputMismatchException depois de atualizar o cargo, está apontando para a linha 26 da minha classe CrudCargoService.
@Service
public class CrudCargoService {
private final CargoRepository cargoRepository;
private Boolean system = true;
public CrudCargoService(CargoRepository cargoRepository) {
this.cargoRepository = cargoRepository;
}
public void inicial(Scanner scanner) {
while(system) {
System.out.println("Qual ação deseja fazer?");
System.out.println("0 - sair");
System.out.println("1 - salvar");
System.out.println("2 - atualizar");
int action = scanner.nextInt();
switch (action) {
case 1:
salvar(scanner);
break;
case 2:
atualizar(scanner);
break;
default:
system = false;
break;
}
}
}
private void salvar(Scanner scanner) {
System.out.println("Descrição do cargo:");
String descricao = scanner.next();
Cargo cargo = new Cargo();
cargo.setDescricao(descricao);
cargoRepository.save(cargo);
System.out.println("Salvo");
}
private void atualizar(Scanner scanner) {
System.out.println("Id:");
int id = scanner.nextInt();
System.out.println("Descrição do cargo:");
String descricao = scanner.next();
Cargo cargo = new Cargo();
cargo.setId(id);
cargo.setDescricao(descricao);
cargoRepository.save(cargo);
System.out.println("Atualizado");
}
}
@SpringBootApplication
public class SpringDataApplication implements CommandLineRunner {
private final CrudCargoService cargoService;
private Boolean system = true;
public SpringDataApplication(CrudCargoService cargoService) {
this.cargoService = cargoService;
}
public static void main(String[] args) {
SpringApplication.run(SpringDataApplication.class, args);
}
@Override
public void run(String... args) throws Exception {
Scanner scanner = new Scanner(System.in);
while(system) {
System.out.println("Qual ação deseja fazer?");
System.out.println("0 - sair");
System.out.println("1 - cargo");
int action = scanner.nextInt();
if(action == 1) {
cargoService.inicial(scanner);
}else {
system = false;
}
}
}
}
java.lang.IllegalStateException: Failed to execute CommandLineRunner
at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:770) ~[spring-boot-2.6.2.jar:2.6.2]
at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:751) ~[spring-boot-2.6.2.jar:2.6.2]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:309) ~[spring-boot-2.6.2.jar:2.6.2]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1301) ~[spring-boot-2.6.2.jar:2.6.2]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1290) ~[spring-boot-2.6.2.jar:2.6.2]
at br.com.alura.spring.data.SpringDataApplication.main(SpringDataApplication.java:24) ~[classes/:na]
Caused by: java.util.InputMismatchException: null
at java.base/java.util.Scanner.throwFor(Scanner.java:939) ~[na:na]
at java.base/java.util.Scanner.next(Scanner.java:1594) ~[na:na]
at java.base/java.util.Scanner.nextInt(Scanner.java:2258) ~[na:na]
at java.base/java.util.Scanner.nextInt(Scanner.java:2212) ~[na:na]
at br.com.alura.spring.data.service.CrudCargoService.inicial(CrudCargoService.java:26) ~[classes/:na]
at br.com.alura.spring.data.SpringDataApplication.run(SpringDataApplication.java:38) ~[classes/:na]
at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:767) ~[spring-boot-2.6.2.jar:2.6.2]
... 5 common frames omitted
O que pode estar ocorrendo?