프로젝트/키오스크

[키오스크 프로젝트] 트러블 슈팅 - 기본 생성자 및 리스트 초기화

yoney 2025. 1. 15. 14:39

도전과제 1 요구사항 맞춰서 코드 작성 후 콘솔로 확인해보려는데 실행하자마자 뜨는 오류

Execution failed for task ':Main.main()'.
> Process 'command 'C:\Users\user\.jdks\corretto-17.0.8.1\bin\java.exe'' finished with non-zero exit value 1

* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.
> Get more help at https://help.gradle.org.
Deprecated Gradle features were used in this build, making it incompatible with Gradle 9.0.
You can use '--warning-mode all' to show the individual deprecation warnings and determine if they come from your own scripts or plugins.
For more on this, please refer to https://docs.gradle.org/8.5/userguide/command_line_interface.html#sec:command_line_warnings in the Gradle documentation.
BUILD FAILED in 610ms
2 actionable tasks: 1 executed, 1 up-to-date

 

일단 메인 메뉴에서부터 문제가 있나 싶어서 디버깅함

메인에서 메뉴 리스트 객체 생성은 잘 이루어졌다.

Main 클래스에서는 객체 생성 후 Kiosk 클래스를 실행하는 메소드 뿐이어서 객체생성이 문제가 아니라면 문제는 Kiosk.java에 있을 것이라고 판단

 

Kiosk에서 디버깅해본 결과

 

java.lang.NullPointerException: Cannot invoke "java.util.List.isEmpty()" because the return value of "com.example.challenge1.Cart.getCartList()" is null

이런 코드가 나왔다. 

 

//장바구니에 물건이 있는 경우
            if (!cart.getCartList().isEmpty()) {
                notEmpty = true;
                System.out.println("[ ORDER MENU ]");
                System.out.println(menuList.size() + 1 + ". Orders      | 장바구니를 확인 후 주문합니다.");
                System.out.println(menuList.size() + 2 + ". Cancel      | 진행중인 주문을 취소합니다.");
            }

이 코드를 작성했었는데 이때 list가 초기화가 안됐다고 에러가 뜨는 것이었다.

 

private List<CartItem> cartList;

Cart클래스에서 장바구니 리스트를 그냥 선언만 해둬서 null이 나오는 것이었다.

 

public Cart() {
    cartList = new ArrayList<CartItem>();
}

기본생성자는 굳이 안 만들어도 자동으로 생성돼서 작성을 안했었는데 리스트 초기화를 여기서 했어야 하는 것을 모르고 있었다.

이렇게 작성하니 바로 잘 실행됨!