{"id":10020505,"date":"2024-12-18T12:33:26","date_gmt":"2024-12-18T04:33:26","guid":{"rendered":"https:\/\/itmax.cn\/?p=10020505"},"modified":"2024-12-18T12:33:26","modified_gmt":"2024-12-18T04:33:26","slug":"%e5%96%84%e7%94%a8optional%ef%bc%8c%e5%91%8a%e5%88%abnpe","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10020505","title":{"rendered":"\u5584\u7528Optional\uff0c\u544a\u522bNPE"},"content":{"rendered":"<p>\u4f5c\u8005\uff1a\u4eac\u4e1c\u7269\u6d41 \u738b\u4e9a\u5b81<\/p>\n<h2>1\u3001NPE\u662f\u4ec0\u4e48\uff1f<\/h2>\n<p>NPE:NullPointerException\uff08\u7a7a\u6307\u9488\u5f02\u5e38\uff09\u3002\u53ef\u4ee5\u8bf4\u81eaNull\u7684\u8bde\u751f\u4ee5\u6765\u5b83\u5c31\u8ba9\u65e0\u6570\u7684\u7a0b\u5e8f\u5458\u4e3a\u4e4b\u54c0\u568e\uff0c\u4e5f\u662f\u65e0\u6570\u7cfb\u7edfBug\u7684\u6765\u6e90\u3002\u6258\u5c3c\u00b7\u970d\u5c14\uff08Tony Hoare\uff09\uff0cNull\u7684\u53d1\u660e\u8005\u4e5f\u8868\u793a\u8fc7\u8fd9\u662f\u4ed6\u5341\u4ebf\u7f8e\u5143\u7684\u9519\u8bef\u3002\u5f53\u7a0b\u5e8f\u8bd5\u56fe\u5728\u7a7a\u5f15\u7528\uff08null\uff09\u4e0a\u8c03\u7528\u65b9\u6cd5\u6216\u8bbf\u95ee\u5c5e\u6027\u65f6\uff0cJVM\u4f1a\u629b\u51faNPE\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code>String str = null;\nint length = str.length(); \/\/ \u8fd9\u91cc\u4f1a\u629b\u51faNPE<\/code><\/pre>\n<h3>1.1\u3001NPE\u7684\u5e38\u89c1\u539f\u56e0<\/h3>\n<p>\u672a\u521d\u59cb\u5316\u7684\u5bf9\u8c61\uff1a\u53d8\u91cf\u58f0\u660e\u540e\u672a\u8d4b\u503c\uff0c\u5373\u9ed8\u8ba4\u4e3anull\u3002<\/p>\n<p>\u65b9\u6cd5\u8fd4\u56denull\uff1a\u65b9\u6cd5\u53ef\u80fd\u8fd4\u56de\u4e00\u4e2a\u5bf9\u8c61\u6216null\uff0c\u4f46\u8c03\u7528\u8005\u672a\u8fdb\u884cnull\u68c0\u67e5\u3002<\/p>\n<p>\u96c6\u5408\u4e2d\u7684null\u5143\u7d20\uff1a\u96c6\u5408\u64cd\u4f5c\u4e2d\u63d2\u5165\u4e86null\uff0c\u540e\u7eed\u64cd\u4f5c\u672a\u5904\u7406\u3002<\/p>\n<p>\u591a\u7ebf\u7a0b\u73af\u5883\u4e2d\u7684\u7ade\u6001\u6761\u4ef6\uff1a\u4e00\u4e2a\u7ebf\u7a0b\u4fee\u6539\u5bf9\u8c61\u72b6\u6001\u4e3anull\uff0c\u53e6\u4e00\u4e2a\u7ebf\u7a0b\u672a\u53ca\u65f6\u68c0\u67e5\u3002<\/p>\n<h3>1.2\u3001NPE\u7684\u5f71\u54cd<\/h3>\n<p>\u7a0b\u5e8f\u5d29\u6e83\uff1a\u672a\u5904\u7406\u7684NPE\u4f1a\u5bfc\u81f4\u7a0b\u5e8f\u7ec8\u6b62\uff0c\u5f71\u54cd\u7528\u6237\u4f53\u9a8c\u3002<\/p>\n<p>\u8c03\u8bd5\u56f0\u96be\uff1aNPE\u7684\u5806\u6808\u4fe1\u606f\u53ef\u80fd\u4e0d\u76f4\u89c2\uff0c\u5b9a\u4f4d\u95ee\u9898\u6e90\u5934\u8017\u65f6\u3002<\/p>\n<p>\u4ee3\u7801\u8d28\u91cf\u4e0b\u964d\uff1a\u9891\u7e41\u7684NPE\u8868\u660e\u4ee3\u7801\u7f3a\u4e4f\u5065\u58ee\u7684null\u5904\u7406\u673a\u5236\u3002<\/p>\n<h2>2\u3001Optional\u5e93\u4ecb\u7ecd<\/h2>\n<p>\u4e3a\u4e86\u5e94\u5bf9NPE\u95ee\u9898\uff0cJava 8\u5f15\u5165\u4e86Optional\u7c7b\uff0c\u5b83\u662f\u4e00\u4e2a\u5bb9\u5668\u5bf9\u8c61\uff0c\u53ef\u4ee5\u5305\u542b\u6216\u4e0d\u5305\u542b\u975enull\u7684\u503c\u3002\u901a\u8fc7Optional\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u663e\u5f0f\u5730\u8868\u793a\u4e00\u4e2a\u503c\u662f\u53ef\u9009\u7684\uff0c\u4ece\u800c\u5f3a\u5236\u8fdb\u884cnull\u68c0\u67e5\uff0c\u51cf\u5c11NPE\u7684\u53d1\u751f\u3002<\/p>\n<h3>2.1\u3001Optional\u7684\u57fa\u672c\u7528\u6cd5<\/h3>\n<pre><code>\u521b\u5efaOptional\u5bf9\u8c61\nOptional&lt;String&gt; optional = Optional.of(&quot;Hello&quot;); \/\/ \u521b\u5efa\u5305\u542b\u503c\u7684Optional\nOptional&lt;String&gt; emptyOptional = Optional.empty(); \/\/ \u521b\u5efa\u7a7a\u7684Optional\nOptional&lt;String&gt; nullableOptional = Optional.ofNullable(null); \/\/ \u53ef\u4ee5\u63a5\u53d7null<\/code><\/pre>\n<pre><code>\u83b7\u53d6\u503c\n\/\/ \u4f7f\u7528get()\u83b7\u53d6\u503c\uff0c\u5982\u679c\u4e3a\u7a7a\u5219\u629b\u51faNoSuchElementException\noptional.get();\n\n\/\/ \u4f7f\u7528orElse()\u63d0\u4f9b\u9ed8\u8ba4\u503c\nString value = optional.orElse(&quot;Default&quot;);\n\n\/\/ \u4f7f\u7528orElseGet()\u63d0\u4f9b\u9ed8\u8ba4\u503c\u7684Supplier\nString value = optional.orElseGet(() -&gt; &quot;Default&quot;);\n\n\/\/ \u4f7f\u7528orElseThrow()\u5728\u503c\u4e3a\u7a7a\u65f6\u629b\u51fa\u5f02\u5e38\nString value = optional.orElseThrow(() -&gt; new IllegalArgumentException(&quot;Value is null&quot;));<\/code><\/pre>\n<pre><code>\u5904\u7406\u503c\n\/\/ \u4f7f\u7528ifPresent()\u5728\u503c\u5b58\u5728\u65f6\u6267\u884c\u64cd\u4f5c\noptional.ifPresent(val -&gt; System.out.println(val));\n\n\/\/ \u4f7f\u7528map()\u8f6c\u6362\u503c\nOptional&lt;Integer&gt; lengthOptional = optional.map(String::length);\n\n\/\/ \u4f7f\u7528flatMap()\u5904\u7406\u5d4c\u5957\u7684Optional\nOptional&lt;Optional&lt;String&gt;&gt; nestedOptional = Optional.of(Optional.of(&quot;Nested&quot;));\nOptional&lt;String&gt; flatOptional = nestedOptional.flatMap(opt -&gt; opt);<\/code><\/pre>\n<h3>2.2\u3001Optional\u7684\u4f18\u52bf<\/h3>\n<p>\u660e\u786e\u7684\u610f\u56fe\uff1a\u65b9\u6cd5\u8fd4\u56deOptional\u8868\u660e\u8fd4\u56de\u503c\u53ef\u80fd\u4e3a\u7a7a\uff0c\u589e\u5f3a\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u3002<\/p>\n<p>\u5f3a\u5236null\u68c0\u67e5\uff1a\u901a\u8fc7Optional\u7684\u65b9\u6cd5\u94fe\uff0c\u5f00\u53d1\u8005\u5fc5\u987b\u5904\u7406\u53ef\u80fd\u7684\u7a7a\u503c\uff0c\u51cf\u5c11\u9057\u6f0f\u3002<\/p>\n<p>\u51fd\u6570\u5f0f\u7f16\u7a0b\u652f\u6301\uff1a\u4e0eLambda\u8868\u8fbe\u5f0f\u548cStream API\u65e0\u7f1d\u7ed3\u5408\uff0c\u7b80\u5316\u4ee3\u7801\u903b\u8f91\u3002<\/p>\n<h2>3\u3001\u6700\u4f73\u5b9e\u4f8b\u793a\u4f8b<\/h2>\n<p>\u793a\u4f8b\u80cc\u666f<\/p>\n<p>\u5047\u8bbe\u6709\u4e00\u4e2a\u7528\u6237\u7c7bUser\uff0c\u5305\u542b\u4e00\u4e2a\u5730\u5740\u7c7bAddress\uff0c\u800c\u5730\u5740\u7c7b\u4e2d\u53c8\u5305\u542b\u57ce\u5e02\u4fe1\u606fCity\u3002\u5728\u83b7\u53d6\u7528\u6237\u7684\u57ce\u5e02\u540d\u79f0\u65f6\uff0c\u5b58\u5728\u591a\u7ea7\u7a7a\u6307\u9488\u7684\u98ce\u9669\u3002<\/p>\n<pre><code>public class User {\n    private Address address;\n\n    public Address getAddress() {\n        return address;\n    }\n}\n\npublic class Address {\n    private City city;\n\n    public City getCity() {\n        return city;\n    }\n}\n\npublic class City {\n    private String name;\n\n    public String getName() {\n        return name;\n    }\n}<\/code><\/pre>\n<p>\u4f7f\u7528\u4f20\u7edf\u65b9\u5f0f\u5904\u7406NPE<\/p>\n<p>\u5728\u6ca1\u6709\u4f7f\u7528Optional\u7684\u60c5\u51b5\u4e0b\uff0c\u83b7\u53d6\u57ce\u5e02\u540d\u79f0\u53ef\u80fd\u9700\u8981\u591a\u7ea7null\u68c0\u67e5\uff1a<\/p>\n<pre><code>public String getUserCityName(User user) {\n    if (user != null) {\n        Address address = user.getAddress();\n        if (address != null) {\n            City city = address.getCity();\n            if (city != null) {\n                return city.getName();\n            }\n        }\n    }\n    return &quot;Unknown&quot;;\n}<\/code><\/pre>\n<p>\u4e0a\u8ff0\u4ee3\u7801\u5c42\u5c42\u5d4c\u5957\uff0c\u903b\u8f91\u590d\u6742\uff0c\u4e14\u6613\u4e8e\u9057\u6f0f\u67d0\u4e00\u7ea7\u7684null\u68c0\u67e5\u3002\u5e76\u4e14\u4ee3\u7801\u4e5f\u4e0d\u5bb9\u6613\u9605\u8bfb<\/p>\n<p>\u4f7f\u7528Optional\u7b80\u5316\u4ee3\u7801<\/p>\n<p>\u5229\u7528Optional\uff0c\u53ef\u4ee5\u5c06\u591a\u7ea7null\u68c0\u67e5\u8f6c\u5316\u4e3a\u94fe\u5f0f\u8c03\u7528\uff0c\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\u660e\u4e86\uff1a<\/p>\n<pre><code>public String getUserCityName(Optional&lt;User&gt; userOptional) {\n    return userOptional\n            .map(User::getAddress)\n            .map(Address::getCity)\n            .map(City::getName)\n            .orElse(&quot;Unknown&quot;);\n}<\/code><\/pre>\n<p>\u53e6\u4e00\u4e2a\u5b9e\u4f8b\uff1a\u5904\u7406\u65b9\u6cd5\u8fd4\u56de\u503c<\/p>\n<p>\u5047\u8bbe\u6709\u4e00\u4e2a\u65b9\u6cd5findUserById\uff0c\u53ef\u80fd\u8fd4\u56de\u4e00\u4e2aUser\u5bf9\u8c61\u6216null\u3002\u4f7f\u7528Optional\u53ef\u4ee5\u4f18\u96c5\u5730\u5904\u7406\u8fd4\u56de\u503c\u3002<\/p>\n<pre><code>public Optional&lt;User&gt; findUserById(String userId) {\n    User user = userRepository.findById(userId); \/\/ \u53ef\u80fd\u8fd4\u56denull\n    return Optional.ofNullable(user);\n}<\/code><\/pre>\n<p>\u8c03\u7528\u65b9\u53ef\u4ee5\u8fd9\u6837\u4f7f\u7528\uff1a<\/p>\n<pre><code>findUserById(&quot;12345&quot;)\n    .map(User::getAddress)\n    .map(Address::getCity)\n    .map(City::getName)\n    .ifPresent(cityName -&gt; System.out.println(&quot;User city: &quot; + cityName));<\/code><\/pre>\n<p>\u5982\u679cUser\u4e0d\u5b58\u5728\u6216\u5176\u5730\u5740\u3001\u57ce\u5e02\u4fe1\u606f\u4e3anull\uff0c\u4e0a\u8ff0\u4ee3\u7801\u4e0d\u4f1a\u6267\u884cifPresent\u4e2d\u7684\u6253\u5370\u64cd\u4f5c\uff0c\u907f\u514d\u4e86NPE\u7684\u98ce\u9669\u3002<\/p>\n<h2>\u603b\u7ed3<\/h2>\n<p>\u901a\u8fc7\u5408\u7406\u4f7f\u7528Java 8\u7684Optional\u7c7b\uff0c\u6211\u4eec\u5f00\u53d1\u8005\u53ef\u4ee5\u6709\u6548\u51cf\u5c11NullPointerException\u7684\u53d1\u751f\uff0c\u63d0\u9ad8\u4ee3\u7801\u7684\u5065\u58ee\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002\u7136\u800c\uff0cOptional\u5e76\u975e\u4e07\u80fd\uff0c\u9700\u7ed3\u5408\u5177\u4f53\u573a\u666f\u5408\u7406\u4f7f\u7528\u3002\u638c\u63e1Optional\u7684\u4f7f\u7528\u6280\u5de7\u548c\u6700\u4f73\u5b9e\u8df5\uff0c\u5c06\u6709\u52a9\u4e8e\u7f16\u5199\u66f4\u5b89\u5168\u3001\u4f18\u96c5\u7684Java\u4ee3\u7801\uff0c\u771f\u6b63\u505a\u5230\u201c\u5584\u7528Optional\uff0c\u544a\u522bNPE\u201d\u3002<\/p>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000045652349\">https:\/\/segmentfault.com\/a\/1190000045652349<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4f5c\u8005\uff1a\u4eac\u4e1c\u7269\u6d41 \u738b\u4e9a\u5b81 1\u3001NPE\u662f\u4ec0\u4e48\uff1f NPE:NullPointerException\uff08\u7a7a\u6307\u9488\u5f02\u5e38\uff09\u3002\u53ef [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4],"tags":[],"class_list":["post-10020505","post","type-post","status-publish","format-standard","hentry","category-share"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10020505","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10020505"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10020505\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10020505"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10020505"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10020505"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}