



1.) Treat Null as a valid response

2.) Treat Null as an invalid response



Life is very easy for the people who go with the second approach. Taking null as an invalid response you won't be getting a null reference calling any of your methods or API. So where there is a situation where null is to be handled, throw InvalidArgumentException or try assert. Assert throws an exception whenever the condition given to it is false. And in case you are using collections always return an empty collection instead of null reference





But most of you will disagree going with the second approach since most of us have been using the first approach from the very start. You have been using APIs where you don't have the control over the code. Now with the first case you never know where you are going to meet with null reference. So you have nothing left except a valid check for the null reference.





Solutions to Valid Null Checks

Depending upon what kind of objects you are checking you may be able to use some of the classes in the apache commons such as: apache commons lang and apache commons collections



Example:



String name;

...

if( StringUtils.isBlank( name) ) {

///do something

}



or (depending on what you need to check):



String name;

...

if( StringUtils.isEmpty( name) ) {

///do something

}



The StringUtils class is only one of many; there are quite a few good classes in the commons that do null safe manipulation.

If you switch to JetBrains Idea, a Java ide, you can use some annotations developed by them.

Basically, you've got:

@Nullable and

@NotNull You can use these annotations in method and parameters, like this:

@NotNull public String getName() { return "Saurab Parakh"; } When you use the first getName() function in this way:

public static void main(String[] args) { if(getName() != null) { System.out.println(getName()); } } Now the JetBrains idea compiler will tell you that the null check is useless, since the getName() will never return null. Using parameter

void someMethod(@NotNull someParameter) { } if you write something like:

someMethod(null); This won't compile.



Other Proposed Solutions from Java 7



The original idea comes from groovy. It was proposed for Java 7 as part of Project Coin: hasn't been accepted yet. The original idea comes from groovy. It was proposed for Java 7 as part of Project Coin: https://wikis.oracle.com/display/ProjectCoin/2009ProposalsTOC , (Elvis and Other Null-Safe Operators) but

Null Safe Operators Elvis or other null safe operators to get every NPE handled.

i) Elvis Operator It results to the value on the left hand side if not null else right hand side is evaluted.

Integer a = getInteger(); // may be null int i = a ?: -1; // no NPE from unboxing Example: So here the value of a will be assigned to i only if a is not equal to null.

ii) Null Safe operator It is similar to member selection dot operator(.). Just a valid null check is added to its definition.

class Student { private Parent parent; // may be null public Parent getParent() { return this.parent; } } class Parent { String name; // may be null private Address address; // may be null public Address getAddress () { return this.address; } } class Address { private String locaton; private private String street; String houseNumber; public String getLocation () { return this.location; } }

So without this feature you would have written something like this

String location= null; if (student != null && student.getParent() != null && student.getParent().getLocation != null) { location= student.getParent().getLocation(); } else { location= "unknown"; }

But with Java's Null safe operator your code can be eased down to something like this.

String location = student?.getParent()?.getLocation() ?: "unknown";

Example:So without this feature you would have written something like thisBut with Java's Null safe operator your code can be eased down to something like this.



Advantages Since the null checks get automatically handled with the use of these operators so now one can focus more on the business logic.

Code size and complexity gets reduced Disadvantages It encourages rather than discouraging the use of null reference in the APIs.

So how did you find the post? Please share your reviews about the post.

Submit this post on reddit

So what are the major benefits and disadvantages of these features.But as we know this feature is not accepted yet, we need to stick to other solutions available as discussed in post above.So how did you find the post? Please share your reviews about the post.

The most common exception that you meet coding in java whether you are a junior or an Intermediate level programmer. Before coming to the solution proposed and added in the new Java update, I would like to discuss the strategies that most of us should follow while handling this common but peculiar exception. There are two approaches that people generally follow.