{"id":2459,"date":"2025-12-23T10:00:04","date_gmt":"2025-12-23T15:00:04","guid":{"rendered":"http:\/\/www.mymiller.name\/wordpress\/?p=2459"},"modified":"2025-12-23T10:00:04","modified_gmt":"2025-12-23T15:00:04","slug":"not-returning-null","status":"publish","type":"post","link":"https:\/\/www.mymiller.name\/wordpress\/java_tips\/not-returning-null\/","title":{"rendered":"Not Returning Null"},"content":{"rendered":"\n<p>We all have done it, in fact, I&#8217;m in the process of changing my APIs over to not do.\u00a0 Returning NULL for a return type.\u00a0 After doing some work with streams, I see that return NULL often adds additional complexity to the code.\u00a0 In this article, we are going to discuss what to return besides NULL.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Empty Collection v&#8217;s NULL<\/h2>\n\n\n\n<p>If your method returns a NULL for when there are no objects in the list, or on an error condition, your calling code is going to have to look like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>List&lt;PersonDTO> allPeople = this.getAllPeople();\n\t\t\n\t\tif(allPeople != null) {\n\t\t\tallPeople = allPeople.stream().filter(person -> person.getLastName().equals(\"Ghost\")).collect(Collectors.toList());\n\t\t}<\/code><\/pre>\n\n\n\n<p>Now if getAllPeople() was guaranteed to not return NULL you could do this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>List&lt;PersonDTO> allPeople = this.getAllPeople().stream().filter(person -> person.getLastName().equals(\"Ghost\")).collect(Collectors.toList());<\/code><\/pre>\n\n\n\n<p>\u00a0It&#8217;s a simple change anywhere that you were doing a &#8220;return null;&#8221;\u00a0 change to &#8220;return Collections.emptyList();&#8221; think about how much easier your code would be to write.<\/p>\n\n\n\n<table class=\"wp-block-table is-style-stripes\"><tbody><tr><td><strong>Type of Collection<\/strong><\/td><td><strong>Collections Method<\/strong><\/td><\/tr><tr><td>java.util.Enumeration&lt;E><\/td><td>Collections.emptyEnumeration()<\/td><\/tr><tr><td>java.util.Iterator&lt;E><\/td><td>Collections.emptyIterator()<\/td><\/tr><tr><td>java.util.List&lt;E><\/td><td>Collections.emptyList()<\/td><\/tr><tr><td>java.util.ListIterator&lt;E><\/td><td>Collections.emptyListIterator()<\/td><\/tr><tr><td>java.util.Map&lt;K.V><\/td><td>Collections.emptyMap()<\/td><\/tr><tr><td>java.util.NavigableMap&lt;K,V><\/td><td>Collections.emptyNavigableMap()<\/td><\/tr><tr><td>java.util.NavigableSet&lt;E><\/td><td>Collections.emptyNavigableSet()<\/td><\/tr><tr><td>java.util.Set&lt;E><\/td><td>Collections.emptySet()<\/td><\/tr><tr><td>java.util.SortedMap&lt;K,V><\/td><td>Collections.emptySortedMap()<\/td><\/tr><tr><td>java.util.SortedSet&lt;E><\/td><td>Collections.emptySortedSet()<\/td><\/tr><\/tbody><\/table>\n\n\n\n<h2 class=\"wp-block-heading\">Empty Stream v&#8217;s NULL<\/h2>\n\n\n\n<p>While I am not a fan of returning a Stream from a method.\u00a0 The above example works just as well if your returning a stream.\u00a0 To return an empty stream use &#8220;Stream.emtpy()&#8221;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Empty Arrays v&#8217;s NULL<\/h2>\n\n\n\n<p>Again having an object instead of having to check for NULL, is the desired outcome.\u00a0 However at the same time you do not want to be wasteful with memory.\u00a0 When it comes to returning an Empty Array one of the best solutions is to use a constant:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>private static final PersonDTO[] NO_PEOPLE = {};\n\t\npublic PersonDTO[] getAllPeople() {\t\t\t\t\n\treturn PersonDTO.NO_PEOPLE;\n}<\/code><\/pre>\n\n\n\n<p>Your not needing to create an empty array each time, it&#8217;s reusable across all of your code.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Single Object v&#8217;s NULL<\/h2>\n\n\n\n<p>Now then this is when things get fun.\u00a0 What do you do when your returning a single object?\u00a0 When it is not an Array, Stream, List, Map, Iterator, or Enumeration.\u00a0 Well often times we return objects on our method calls.\u00a0 This doesn&#8217;t have to change, but if your method is one that could return NULL, then maybe you should consider returning an &#8220;Optional&#8221;.\u00a0 Now with Optional you can return &#8220;Optional.empty()&#8221; instead of a NULL.\u00a0 In cases where you are returning your Object, you use &#8220;Optional.of(yourObject)&#8221;.<\/p>\n\n\n\n<p>This provides a built in portion of the NULL Pointer check for the user of your method call.\u00a0 A simple call to the &#8220;isPresent()&#8221; method call will tell us if we have a value or not.\u00a0 This makes the code far easier to read.\u00a0<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What if you have to return NULL?<\/h2>\n\n\n\n<p>Sometimes there isn&#8217;t a way around it.\u00a0 Either it&#8217;s legacy code and you have to maintain the code in that form, or some other reason you deem you must return a NULL.\u00a0 Well take a small step to help the user of your method to know if may return a NULL.\u00a0 Use the &#8220;@Nullable&#8221; annotation to signal to the user this method call can return a NULL.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>We all have done it, in fact, I&#8217;m in the process of changing my APIs over to not do.\u00a0 Returning NULL for a return type.\u00a0 After doing some work with streams, I see that return NULL often adds additional complexity to the code.\u00a0 In this article, we are going to discuss what to return besides [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":2463,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_coblocks_attr":"","_coblocks_dimensions":"","_coblocks_responsive_height":"","_coblocks_accordion_ie_support":"","jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[452],"tags":[69],"series":[],"class_list":["post-2459","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java_tips","tag-java-2"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2018\/11\/paper-3204064_640.jpg?fit=640%2C372&ssl=1","jetpack-related-posts":[{"id":2233,"url":"https:\/\/www.mymiller.name\/wordpress\/java_extra\/javas-missing-treemap\/","url_meta":{"origin":2459,"position":0},"title":"Java&#8217;s missing TreeMap","author":"Jeffery Miller","date":"December 18, 2025","format":false,"excerpt":"One thing I have always found lacking in Java collections\/containers is TreeMap. A simple class that takes a hierarchical approach to mapping data to a node.\u00a0 Given a hierarchical string like \"\/java\/myapp\/javafx\/config\/phone\" I can set the various objects relative to this on that node. Java Preferences() class supports this hierarchical\u2026","rel":"","context":"In &quot;Java Extras&quot;","block_context":{"text":"Java Extras","link":"https:\/\/www.mymiller.name\/wordpress\/category\/java_extra\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2018\/04\/tree-3097419_640.jpg?fit=640%2C360&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2018\/04\/tree-3097419_640.jpg?fit=640%2C360&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2018\/04\/tree-3097419_640.jpg?fit=640%2C360&ssl=1&resize=525%2C300 1.5x"},"classes":[]},{"id":2711,"url":"https:\/\/www.mymiller.name\/wordpress\/lists\/listutils-size\/","url_meta":{"origin":2459,"position":1},"title":"ListUtils.size()","author":"Jeffery Miller","date":"December 23, 2025","format":false,"excerpt":"Simple yet handy method for your ListUtils class. Handle if null, and return the number of elements in the list. Effective and simple.","rel":"","context":"In &quot;Lists&quot;","block_context":{"text":"Lists","link":"https:\/\/www.mymiller.name\/wordpress\/category\/lists\/"},"img":{"alt_text":"List size","src":"https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2020\/04\/number-703136_640.jpg?fit=640%2C426&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2020\/04\/number-703136_640.jpg?fit=640%2C426&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2020\/04\/number-703136_640.jpg?fit=640%2C426&ssl=1&resize=525%2C300 1.5x"},"classes":[]},{"id":2736,"url":"https:\/\/www.mymiller.name\/wordpress\/lists\/listutils-safe\/","url_meta":{"origin":2459,"position":2},"title":"ListUtils.safe()","author":"Jeffery Miller","date":"December 24, 2025","format":false,"excerpt":"Sometimes you need to guarantee a list is safe to use. A simple method can make sure your safe to use the list, or have an empty list that would be safe for use. \/** * Guarantees a list will be safe to use, even if null. * @param list\u2026","rel":"","context":"In &quot;Lists&quot;","block_context":{"text":"Lists","link":"https:\/\/www.mymiller.name\/wordpress\/category\/lists\/"},"img":{"alt_text":"Keeping Containers Safe","src":"https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2020\/04\/rappelling-755399_640.jpg?fit=640%2C480&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2020\/04\/rappelling-755399_640.jpg?fit=640%2C480&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2020\/04\/rappelling-755399_640.jpg?fit=640%2C480&ssl=1&resize=525%2C300 1.5x"},"classes":[]},{"id":3357,"url":"https:\/\/www.mymiller.name\/wordpress\/java_new_features\/nullpointerexcepiton-enhancements\/","url_meta":{"origin":2459,"position":3},"title":"NullPointerExcepiton enhancements","author":"Jeffery Miller","date":"December 24, 2025","format":false,"excerpt":"The NullPointerException is a common exception in Java that occurs when a null reference is dereferenced. This exception can make debugging difficult, as it does not provide any information about which variable was null, making it harder to pinpoint the cause of the issue. However, starting from Java 14, a\u2026","rel":"","context":"In &quot;Java New Features&quot;","block_context":{"text":"Java New Features","link":"https:\/\/www.mymiller.name\/wordpress\/category\/java_new_features\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2023\/06\/paper-g580d50d6b_640-1.jpg?fit=640%2C453&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2023\/06\/paper-g580d50d6b_640-1.jpg?fit=640%2C453&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2023\/06\/paper-g580d50d6b_640-1.jpg?fit=640%2C453&ssl=1&resize=525%2C300 1.5x"},"classes":[]},{"id":2391,"url":"https:\/\/www.mymiller.name\/wordpress\/java_tips\/java-development-tips\/","url_meta":{"origin":2459,"position":4},"title":"Java Development Tips","author":"Jeffery Miller","date":"December 23, 2025","format":false,"excerpt":"Java-based servers or applications often have to deal with large amounts of data.\u00a0 Whether the data is from a database, or from a local file, processing this data in an efficient manner is a priority for maintainability. In this article, we will discuss the types of Java Data Objects and\u2026","rel":"","context":"In &quot;Java Tips&quot;","block_context":{"text":"Java Tips","link":"https:\/\/www.mymiller.name\/wordpress\/category\/java_tips\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2018\/10\/archive-1850170_640.jpg?fit=640%2C426&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2018\/10\/archive-1850170_640.jpg?fit=640%2C426&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2018\/10\/archive-1850170_640.jpg?fit=640%2C426&ssl=1&resize=525%2C300 1.5x"},"classes":[]},{"id":3179,"url":"https:\/\/www.mymiller.name\/wordpress\/java_extra\/understanding-json-data-processing-with-java-exploring-the-jsonfieldprocessor-class\/","url_meta":{"origin":2459,"position":5},"title":"Understanding JSON Data Processing with Java: Exploring the JsonFieldProcessor Class","author":"Jeffery Miller","date":"January 15, 2026","format":false,"excerpt":"In today's digital era, data comes in various formats, with JSON (JavaScript Object Notation) being one of the most popular for representing structured data. Manipulating and processing JSON data efficiently is crucial for many software applications, from web development to data analysis. In this article, we'll delve into the workings\u2026","rel":"","context":"In &quot;Java Extras&quot;","block_context":{"text":"Java Extras","link":"https:\/\/www.mymiller.name\/wordpress\/category\/java_extra\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/04\/data-7798787_640.png?fit=640%2C640&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/04\/data-7798787_640.png?fit=640%2C640&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/04\/data-7798787_640.png?fit=640%2C640&ssl=1&resize=525%2C300 1.5x"},"classes":[]}],"jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/posts\/2459","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/comments?post=2459"}],"version-history":[{"count":3,"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/posts\/2459\/revisions"}],"predecessor-version":[{"id":2462,"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/posts\/2459\/revisions\/2462"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/media\/2463"}],"wp:attachment":[{"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/media?parent=2459"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/categories?post=2459"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/tags?post=2459"},{"taxonomy":"series","embeddable":true,"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/series?post=2459"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}