๐Ÿ“• [JAVA] ์‚ผํ•ญ ์—ฐ์‚ฐ์ž ์‚ฌ์šฉ ์ค‘ NPE ๋ฐœ์ƒ ์ด์œ 

์–‘์•„ยท2021๋…„ 11์›” 3์ผ
2

JAVA

๋ชฉ๋ก ๋ณด๊ธฐ
2/2
post-thumbnail

์‚ผํ•ญ ์—ฐ์‚ฐ์ž๋ฅผ ์ด์šฉํ•˜๋‹ค๊ฐ€ ๊ณ„์† NullPointException ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜์—ฌ ์•Œ์•„๋ณด๋‹ค๊ฐ€ ์•Œ๊ฒŒ ๋œ ๋ถ€๋ถ„์ด๋ผ ๋น„์Šทํ•œ ๊ณ ๋ฏผ์„ ํ•˜์‹œ๋Š” ๋ถ„๋“ค์—๊ฒŒ ์† ์‹œ์›ํ•œ ๋‹ต๋ณ€์„ ๋“œ๋ฆฌ๊ธฐ ์œ„ํ•œ ๊ธ€๐Ÿ˜‰


๐Ÿ™‰ ๋ฌธ์ œ ๋ฐœ์ƒ ๋ถ€๋ถ„

์ด๋ ‡๊ฒŒ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ๋ฐ›์€ ๊ฐ’์ด null์ธ ๊ฒฝ์šฐ์—๋Š” ๊ทธ๋Œ€๋กœ setํ•˜๊ณ , not null์ธ ๊ฒฝ์šฐ์—๋Š” ๊ณ„์‚ฐ์ด ํ•„์š”ํ•œ ๋ถ€๋ถ„์ด ์žˆ๋Š”๋ฐ if๋ฌธ์„ ์“ฐ๊ฒŒ ๋˜๋ฉด ์†Œ์Šค๊ฐ€ ์“ธ๋ฐ์—†์ด ๊ธธ์–ด์ ธ์„œ ์‚ผํ•ญ ์—ฐ์‚ฐ์ž๋ฅผ ์ด์šฉํ•จ

์ผ๋ถ€๋Ÿฌ ๋ฐ์ดํ„ฐ์— null์„ ๋„ฃ์–ด ํ…Œ์ŠคํŠธ ํ•ด ๋ณธ ๊ฒฐ๊ณผ ์œ„์™€ ๊ฐ™์ด NullPointException ์—๋Ÿฌ๊ฐ€ ๋œธ๐Ÿ˜จ

์ด์ฏค์—์„œ ๋‚ด๊ฐ€ ๋ฐ์ดํ„ฐ์— null ๊ฐ’์„ ๋„ฃ์€๊ฒŒ ์‚ฌ์‹ค null๊ฐ’์ด ์•„๋‹Œ๊ฑธ๊นŒ..? ์‹ถ๊ณ  ๋„ํ†ต ์ด์œ ๋ฅผ ์•Œ์ง€ ๋ชปํ•ด์„œ ์ฐ์–ด ๋ณธ ๋กœ๊ทธ

ํ•˜์ง€๋งŒ ๋กœ๊ทธ๋Š” ๋‚ด๊ฐ€ ๋„ฃ์€ ๋ฐ์ดํ„ฐ๊ฐ€ null์ด๋ผ๊ณ  ์™ธ์น˜๊ณ  ์žˆ์Œ๐Ÿ˜ฑ

๐Ÿ’ก ๋ฌธ์ œ ๋ฐœ์ƒ ์ด์œ 

if๋ฌธ์œผ๋กœ ๋ฐ”๊ฟ”์„œ ํ…Œ์ŠคํŠธ ํ•ด๋ณด๋ฉด ๋ฌธ์ œ๊ฐ€ ์—†๊ธฐ์— ์‚ผํ•ญ ์—ฐ์‚ฐ์ž ๋ฌธ์ œ๋ผ ์ƒ๊ฐ ๐Ÿค”
๊ทธ๋ž˜์„œ ์ฐพ์•„๋ณด๋‹ˆ ์ด ๋ฌธ์ œ๋Š” ์ž๋ฐ” ์„ธ๊ณ„์—์„œ ์œ ๋ช…ํ•œ ์‚ผํ•ญ ์—ฐ์‚ฐ์ž ์–ธ๋ฐ•์‹ฑ ๋ฌธ์ œ์˜€๋‹ค.

๋‚˜๋Š” ์ž๋ฐ” 8๋ฒ„์ „์„ ์‚ฌ์šฉ ์ค‘์ธ๋ฐ ์ž๋ฐ” 8 ์ŠคํŽ™ ๋ฌธ์„œ๋ฅผ ์ฐพ์•„๋ณด๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋‚ด์šฉ์„ ํ™•์ธํ•ด๋ณผ ์ˆ˜ ์žˆ๋‹ค.

[Unboxing Conversion]

