ããã°ã®ä¸æ¸ããçºãã¦ããé·æãã¿ã¤ãã£ãã®ã§ä¾é¤ãã¦ããã¾ãã TODOãDOããããã¯ãããã¾ãããå¿ã®ç®ã§è¦ã¦ãã ããã
Javaã«ã¯ null
ããã£ã¦ããã®åãããªãä¸çããããã§ããã©ãããªããªã£ããã¯ãã¦ãããªãã®ã§ãä»æ¹ãããã¾ãããããæãã«ä»ãåã£ã¦ããã¾ããããã¼ã£ã¦è©±ãæ¸ãã¾ããé¢é£ã¨ã³ããªã¨ãã¹ã©ã¤ãã¯ãã®è¾ºã
- ãµã¤ãã®Javaã³ã¼ãã£ã³ã°
- 2017å¹´ã®JJUG CCCã®ã¹ã©ã¤ãããªãããããã¨æ¸ãã¦ãã
- nullãå«ã
- 2019å¹´ãæ¸ããã®ã¯2015å¹´ã ã£ã¦è¨ã訳ããªããæãããã¨ã ã
- Optionalã®åãæ±ããã
- 2015å¹´ãJava8ã§ç»å ´ãã
Optional
ã«è²ã æå¾ ãããã©ãã¾ãã»ã©ã»ã©ãªã¨ããã«è½ã¡çããããªã¼ã£ã¦æã£ã¦ã¾ãã
- 2015å¹´ãJava8ã§ç»å ´ãã
- nullãå®æã«ä½¿ããªã
- 2012å¹´ããã10å¹´å¼±åã®ç§ããã¾ã ã«ãããªè©±ãã¦ããï¼
以éã¯é·æã§ããããã®æã®ã¯å®åã§ããã¤ãã®æ¡ä»¶ãæºãããä¸ã§ã®è©±ã«ãªãã¾ãã ãªãã¹ãæ¸ããã¤ããã§ãããç¡æèã«åæã¨ãã¦ãããã®ã¯ã¹ã³ã¼ã³ã¨æãã¦ããã§ãããããã¾ãã£ããåãæ¡ä»¶ã§ãããã¡ãã®æ¹ããããã¨ããé¸æè¢ããã£ã¦ããã¨æãã¾ãããã®è¾ºã®è©±ã¯å®åã§ãããããªã¼ã£ã¦æã£ã¦ã¾ãã
æ¬ç¨¿ã¯ãJavaã®è©±ãã¨ãã¦æ¸ãã¦ã¾ãããä»ã®è¨èªã«ããã°ãããã¨ãã¯ç°ãªã話ã§ãã
ç§ã®åºæ¬çãªèã
NullPointerException
ï¼ä»¥éNPEï¼ã¯èµ·ããã¹ãã¨ããã§èµ·ãã£ã¦è¯ããããµãããã¨äºæããªã null
ãæ··å
¥ããéã«çºçããé©åãªä¾å¤ã¯NPEã§ãããä»ã®ä½ãã§åãç¹ãå¿
è¦ã¯ãªãã
äºæããªã null
ã null
ãã§ãã¯ã§èª¤éåããªããnull
ãã§ãã¯ã㦠null
ãè¿ããããªãã®ã¯å»¶å½æªç½®ã«ãããªããªããããããªå»¶å½ãã¦ãéã«ã©ãã©ãå·ã¯æ·±ããªãããã§ã¤ã«ãã¡ã¹ãã
ãã®ãã㪠null
ã¯æ··å
¥ãããå¢çã§æ¤ç«ããä¸å¯§ã«åãé¤ããå®è£
ã¯åé·ã§è¯ããä¸æã«æ ¼å¥½è¯ã null
ãåé¿ã§ããã¨ãä¸å¾ãããè¡ããããªæèåæ¢ãæããããªãã null
ã¯å¿
è¦ãªã¨ããã§ã ãã§ãæ³¥èãããµãç®ç«ã¤å½¢ã§åãæ±ãã
ããå°ã詳ãã話
ï¼ãªããã©ã£ãã§æ¸ããã¨æããã ãã©ãªããã¨æããªããï¼
null
ãæ··å
¥ããã¿ã¤ãã³ã°ã¯ãããªãã«éå®çã§ãããäºæ¸¬å¯è½ã§ãã
- èªèº«ã§
null
ãæ示çã«ä½¿ç¨ããå ´åãå¤æ°ã¸ä»£å ¥ããããã¡ã½ããã®å¼æ°ãæ»ãå¤ã«ãããã¨ã§ä¼æãããæ¯è¼ã§ã®ä½¿ç¨ã¯åé¡ãªãã - åç §åãã£ã¼ã«ãã®ããã©ã«ãå¤ã
- ãã¬ã¼ã ã¯ã¼ã¯ã«è¨å®ãããå¼æ°ã
- ã©ã¤ãã©ãªã®ã¡ã½ããã®æ»ãå¤ã
ãã®ãã¡åè
2ã¤ã¯èªèº«ã®å®è£
ã®è©±ã§ãããã£ã¼ã«ãã®ããã©ã«ãå¤ã¯å®å
¨ã³ã³ã¹ãã©ã¯ã¿ã«æãã°ãèªèº«ãæ··å
¥ãããã®ã¯ null
ãæ示çã«è¨è¿°ããå ´åã«éå®ããã¾ããã³ã³ã¹ãã©ã¯ã¿å¼æ°ã« null
ãåæã«çªã£è¾¼ã¾ããã®ã¯ãå¾ã®ãã¬ã¼ã ã¯ã¼ã¯ãã©ã¤ãã©ãªã®è©±ã
è±ç·: ã¡ãªã¿ã« JIG ã¯ãã¤ãã³ã¼ããã null
ãè¦ã¤ãã¦è¦åãã¦ãã¾ããèªèº«ã®ã³ã¼ãã«ããæ··å
¥ãã§ãã«ã¼ãä½è
ã® null
å«ãããããã¾ããï¼ãã ã®èªå·±ç´¹ä»ï¼ã
èªèº«ã§å¶å¾¡ãã¥ããã®ã¯å¾è
ã®2ã¤ã§ããããã¬ã¼ã ã¯ã¼ã¯ãã©ã¤ãã©ãªã®å®è£
é½åã¨ã®æ¥ç¹ã§ null
ã¯çä»ãã¦ãã¾ãããã«è¨è¨ãã¾ãã
æè¿ã®ãã¬ã¼ã ã¯ã¼ã¯ãã©ã¤ãã©ãªã¯ null
ã®ä»£ããã« Optional
ãé¸ã¹ããã®ãå¢ãã¦ãã¾ããããããã¯å¥ã«è§£ã§ã¯ããã¾ãããã¦ã Optional
ã§ãã£ã¦ãèæ
®ããªãããããªããã¨ã«ã¯ä»£ãããªãã®ã§ãå¯è½ã§ããã° null
ãempty㪠Optional
ãåãé¤ããäºæããªãå¤ã§ããã°ããã§ç´ ç´ã«ä¾å¤ãçºçããã¾ããäºæã§ãã null
ã§ããã°ãé©åãªåãæ±ãããã®å¢çã§æ±ºå®ããã³ã¢ã§ããå¦çã«ã¯æã¡è¾¼ã¾ãªãããã«ãã¾ãã
ï¼ï¼ï¼TODO ä¾ç¤ºï¼ï¼ï¼
null
ã¯è¨èªé½åãå®è£
é½åã§æ··å
¥ãããã®ãªã®ã§ãã³ã¢ãªã¨ããã« null
ã¯å
¥ã£ã¦ãã¦æ¬²ãããªãããã§ããJavaã¨ããè¨èªä¸ã©ãã«ã§ãå
¥ããã null
ããã®ã¾ã¾ã«ãã¦ããã¨ãæ¬å½ã«ãããããã¨ã null
ã®èæ
®ã«å§è¿«ããã¾ããèããªããã°ãªããªããã¨ãæ¸ãããè¨è¨ã®åºæ¬ã ã¨æã£ã¦ããã®ã§ãå®å
¨ãªé åãå®ãã¦å¢çã§å¼¾ããå¼¾ãæãããã®ã¯ç´ ç´ã«NPEã®ãã£ã¼ãããã¯ãåãåãããããªæãã§ãã
nullã»ã¼ããªæ¼ç®å
ãã¨ãã°Groovyã«ã¯ ?.
ãããã¾ãã
null
ã ã£ãå ´åãæ°ã«ããã«ã¡ã½ãããå¼ã³åºããã®ããã«è¨è¿°ã§ãã null
ã®å ´åã¯å¼ã³åºãããã«null
ãè¿ã便å©ããã§ãããä»ã®è¨èªã§ãä¼¼ããããªæ©è½ã¯çµæ§è¦ãã¨æãã¾ããå±æçã«ã¯ã·ã³ãã«ãªã³ã¼ããè¨è¿°ã§ãã¾ãã
ããã¯å°ãªãã¨ãç§ãJavaã§æ¥åå¦çãæ¸ãæã«ã¯åå¨ãã¦ãã¦æ¬²ãããªããã®ã§ãã
éå
·ãæªãã®ã§ã¯ãªã使ãæ¹ã®åé¡ã§ã¯ãããã§ããããhoge.method()
ã¨æ¸ãããã®ã§ãã£ã¦ã hoge?.method()
ã¨æ¸ãã¦ããã°å®å
¨ãã®ãããªæèåæ¢ãæãããã§ããä¸å¾ããã®ã¯é¿ããã¨ãã¦ãããNPEãçºçããã .
ã ?.
ã«æ¸ãæããããããªãã¨ãèµ·ããã¾ããç§ã¯æå¿ãå¼±ãã®ã§ããã®å¼åã«è² ããªãèªä¿¡ã¯ããã¾ãããããã¦ãã£ã¨å¾æããã
ããã¯ãå¼·åãªæ©è½ãè¨è¨ã®æªã¿ãè¦ãé ãããã¿ã¼ã³ã ã¨æã£ã¦ãã¾ãã null
ãæ··å
¥ããªããªã£ãå ´åã«é¤å»ãããã©ã¼ã¹ãåãã¾ãããããã¦ãã®ã³ã¼ããããéãã null
ã¯æ··å
¥ãããã®ãï¼ã¨çå¿æ鬼ã«ãªãã¾ãã1ç®æãããã ãããä»ã§ãå
¥ã£ã¦ããããããããâ¦â¦ã¨ããããªãã¤ãºãå
¥ããªããã³ã¼ããèªã¿æ¸ãããããªããã ã
ã§ãããè¨èªæ©è½ã¨ãã¦æã£ã¦ãããç¹å®ã®æèï¼ã©ã¤ãã©ãªã¨ããã¬ã¼ã ã¯ã¼ã¯ä½ã£ã¦ãæã¨ãï¼ã§ã¯åãã§ä½¿ãã¨æãã便å©ã¯ä¾¿å©ã ããããã
JEP 358: Helpful NullPointerExceptions
2021年風å³ãæ··ãã¦ããã¾ãã
Java14ã§å
¥ã£ãJEP 358ã¯ãä»å¹´9æãªãªã¼ã¹äºå®ã§ããããã°ããã®ã¹ã¿ã³ãã¼ãã«ãªãã¨æãããJava17ã§ä½¿ç¨ã§ãã¾ãã
ããã¯ä½ãã¨ããã¨ãã¡ã½ããããã§ã¼ã³ãã¦ããå ´åãªã©ã®éä¸ã§NPEãçºçããå ´åãã©ãã null
ããæãã¦ãããæ©è½ã§ãã
以ä¸ã¯ 16.0.0-librca
ã§ã®å®è¡ä¾ã§ãã
jshell> class A { Object obj;} | 次ãä½æãã¾ãã: ã¯ã©ã¹ A jshell> var a = new A() a ==> A@5f184fc6 jshell> a.obj.toString() | ä¾å¤java.lang.NullPointerException: Cannot invoke "Object.toString()" because "REPL.$JShell$12.a.obj" is null | at (#3:1) jshell>
Java13以å㧠a.obj.toString()
ã®ãããªã³ã¼ãã§NPEãçºçããå ´åãã¹ã¿ãã¯ãã¬ã¼ã¹ã«ã¯è¡çªå·ãã表示ããããa
ã null
ãªã®ã obj
ã null
ãªã®ããããã¾ããã§ããããã®ããNPEã®è§£æã§ã¯ä¸æå¤æ°ã§åãã¦ã¿ãããa
ã a.obj
ããããã«å¯¾ã㦠null
ãã§ãã¯ãè¡ã£ã¦ã¿ããªã©ã®è¶³æ»ããå¿
è¦ã§ããããããã¬ãªã©ã使ç¨ããªãå ´åãã³ã¼ããå¤ããªãã¨è§£æãå°é£ãªãã¨ãããï¼å°ãªãã¨ãã¹ã¿ãã¯ãã¬ã¼ã¹ããç¬æã«å¤æã¯ã¤ããªãï¼ãNPEãå«ç
ãããä¸å ã ã£ããã¨æãã¾ãã
Java14以éã§ã¯Objects#requireNonNull(Object)
ã¡ã½ãããèªèº«ã§ null
ãã§ãã¯ãã¦ã®ä¾å¤éåºããããã®ã¾ã¾NPEãèµ·ãããæ¹ã解æããããã¾ã§ããã¾ããäºæããªãNPEã«ã¯åããããããªããäºæããªãnull
ãæãã¦ãããNPEãç´ ç´ã«åãæ¢ããã®ãããã¨æãã¾ãã
nullã許容ããå ´å
ã©ã¤ãã©ãªãè¦æ±ããAPIã«ãªã£ã¦ããå ´åã¨ãéããã¹ã³ã¼ãã§ä½¿ç¨ããç¹æ®å¤ã§ãã
ã¯ã©ã¹ã«éãããã£ã¼ã«ãã§ãæã軽éã«æ±ããç¹æ®å¤ã¯ãããã null
ã§ããããã以å¤ã®å¤ã使ç¨ããã®ã¯ã³ã¹ããä¸åãã¾ãã
ãã ãã® null
ãå¤é¨ã«æµåºããªãããã«è¨è¨ï¼å¶å¾¡ããå¿
è¦ã¯ããã¾ãããå¶å¾¡å¯è½ãªç¯å²ã«ãã㦠null
以å¤ãé å¼µã£ã¦ä½¿ç¨ãããããã¯æ±ããããã§ããããã¯åã« null
ãè¨èªã«çµã¿è¾¼ã¾ããç¹æ®å¤ã§ãããæ
ã§ã使ãããã使ãã¨è¨ãã¹ã¿ã³ã¹ã§ãã決ãã¦ã null
ãæé©è§£ãã§ã¯ãªãã®ã§ã誤解ããã¬ããããã
ã¾ã¨ã
NPEãããµãã£ã¦æ°æã¡ã¯ããããªãã¯ãªãã§ãããNPE以å¤ã®äºæããªãå®è¡æä¾å¤ãã©ããããµãã§ããNPEãããªããåªåãªãã¦è¦ããªãããããªãããªã£ã¦ã
ãã¼ null
ç¡ããªããªãããªã¼ã
ãã¾ãï¼2024-05-12T0:03ï¼
æ稿ããããµã ãã«åºãç»åããã¾ãã§ã¤ãã¦ããã¾ãã
ãã¶ã ï¼ï¼ï¼TODO ä¾ç¤ºï¼ï¼ï¼
ã§ãããã£ã¦æ£²ã¿åãããã ã¼ã¿ãããªãã¨ãæ¸ããã¨ãã¦ããã ã¨æãã¾ãã
ãã¡ã¤ã«åã 20210421013620.png
ã¨ãããå¯ããã«ããããã¢ã¬ã§ãããè¦ãã¦ãªãã
Miroã§æ¸ãã¦ãã£ã½ãããã©ããã®ãã¼ãã«æ®éª¸ã¯ãããã ããã©â¦â¦