• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Do we have any way to check null value and empty strings all the time ?

 
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

I know that null value check and Empty string value check is mandatory for us being a programmer.
I also know that we have null value check and Empty string check methods inside JAVA already.

But i want to know we need to make program all the time to check for null values and empty strings manually.
This is increasing the number of lines of program as well. So do we have any way to get rid of this ?

Let say by example:


Here in above code, i need to check for Empty String all time manually and also i need to write code for null value check before empty strings checking.
So, i am writing same code again and again for that purpose specially.

Do we have any way, please suggest me ?
 
Marshal
Posts: 76394
364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't you have a method that tests for nullity? Remember the zero‑length String is different from null. Is there any feature of an Optional<String> that you could use? Look at this article by Urma.
 
Author
Posts: 161
31
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Why not write a program to do the job, and then simply call this program with the variable arguments? Here is an example. The Consumer<String> process is your actual program. The rest is just testing environment:

 
SunilK Chauhan
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes,

Means in programming we need to check null values every time to save program from the null exception while execution.

That's why i asked this question because this number of null checking increase the line numbers of program than the basic business logic to implement.
 
Campbell Ritchie
Marshal
Posts: 76394
364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

SunilK Chauhan wrote:. . .  we need to check null values every time . . .

It might be better to ensure that nulls don't get into your program in the first place.
 
SunilK Chauhan
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ya it is the right approach. But still we need to check for the values because it may happen while we are working on such system which database is modifying most of the time by number of external systems.
 
Campbell Ritchie
Marshal
Posts: 76394
364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I suggest it might be a good idea to monitor places where nulls enter your program. If they come in, treat them like a wasp trying to eat your dinner:-
  • 1: Kill it straight off. Or throw a NullPointerException as soon as the null appears.
  • 2: Put a glass over it. You can hide nulls away safely in some sorts of data structure. An Optional<T> object may be a good place to hide it. Keep it there where it can't do any harm.
  • 3: At least keep an eye on it. There are some data structures, e.g. trees, singly linked lists, where nulls are unavoidable. At least be aware of the possibility; this is where null tests are a good idea.
  • 4: Let it sting you. Rest assured, it will find somewhere where it really hurts
  • There are some data structures which can be used to avoid nulls. For example, if you have a Person class you can design it like this:-...or like this:-If I go into the first class, what are you going to do about the middle name I haven't got? Mark it null? But in the second version, you can have an array with all middle names in; in my case you can use a zero‑length array. Abracadabra! No nulls More details in books like Effective Java by Joshua Bloch.
     
    Greenhorn
    Posts: 6
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    you can use
     
    Evacuate the building! Here, take this tiny ad with you:
    the value of filler advertising in 2021
    https://coderanch.com/t/730886/filler-advertising
    reply
      Bookmark Topic Watch Topic
    • New Topic