{"id":16047,"date":"2024-07-22T09:38:39","date_gmt":"2024-07-22T01:38:39","guid":{"rendered":"https:\/\/92it.top\/?p=16047"},"modified":"2024-07-22T09:38:39","modified_gmt":"2024-07-22T01:38:39","slug":"%e5%a6%82%e4%bd%95%e7%bc%96%e5%86%99-null-safety-%e4%bb%a3%e7%a0%81%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/92it.top\/?p=16047","title":{"rendered":"\u5982\u4f55\u7f16\u5199 Null-Safety \u4ee3\u7801\uff1f"},"content":{"rendered":"\n<p>\u8f6c\u8f7d\uff1a<a href=\"https:\/\/mp.weixin.qq.com\/s?__biz=MzIwNDAyOTI2Nw==&amp;mid=2247498447&amp;idx=1&amp;sn=e518adb323c53b72dc861abf6d797679&amp;chksm=96c4d6f3a1b35fe5cf2b3fd877af7abcf96c24dd039ad66eb5aaa55ee082e1dcc962b114bc99&amp;cur_album_id=2818595943724974083&amp;scene=190#rd\">\u5982\u4f55\u7f16\u5199 Null-Safety \u4ee3\u7801\uff1f\u4e00\u6587\u5e2e\u4f60\u8bb2\u900f\uff01<\/a><\/p>\n\n\n\n<p>NullPointerExceptions\uff08NPE\uff09\u7a7a\u6307\u9488\u5f02\u5e38\u5e94\u8be5\u662f\u6bcf\u4e2a\u7a0b\u5e8f\u5458\u7684\u5669\u68a6\uff0c\u4f5c\u4e3a Java \u7a0b\u5e8f\u5458\u90fd\u77e5\u9053\uff1aNPE \u662f\u8fd0\u884c\u65f6\u5f02\u5e38\uff0c\u5728\u4ee3\u7801\u7f16\u8bd1\u8fc7\u7a0b\u4e2d\u5f88\u96be\u53d1\u73b0\u8fd9\u79cd\u5f02\u5e38\u3002\u56e0\u6b64\uff0c\u8fd9\u7bc7\u6587\u7ae0\uff0c\u6211\u4eec\u5c06\u5206\u6790\u5982\u4f55\u7f16\u5199 null \u5b89\u5168\u4ee3\u7801\u3002<\/p>\n\n\n\n<p>\u5728\u5b9e\u9645\u5de5\u4f5c\u4e2d\uff0c\u5bf9\u4e8e null\u7684\u5904\u7406\u901a\u5e38\u6709\u4ee5\u4e0b\u5904\u7406\u65b9\u5f0f\uff1a<\/p>\n\n\n\n<ul>\n<li>Nullable\u6ce8\u89e3<\/li>\n\n\n\n<li>NonNull\u6ce8\u89e3<\/li>\n\n\n\n<li>\u663e\u793a\u5904\u7406 null<\/li>\n\n\n\n<li>\u4f7f\u7528 Optional\u7c7b<\/li>\n\n\n\n<li>\u4f7f\u7528\u65ad\u8a00<\/li>\n\n\n\n<li>\u7f16\u5199\u6e05\u6670\u7684\u6587\u6863<\/li>\n<\/ul>\n\n\n\n<p><strong>Nullable\u6ce8\u89e3<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><code>@Nullable<\/code>\u662f Spring \u7684\u5185\u7f6e\u6ce8\u89e3\uff0c\u53ef\u7528\u4e8e\u65b9\u6cd5\u3001\u53c2\u6570\u6216\u5b57\u6bb5\u7ea7\u522b\uff0c\u7528\u4e8e\u6807\u8bb0\u65b9\u6cd5\u7684\u8fd4\u56de\u503c\u6216\u53c2\u6570\u6216\u5b57\u6bb5\u53ef\u4ee5\u4e3a null\uff0c\u901a\u8fc7\u4f7f\u7528 <code>@Nullable<\/code> &nbsp;\u6ce8\u89e3\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u660e\u786e\u5730\u8868\u660e\u67d0\u4e2a\u65b9\u6cd5\u53ef\u80fd\u8fd4\u56de null \u503c\uff0c\u4ece\u800c\u5728\u8c03\u7528\u8be5\u65b9\u6cd5\u65f6\u66f4\u52a0\u8c28\u614e\u3002<\/p>\n\n\n\n<p>\u5982\u4e0b\u793a\u4f8b\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/\/ \u793a\u4f8b1\uff1a\u7528\u4e8e\u65b9\u6cd5\n@Nullable\npublic String getUserById(Long userId) {\n    if (userId == null) {\n        return null;\n    }\n    \/\/ \u6839\u636euserId\u67e5\u8be2\u7528\u6237\u90ae\u7bb1\n    return userRepository.findUserById(userId);\n}\n\n\n\/\/ \u793a\u4f8b2\uff1a\u7528\u4e8e\u53c2\u6570\nUser addUser(@NonNull Long userId, @Nullable String email);\n\n\/\/ \u793a\u4f8b3\uff1a\u7528\u4e8e\u5b57\u6bb5\npublic class User {\n  @NonNull\n  Long userId;\n  \n  @Nullable\n  String email;\n}<\/pre>\n\n\n\n<p>\u5728\u4e0a\u8ff0\u7684\u793a\u4f8b\u4e2d\uff0c<code>@Nullable<\/code> \u7528\u4e8e\u65b9\u6cd5\uff0c\u5b57\u6bb5\uff0c\u53c2\u6570\uff0c\u6807\u8bc6\u53ef\u4ee5\u4e3a null\uff0c\u56e0\u6b64\uff0c\u65b9\u6cd5\u7684\u8c03\u7528\u7528\u5c31\u9700\u8981\u6839\u636e\u4e1a\u52a1\u5224\u65ad null\u3002<\/p>\n\n\n\n<p><strong>NonNull\u6ce8\u89e3<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><code>@NonNull<\/code> \u662f Spring \u7684\u5185\u7f6e\u6ce8\u89e3\uff0c\u53ef\u7528\u4e8e\u65b9\u6cd5\u3001\u53c2\u6570\u6216\u5b57\u6bb5\u7ea7\u522b\uff0c\u7528\u4e8e\u6807\u8bb0\u65b9\u6cd5\u7684\u8fd4\u56de\u503c\u6216\u53c2\u6570\u6216\u5b57\u6bb5\u4e0d\u80fd\u4e3a null\u3002\u901a\u8fc7\u4f7f\u7528 <code>@NonNull<\/code> \u6ce8\u89e3\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u660e\u786e\u5730\u8868\u660e\u67d0\u4e2a\u65b9\u6cd5\u4e0d\u4f1a\u8fd4\u56de null \u503c\uff0c\u4ece\u800c\u5728\u8c03\u7528\u8be5\u65b9\u6cd5\u65f6\u53ef\u4ee5\u653e\u5fc3\u5730\u4f7f\u7528\u8fd4\u56de\u503c\u3002<\/p>\n\n\n\n<p>\u5982\u4e0b\u793a\u4f8b\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import org.springframework.lang.NonNull;\n\/\/ \u793a\u4f8b1\uff1a\u7528\u4e8e\u65b9\u6cd5\n@NonNull\npublic User getUserById(Long userId) {\n    \/\/ \u6839\u636euserId\u67e5\u8be2\u7528\u6237\u4fe1\u606f\n    User user = userRepository.findUserById(userId);\n    if (user == null) {\n        throw new IllegalArgumentException(\"User not found\");\n    }\n    return user;\n}\n\n\/\/ \u793a\u4f8b2\uff1a\u7528\u4e8e\u53c2\u6570\nUser findUserById(@NonNull Long userId);\n\n\/\/ \u793a\u4f8b3\uff1a\u7528\u4e8e\u5b57\u6bb5\npublic class User {\n    @NonNull\n    Long id;\n}<\/pre>\n\n\n\n<p>\u5728\u4e0a\u8ff0\u7684\u793a\u4f8b\u4e2d\uff1a\u793a\u4f8b1\uff0c<code>@NonNull<\/code> \u6ce8\u89e3\u6807\u8bb0\u65b9\u6cd5\u5176\u8fd4\u56de\u503c\u4e0d\u80fd\u4e3a null\uff0c\u56e0\u6b64\uff0c\u65b9\u6cd5\u7684\u8c03\u7528\u8005\u77e5\u9053\u8be5\u65b9\u6cd5\u4e0d\u4f1a\u8fd4\u56de null \u503c\uff0c\u53ef\u4ee5\u653e\u5fc3\u5730\u4f7f\u7528\u8fd4\u56de\u503c\u800c\u4e0d\u7528\u62c5\u5fc3\u7a7a\u6307\u9488\u5f02\u5e38\u3002<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"217\" src=\"https:\/\/92it.top\/wp-content\/uploads\/2024\/07\/image-221-1024x217.png\" alt=\"\" class=\"wp-image-16049\" style=\"width:534px;height:auto\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2024\/07\/image-221-1024x217.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2024\/07\/image-221-300x64.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2024\/07\/image-221-768x163.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2024\/07\/image-221-830x176.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2024\/07\/image-221-230x49.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2024\/07\/image-221-350x74.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2024\/07\/image-221-480x102.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2024\/07\/image-221.png 1356w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure><\/div>\n\n\n<p>\u793a\u4f8b2\uff0c<code>@NonNull<\/code> \u6ce8\u89e3\u6807\u8bb0\u65b9\u6cd5\u7684\u5165\u53c2\u4e0d\u80fd\u4e3a null<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"99\" src=\"https:\/\/92it.top\/wp-content\/uploads\/2024\/07\/image-223-1024x99.png\" alt=\"\" class=\"wp-image-16051\" style=\"width:546px;height:auto\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2024\/07\/image-223-1024x99.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2024\/07\/image-223-300x29.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2024\/07\/image-223-768x74.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2024\/07\/image-223-830x80.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2024\/07\/image-223-230x22.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2024\/07\/image-223-350x34.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2024\/07\/image-223-480x46.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2024\/07\/image-223.png 1346w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure><\/div>\n\n\n<p>\u793a\u4f8b3\uff0c<code>@NonNull<\/code> \u6ce8\u89e3\u6807\u8bb0\u5b57\u6bb5\u4e0d\u80fd\u4e3a null\uff0c\u4e00\u65e6\u5b57\u6bb5\u503c\u4e3a null\uff0cIDE\u4f1a\u51fa\u73b0\u4ee5\u4e0b\u8b66\u544a\uff1a<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"143\" src=\"https:\/\/92it.top\/wp-content\/uploads\/2024\/07\/image-225-1024x143.png\" alt=\"\" class=\"wp-image-16053\" style=\"width:536px;height:auto\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2024\/07\/image-225-1024x143.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2024\/07\/image-225-300x42.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2024\/07\/image-225-768x107.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2024\/07\/image-225-830x116.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2024\/07\/image-225-230x32.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2024\/07\/image-225-350x49.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2024\/07\/image-225-480x67.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2024\/07\/image-225.png 1360w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure><\/div>\n\n\n<p><strong>\u663e\u5f0f\u5904\u7406 null<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u663e\u5f0f\u5904\u7406 null\u5e94\u8be5\u662f\u6211\u4eec\u4f7f\u7528\u6700\u591a\u4e5f\u662f\u6700\u7075\u6d3b\u5904\u7406 null\u7684\u65b9\u6cd5\uff0c\u5728\u7f16\u5199\u4ee3\u7801\u65f6\uff0c\u8981\u6ce8\u610f\u5bf9\u53ef\u80fd\u4e3a null \u7684\u503c\u8fdb\u884c\u663e\u5f0f\u7684\u7a7a\u503c\u68c0\u67e5\u548c\u5904\u7406\uff0c\u53ef\u4ee5\u4f7f\u7528 if \u8bed\u53e5\u3001try-catch\u5757\u7b49\u65b9\u5f0f\u6765\u5904\u7406\u7a7a\u503c\u60c5\u51b5\uff0c\u907f\u514d\u7a0b\u5e8f\u5d29\u6e83\u6216\u6570\u636e\u4e22\u5931\u3002<\/p>\n\n\n\n<p>\u5982\u4e0b\u4ee3\u7801\uff0c\u9488\u5bf9 user \u5bf9\u8c61\u662f\u5426\u4e3a null \u505a\u4e0d\u540c\u7684\u5904\u7406\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/\/ \u65b9\u5f0f1\nif (user != null) {\n    \/\/ \u6267\u884c\u64cd\u4f5c\n} else {\n    \/\/ \u5904\u7406\u7a7a\u503c\u60c5\u51b5\n}\n\n\/\/ \u65b9\u5f0f2\ntry {\n   \/\/ \u4e1a\u52a1\u5904\u7406\n} catch (Exception e) {\n  \/\/ \u5f02\u5e38\u5904\u7406\n}<\/pre>\n\n\n\n<p><strong>\u4f7f\u7528 Optional\u7c7b<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>Optional\u7c7b\u662f Java 8 \u5f15\u5165\u7684\uff0c\u5b83\u7684\u4f5c\u7528\u662f\u5c01\u88c5\u4e00\u4e2a\u53ef\u80fd\u4e3a\u7a7a\u7684\u503c\uff0c\u901a\u8fc7 Optional \u7c7b\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u66f4\u52a0\u5b89\u5168\u548c\u4f18\u96c5\u5730\u5904\u7406\u7a7a\u503c\u60c5\u51b5\uff0c\u4ece\u800c\u907f\u514d NPE \u7a7a\u6307\u9488\u5f02\u5e38\u3002<\/p>\n\n\n\n<p>\u5982\u4e0b\u793a\u4f8b\uff0c\u9488\u5bf9\u4e0a\u9762\u624b\u52a8\u5904\u7406 null\uff0c \u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 optional\u66f4\u4f18\u96c5\u7684\u5b9e\u73b0 null \u5224\u65ad:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">Optional&lt;User> userOptional = Optional.ofNullable(user);\nString userName = userOptional.map(User::getName).orElse(\"Unknown\");<\/pre>\n\n\n\n<p>\u5173\u4e8e Optional \u66f4\u591a\u7684\u4f7f\u7528\uff0c\u53c2\u8003\u4e0b\u56fe\uff1a<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"1009\" height=\"1024\" src=\"https:\/\/92it.top\/wp-content\/uploads\/2024\/07\/image-227-1009x1024.png\" alt=\"\" class=\"wp-image-16055\" style=\"width:458px;height:auto\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2024\/07\/image-227-1009x1024.png 1009w, https:\/\/92it.top\/wp-content\/uploads\/2024\/07\/image-227-296x300.png 296w, https:\/\/92it.top\/wp-content\/uploads\/2024\/07\/image-227-768x780.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2024\/07\/image-227-830x842.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2024\/07\/image-227-230x233.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2024\/07\/image-227-350x355.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2024\/07\/image-227-480x487.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2024\/07\/image-227.png 1330w\" sizes=\"(max-width: 1009px) 100vw, 1009px\" \/><\/figure><\/div>\n\n\n<p>\u4f7f\u7528 Optional \u7c7b\u7684\u51e0\u4e2a\u597d\u5904\uff1a<\/p>\n\n\n\n<ul>\n<li>\u53ef\u4ee5\u907f\u514d NPE \u7a7a\u6307\u9488\u5f02\u5e38\uff0c\u63d0\u9ad8\u4ee3\u7801\u7684\u5065\u58ee\u6027\uff1b<\/li>\n\n\n\n<li>\u53ef\u4ee5\u51cf\u5c11\u624b\u52a8 null \u5224\u65ad\uff0c\u4f7f\u4ee3\u7801\u66f4\u7b80\u6d01\u548c\u4f18\u96c5\uff1b<\/li>\n\n\n\n<li>\u7b26\u5408\u51fd\u6570\u5f0f\u7f16\u7a0b\u7684\u7279\u6027\uff1b<\/li>\n<\/ul>\n\n\n\n<p><strong>\u4f7f\u7528\u65ad\u8a00<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u65ad\u8a00 assert \u53ef\u4ee5\u5e2e\u52a9\u53d1\u73b0\u6f5c\u5728\u7684\u7a7a\u6307\u9488\u5f02\u5e38\u95ee\u9898\uff0c\u786e\u4fdd\u4ee3\u7801\u7684\u5047\u8bbe\u5f97\u5230\u6b63\u786e\u9a8c\u8bc1\u3002<\/p>\n\n\n\n<p>\u5982\u4e0b\u793a\u4f8b\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">assert user != null : \"User object should not be null\";<\/pre>\n\n\n\n<p>\u5728\u4e0a\u8ff0\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u65ad\u8a00\u6765\u9a8c\u8bc1 user \u5bf9\u8c61\u4e0d\u4e3a null\uff0c\u5982\u679c\u65ad\u8a00\u5931\u8d25\uff0c\u5219\u629b\u51fa\u76f8\u5e94\u7684\u9519\u8bef\u4fe1\u606f\u3002<\/p>\n\n\n\n<p><strong>\u7f16\u5199\u6e05\u6670\u7684\u6587\u6863<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u5728\u65b9\u6cd5\u7684\u6ce8\u91ca\u4e2d\u660e\u786e\u8bf4\u660e\u65b9\u6cd5\u7684\u8fd4\u56de\u503c\u548c\u53c2\u6570\u662f\u5426\u53ef\u4ee5\u4e3a null\uff0c\u4ee5\u53ca\u5982\u4f55\u5904\u7406 null\u60c5\u51b5\u3002\u6e05\u6670\u7684\u6587\u6863\u53ef\u4ee5\u5e2e\u52a9\u5176\u4ed6\u5f00\u53d1\u8005\u66f4\u597d\u5730\u7406\u89e3\u4ee3\u7801\u903b\u8f91\u548c\u89c4\u8303\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/**\n * \u6839\u636e\u7528\u6237ID\u83b7\u53d6\u7528\u6237\u90ae\u7bb1\u5730\u5740\n * @param userId \u7528\u6237ID\n * @return \u7528\u6237\u90ae\u7bb1\u5730\u5740\uff0c\u53ef\u80fd\u4e3anull\n *\/\n@Nullable\npublic String getUserEmailById(Long userId) {\n    \/\/ \u65b9\u6cd5\u5b9e\u73b0\n}<\/pre>\n\n\n\n<p>\u4e0a\u8ff0\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u7f16\u5199\u4e86\u6e05\u6670\u7684\u65b9\u6cd5\u6ce8\u91ca\uff0c\u660e\u786e\u8bf4\u660e\u4e86\u65b9\u6cd5\u7684\u8fd4\u56de\u503c\u53ef\u80fd\u4e3a null\uff0c\u5e2e\u52a9\u5176\u4ed6\u5f00\u53d1\u8005\u66f4\u597d\u5730\u7406\u89e3\u65b9\u6cd5\u7684\u4f7f\u7528\u89c4\u8303\u548c\u5904\u7406\u7a7a\u503c\u60c5\u51b5\u3002<\/p>\n\n\n\n<p><strong>\u6ce8\u610f\u4e8b\u9879<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u5bf9\u4e8e\u4e0a\u8ff0 Spring \u7684 <code>@Nullable<\/code> \u548c <code>@NonNull<\/code> \u6ce8\u89e3\uff0c\u5e76\u975e\u6240\u6709\u5f00\u53d1\u5de5\u5177\u90fd\u80fd\u663e\u793a\u8fd9\u4e9b\u7f16\u8bd1\u8b66\u544a\uff0c\u5982\u679c\u672a\u770b\u5230\u76f8\u5173\u8b66\u544a\uff0c\u8bf7\u68c0\u67e5 IDE \u4e2d\u7684\u7f16\u8bd1\u5668\u8bbe\u7f6e\u3002\u8fd9\u91cc\u4ee5 IntelliJ \u8bbe\u7f6e\u4e3a\u4f8b\uff1a<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"252\" src=\"https:\/\/92it.top\/wp-content\/uploads\/2024\/07\/image-229-1024x252.png\" alt=\"\" class=\"wp-image-16057\" style=\"width:560px;height:auto\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2024\/07\/image-229-1024x252.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2024\/07\/image-229-300x74.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2024\/07\/image-229-768x189.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2024\/07\/image-229-830x204.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2024\/07\/image-229-230x57.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2024\/07\/image-229-350x86.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2024\/07\/image-229-480x118.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2024\/07\/image-229.png 1366w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure><\/div>\n\n\n<p>\u4e3a\u4e86\u65b9\u4fbf IDE \u8fdb\u884c\u4e00\u4e9b\u81ea\u52a8\u5316\u4ee3\u7801\u68c0\u67e5\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 <code>SpotBugs<\/code>\u63d0\u4f9b\u4e86\u4e00\u4e2a \u63d2\u4ef6\uff0c\u53ef\u4ee5\u68c0\u6d4b\u7531\u4e8e\u53ef\u4e3a null \u6027\u800c\u5bfc\u81f4\u7684\u4ee3\u7801\u5f02\u5e38\u3002<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"542\" src=\"https:\/\/92it.top\/wp-content\/uploads\/2024\/07\/image-230-1024x542.png\" alt=\"\" class=\"wp-image-16058\" style=\"width:616px;height:auto\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2024\/07\/image-230-1024x542.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2024\/07\/image-230-300x159.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2024\/07\/image-230-768x407.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2024\/07\/image-230-830x439.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2024\/07\/image-230-230x122.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2024\/07\/image-230-350x185.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2024\/07\/image-230-480x254.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2024\/07\/image-230.png 1360w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure><\/div>\n\n\n<p>\u5b89\u88c5\u4e0a\u8ff0\u63d2\u4ef6\u540e\uff0c\u5982\u679c\u4efb\u4f55\u7528 <code>@NonNull<\/code> \u6ce8\u91ca\u7684\u65b9\u6cd5\u610f\u5916\u8fd4\u56de null\uff0c\u5219 SpotBugs \u68c0\u67e5\u5c06\u5931\u8d25\uff0c\u5e76\u51fa\u73b0\u7c7b\u4f3c\u4e8e\u4ee5\u4e0b\u5185\u5bb9\u7684\u9519\u8bef\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">[ERROR] High: xxx may return null, but is declared @Nonnull  At xxx.java:[line 36] NP_NONNULL_RETURN_VIOLATION<\/pre>\n\n\n\n<p><strong>\u603b\u7ed3<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u672c\u6587\uff0c\u6211\u4eec\u603b\u7ed3\u4e86\u5bf9 null\u5904\u7406\u7684\u51e0\u79cd\u901a\u5e38\u65b9\u5f0f\uff1a<\/p>\n\n\n\n<ul>\n<li>Nullable\u6ce8\u89e3<\/li>\n\n\n\n<li>NonNull\u6ce8\u89e3<\/li>\n\n\n\n<li>\u663e\u793a\u5904\u7406 null<\/li>\n\n\n\n<li>\u4f7f\u7528 Optional\u7c7b<\/li>\n\n\n\n<li>\u4f7f\u7528\u65ad\u8a00<\/li>\n\n\n\n<li>\u7f16\u5199\u6e05\u6670\u7684\u6587\u6863<\/li>\n<\/ul>\n\n\n\n<p>\u529f\u592b\u5728\u5e73\u5e38\uff0c\u529f\u592b\u5728\u7ec6\u8282\uff01\u53ea\u8981\u6211\u4eec\u5728\u65e5\u5e38\u5f00\u53d1\u4e2d\u591a\u6ce8\u610f\u4e00\u70b9\u7ec6\u8282\uff0c\u8ba9\u597d\u4e60\u60ef\u6210\u4e3a\u81ea\u7136\uff0c\u7ec8\u6709\u4e00\u5929\uff0c\u4e0d\u4f46\u53ef\u4ee5\u5199\u51fa\u8ba9\u4eba\u7fa1\u6155\u7684\u4f18\u96c5\u4ee3\u7801\uff0c\u8fd8\u53ef\u4ee5\u5199\u51fa\u9ad8\u8d28\u91cf\u7684\u4ee3\u7801\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8f6c\u8f7d\uff1a\u5982\u4f55\u7f16\u5199 Null-Safety \u4ee3\u7801\uff1f\u4e00\u6587\u5e2e\u4f60\u8bb2\u900f\uff01 NullPointerExceptions\uff08NP [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[26],"tags":[],"_links":{"self":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/16047"}],"collection":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=16047"}],"version-history":[{"count":1,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/16047\/revisions"}],"predecessor-version":[{"id":16060,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/16047\/revisions\/16060"}],"wp:attachment":[{"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=16047"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=16047"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=16047"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}