Syed
Syed

Reputation: 2607

Check whether list object is null in java

I'm checking whether a List object is null or not using java. But I'm not sure whether it is the optimized way or not.

Here is my code:

List<String> listSCBPLNewErrMsgs= new ArrayList<String>(Arrays.asList(SCBPL_NEW_ERRORMESSAGES.split("\\$\\@")));

The above line itself throws null pointer exception.

if(listSCBPLNewErrMsgs != null) <Right way?>

This will get all the values from the config.

Now, tomorrow if I change the config entry, this should not throw an null pointer exception

Upvotes: 2

Views: 9461

Answers (8)

Naman Gala
Naman Gala

Reputation: 4692

You need to add null check for SCBPL_NEW_ERRORMESSAGES.

if (SCBPL_NEW_ERRORMESSAGES != null && !SCBPL_NEW_ERRORMESSAGES.isEmpty()) {

In your declaration, list can not be null as your are doing new ArrayList<String>. So no need to worry about null pointer exception.


If you wish to check for empty list. Then you can try isEmpty() method.

if (SCBPL_NEW_ERRORMESSAGES != null && !SCBPL_NEW_ERRORMESSAGES.isEmpty()) {
    List<String> listSCBPLNewErrMsgs = new ArrayList<String>(Arrays.asList(SCBPL_NEW_ERRORMESSAGES.split("\\$\\@")));
    if (!listSCBPLNewErrMsgs.isEmpty()) {
        // Do something.
    }
}

Upvotes: 0

Robin-Hoodie
Robin-Hoodie

Reputation: 4974

If SCBPL_NEW_ERRORMESSAGES is nulll that will throw a NPE exception indeed since you will be using the split method on null.

You can first check if that's not null:

if (SCBPL_NEW_ERRORMESSAGES != null) {
      //Instantiate list
      //Optional isEmpty check
}

You will first check if SCBPL_NEW_ERRORMESSAGES is not null

Then you can instantiate your list and perform an optional isEmpty check on the new list.

Upvotes: 1

Asha Soman
Asha Soman

Reputation: 1856

First you have to check SCBPL_NEW_ERRORMESSAGES is null or empty

if(!TextUtils.isEmpty(SCBPL_NEW_ERRORMESSAGES))

You have to check both whether the list is null/empty or not. So I prefer

if(listSCBPLNewErrMsgs != null && !listSCBPLNewErrMsgs.isEmpty()) {
}

Upvotes: 0

Naresh kumar
Naresh kumar

Reputation: 1069

if(SCBPL_NEW_ERRORMESSAGES != null)
  List<String> listSCBPLNewErrMsgs= new ArrayList<String>Arrays.asList(SCBPL_NEW_ERRORMESSAGES.split("\\$\\@")));

No Need of listSCBPLNewErrMsgs != null as everyone said

Upvotes: 0

npinti
npinti

Reputation: 52185

If SCBPL_NEW_ERRORMESSAGES is null that code will still fail.

Assuming that SCBPL_NEW_ERRORMESSAGES has some value or is empty, the split will return an array of size 0 or more. Changing it to a list from an array will yield either an array with 0 or more elements.

Lastly, the copy constructor will copy the content and assign it to a new list. In all scenarios, unless there is a null pointer on SCBPL_NEW_ERRORMESSAGES, the returned list (listSCBPLNewErrMsgs) will never be null, at most it will be empty, which can be checked with the isEmpty() method call.

As per your comment, if you are getting a null pointer on that line, it should be due to the fact that SCBPL_NEW_ERRORMESSAGES is null.

Try this:

List<String> listSCBPLNewErrMsgs = null;
if(SCBPL_NEW_ERRORMESSAGES != null) {
    listSCBPLNewErrMsgs= new ArrayList<String>(Arrays.asList(SCBPL_NEW_ERRORMESSAGES.split("\\$\\@")));
}
else {
    listSCBPLNewErrMsgs = new ArrayList<>();
}

Upvotes: 1

Mureinik
Mureinik

Reputation: 311308

The new operator in Java can never return null. Neither can String#split.

What you may want to check, however, is that the list is not empty:

if (listSCBPLNewErrMsgs.isEmpty()) {
    // do something
}

Upvotes: 3

DSF
DSF

Reputation: 804

From the looks if your code your listSCBPLNewErrMsgs object won't be null. Test if it is empty using the listSCBPLNewErrMsgs.isEmpty();

Upvotes: 1

novy1234
novy1234

Reputation: 332

If you want to check whether it is null it is right way (even though it will never be null), however if you simply want to check if list is empty then you should use isEmpty() method:

if(listSCBPLNewErrMsgs.isEmpty()) {/**/}

Upvotes: 1

Related Questions