Programming

int num = Integer.getInteger (“123”)에서 NullPointerException이 발생하는 이유는 무엇입니까?

procodes 2020. 8. 18. 20:04
반응형

int num = Integer.getInteger (“123”)에서 NullPointerException이 발생하는 이유는 무엇입니까?


다음 코드는 다음을 던집니다 NullPointerException.

int num = Integer.getInteger("123");

내 컴파일러 getInteger가 정적이기 때문에 null을 호출 합니까? 말이 안 돼!

무슨 일이야?


큰 그림

여기에는 두 가지 문제가 있습니다.

  • Integer getInteger(String) 당신이 생각하는대로하지 않는다
    • null이 경우 반환
  • Integerto 에서 할당 int하면 자동 언 박싱 발생합니다.
    • 때문에 IntegerIS null, NullPointerException발생합니다

구문 분석하기 (String) "123"위해 (int) 123, 당신은 예를 사용할 수 있습니다 int Integer.parseInt(String).

참고 문헌

Integer API 참조


의 위에 Integer.getInteger

이 메서드가 수행하는 작업에 대한 설명서의 내용은 다음과 같습니다.

public static Integer getInteger(String nm): 지정된 이름을 가진 시스템 속성의 정수 값을 결정합니다. 지정된 이름의 속성이 없거나 지정된 이름이 비어 있거나 null또는 속성에 올바른 숫자 형식이없는 경우 null반환됩니다.

즉,이 방법은 구문 분석과는 아무 상관이 없습니다 Stringint/Integer값을 것이 아니라, 그것을 함께 할 수있다 System.getProperty방법.

물론 이것은 상당히 놀라운 일입니다. 라이브러리에 이와 같은 놀라움이 있다는 것은 안타깝지만 귀중한 교훈을 제공합니다. 항상 문서를 찾아 메서드가 수행하는 작업을 확인하십시오.

공교롭게도이 문제의 변형은 Return of the Puzzlers : Schlock and Awe (TS-5186) , Josh Bloch 및 Neal Gafter의 2009 JavaOne 기술 세션 프레젠테이션에 등장했습니다. 결론 슬라이드는 다음과 같습니다.

도덕

  • 도서관에는 이상하고 끔찍한 방법이 숨어 있습니다.
    • 일부는 무해한 이름을 가지고 있습니다.
  • 코드가 잘못 작동하는 경우
    • 올바른 메서드를 호출하고 있는지 확인
    • 라이브러리 문서 읽기
  • API 디자이너 용
    • 최소 경악의 원칙을 위반하지 마십시오
    • 추상화 계층을 위반하지 마십시오
    • 매우 다른 행동에 유사한 이름을 사용하지 마십시오.

완전성을 위해 Integer.getInteger다음 과 유사한 방법도 있습니다 .

관련 질문


자동 언 박싱

물론 다른 문제는 어떻게 NullPointerException던져 지는가입니다. 이 문제에 집중하기 위해 다음과 같이 코드 조각을 단순화 할 수 있습니다.

Integer someInteger = null;
int num = someInteger; // throws NullPointerException!!!

다음은 Effective Java 2nd Edition, 항목 49의 인용문입니다. 박스형 기본 형식보다 기본 형식을 선호합니다.

요약하면, 선택할 수있을 때마다 boxed primitive보다 primitive를 사용하십시오. 기본 유형은 더 간단하고 빠릅니다. 박스형 프리미티브를 사용해야한다면 조심하세요! Autoboxing은 boxed primitives를 사용하는 위험은 아니지만 자세한 정도를 줄입니다. 프로그램이 두 개의 박스형 프리미티브를 ==연산자 비교할 때 ID 비교를 수행합니다. 이는 거의 확실히 원하는 것이 아닙니다. 프로그램이 boxed 및 unboxed 프리미티브와 관련된 혼합 유형 계산을 수행하면 unboxing을 수행하고 프로그램에서 unboxing을 수행하면 NullPointerException. 마지막으로 프로그램이 기본 값을 상자에 넣으면 비용이 많이 들고 불필요한 개체 생성이 발생할 수 있습니다.

There are places where you have no choice but to use boxed primitives, e.g. generics, but otherwise you should seriously consider if a decision to use boxed primitives is justified.

Related questions


From http://konigsberg.blogspot.com/2008/04/integergetinteger-are-you-kidding-me.html:

getInteger 'Determines the integer value of the system property with the specified name.'

You want this:

Integer.parseInt("123")

Please check documentation of the method getInteger(). In this method, the String parameter is a system property that determines the integer value of the system property with the specified name. "123" is not the name of any system property, as discussed here. If you want to convert this String to int, then use the method as int num = Integer.parseInt("123").

참고URL : https://stackoverflow.com/questions/3123349/why-does-int-num-integer-getinteger123-throw-nullpointerexception

반응형