인프런 커뮤니티 질문&답변

수인님의 프로필 이미지
수인

작성한 질문수

51강) NullPointerException 왜 나는건지 모르겠습니다..

작성

·

148

0

강사님이랑 똑같이 따라 쳤는데 위 결과가 나옵니다.. 왜 그런 걸까요?

아래가 제가 쓴 코드입니다.

 

 

package hello.core.autowired; import hello.core.AutoAppConfig; import hello.core.discount.DiscountPolicy; import hello.core.member.Grade; import hello.core.member.Member; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import java.util.List; import java.util.Map; import static org.assertj.core.api.Assertions.assertThat; public class AllBeanTest { @Test void findAllBean() { ApplicationContext ac = new AnnotationConfigApplicationContext(AutoAppConfig.class, DiscountService.class); DiscountService discountService = ac.getBean(DiscountService.class); Member member = new Member(1L, "userA", Grade.VIP); int discountPrice = discountService.discount(member, 10000,"fixDiscountPolicy"); assertThat(discountService).isInstanceOf(DiscountService.class); assertThat(discountPrice).isEqualTo(1000); } static class DiscountService { private final Map<String, DiscountPolicy> policyMap; private final List<DiscountPolicy> policies; public DiscountService(Map<String, DiscountPolicy> policyMap, List<DiscountPolicy> policies) { this.policyMap = policyMap; this.policies = policies; System.out.println("policyMap = " + policyMap); System.out.println("policies = " + policies); } public int discount(Member member, int price, String discountCode) { DiscountPolicy discountPolicy = policyMap.get(discountCode); System.out.println("discountCode = " + discountCode); System.out.println("discountPolicy = " + discountPolicy); return discountPolicy.discount(member, price); } } }

답변 1

0

package hello.core.autowired;

import hello.core.AutoAppConfig;

import hello.core.discount.DiscountPolicy;

import hello.core.member.Grade;

import hello.core.member.Member;

import org.junit.jupiter.api.Test;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.context.ApplicationContext;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import java.util.List;

import java.util.Map;

import static org.assertj.core.api.Assertions.assertThat;

public class AllBeanTest {

@Test

void findAllBean() {

ApplicationContext ac = new AnnotationConfigApplicationContext(AutoAppConfig.class, DiscountService.class);

DiscountService discountService = ac.getBean(DiscountService.class);

Member member = new Member(1L, "userA", Grade.VIP);

int discountPrice = discountService.discount(member, 10000, "fixDiscountPolicy");

assertThat(discountService).isInstanceOf(DiscountService.class);

assertThat(discountPrice).isEqualTo(1000);

}

static class DiscountService {

private final Map<String, DiscountPolicy> policyMap;

private final List<DiscountPolicy> policies;

public DiscountService(Map<String, DiscountPolicy> policyMap, List<DiscountPolicy> policies) {

this.policyMap = policyMap;

this.policies = policies;

System.out.println("policyMap = " + policyMap);

System.out.println("policies = " + policies);

}

public int discount(Member member, int price, String discountCode) {

DiscountPolicy discountPolicy = policyMap.get(discountCode);

System.out.println("discountCode = " + discountCode);

System.out.println("discountPolicy = " + discountPolicy);

return discountPolicy.discount(member, price);

}

}

}

 


DiscountPolicy discountPolicy = policyMap.get(discountCode);
해당 구문에서 null값을 읽어와 생긴 에러입니다. 적어주신 코드만으로는확인이 어렵네요.


public DiscountService(Map<String, DiscountPolicy> policyMap, List<DiscountPolicy> policies)
해당 클래스 생성자가 실행 되었는지 확인해 주시고 강의에서 놓친 부분이 없는지 확인해 주세요.

강의 관련 질문을 올리실때는 강의페이지 -> 커뮤니티 -> 질문 에 글쓰기 하셔야 어느 강의 질문인지 알 수 있고, 강사님이 해당 질문을 확인할 수 있습니다.

코드를 함께 첨부해주실 때는 코드정렬 후 첨부해주시면 더욱 편할 것 같습니다.
에러 메시지 또한 전체를 첨부해주시면 도움 드리기 더욱 편할 것 같습니다.

수인님의 프로필 이미지
수인

작성한 질문수

질문하기