Unboxing conversion converts expressions of reference type to corresponding expressions of primitive type. Specifically, the following eight conversions are called the unboxing conversions:
From type Boolean to type boolean
From type Byte to type byte
From type Short to type short
From type Character to type char
From type Integer to type int
From type Long to type long
From type Float to type float
From type Double to type double

At run time, unboxing conversion proceeds as follows:
If r is a reference of type Boolean, then unboxing conversion converts r into r.booleanValue()
If r is a reference of type Byte, then unboxing conversion converts r into r.byteValue()
If r is a reference of type Character, then unboxing conversion converts r into r.charValue()
If r is a reference of type Short, then unboxing conversion converts r into r.shortValue()
If r is a reference of type Integer, then unboxing conversion converts r into r.intValue()
If r is a reference of type Long, then unboxing conversion converts r into r.longValue()
If r is a reference of type Float, unboxing conversion converts r into r.floatValue()
If r is a reference of type Double, then unboxing conversion converts r into r.doubleValue()
If r is null, unboxing conversion throws a NullPointerException

์—ฌ๊ธฐ์—์„œ ๊ฐ€์žฅ ๋งˆ์ง€๋ง‰ ์ค„์— ์ ํ˜€ ์žˆ๋Š” If r is null, unboxing conversion throws a NullPointerException!!
์ฆ‰, null๊ฐ’์„ ์–ธ๋ฐ•์‹ฑ ํ•˜๋ ค๊ณ  ํ–ˆ๋”๋‹ˆ NPE ํ˜„์ƒ์ด ์ƒ๊ธด ๊ฒƒ์ด์—ˆ๋‹ค. ๐Ÿ™€

๐Ÿ˜Ž ๋ฌธ์ œ ํ•ด๊ฒฐ ๋ฐฉ๋ฒ•

๊ทธ๋ ‡๋‹ค๋ฉด ์—ฌ๊ธฐ์„œ ๊ถ๊ธˆํ•œ ์ ์ด ์ƒ๊ธด๋‹ค.
null๊ฐ’์ด ์ƒ๊ธฐ๋Š” ๊ฑด 3ํ•ญ ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ๋ชปํ• ๊นŒ?๐Ÿค”

์ •๋‹ต์€ null๊ฐ’์ด ์ƒ๊ฒจ๋„ 3ํ•ญ ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉ ํ•  ์ˆ˜ ์žˆ๋‹ค!

์ด์œ ๋Š”? ์‚ผํ•ญ ์—ฐ์‚ฐ์ž == ์กฐ๊ฑด ์—ฐ์‚ฐ์ž์ด๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

[์กฐ๊ฑด ์—ฐ์‚ฐ์ž ๊ทœ์น™]

  • ๋‘ ๋ฒˆ์งธ ๋ฐ ์„ธ ๋ฒˆ์งธ ํ”ผ์—ฐ์‚ฐ์ž๊ฐ€ ๋™์ผํ•œ ์œ ํ˜•(null ํ˜•์‹์ผ ์ˆ˜ ์žˆ์Œ)์„ ๊ฐ€์งˆ ๊ฒฝ์šฐ, ์ด๊ฒƒ์ด ์กฐ๊ฑด์‹์˜ ์œ ํ˜•์ด๋‹ค.
  • ๋‘ ๋ฒˆ์งธ ํ”ผ์—ฐ์‚ฐ์ž ๋ฐ ์„ธ ๋ฒˆ์งธ ํ”ผ์—ฐ์‚ฐ์ž ์ค‘ ํ•˜๋‚˜๊ฐ€ T ์›์‹œ ํ˜•์‹์ด๊ณ  ๋‹ค๋ฅธ ํ”ผ์—ฐ์‚ฐ์ž์˜ ์œ ํ˜•์ด T์— ๋ฐ•์‹ฑ ๋ณ€ํ™˜(55.1.7)์„ ์ ์šฉํ•œ ๊ฒฐ๊ณผ์ธ ๊ฒฝ์šฐ, ์กฐ๊ฑด์‹์˜ ์œ ํ˜•์€ T์ด๋‹ค.

์ฆ‰, ๋‘ ๋ฒˆ์งธ ํ”ผ์—ฐ์‚ฐ์ž์™€ ์„ธ ๋ฒˆ์งธ ํ”ผ์—ฐ์‚ฐ์ž์˜ ํ˜•์‹์„ ๋งž์ถฐ์ฃผ๋ฉด ๋œ๋‹ค.

์ด๋กœ์จ, ์‚ผํ•ญ์„ ์‚ฌ์šฉํ•˜๋ฉด์„œ ๋” ์ด์ƒ ๊ด€๋ จ NPE๋Š” ๋งŒ๋‚˜์ง€ ์•Š๊ฒŒ ๋˜์—ˆ๋‹ค.๐Ÿฅณ

profile
Back-end developer

0๊ฐœ์˜ ๋Œ“๊ธ€