• 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
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

Null and isEmpty Check for ArrayLists

 
Ranch Hand
Posts: 571
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How to do null check and also isEmpty check on a arraylist. Any best practices around it.

I have if  then else if then else if loops

if listA not null and not empty also listB not null and not empty i have to call database twice (first time listA data, second time using listB data). Then merge both results using addAll

In first else if listA has data whereas listB is null or empty call only listA specific database call using that stored procedure

in second else if listB has data whereas listA is null or empty call only listA specific database call using that stored procedure

For me with null flows are working while passing blank list [] not working.

How to check both null and isEmpty same time?
if list null and if i check isEmpty giving null pointer exception.
 
Saloon Keeper
Posts: 8738
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Yes, you have to test both and in this order.
 
Carey Brown
Saloon Keeper
Posts: 8738
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You could make a method that does both.
 
Marshal
Posts: 74353
334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Would you make that a static method, Carey? It looks like a 1368 in the most dubious classification of methods known to modern science, and it might be a utility method.
Do you need to use @SuppressWarnings? I wrote such a method on JShell and it didn't give any warnings about type safety. I presume that is because I didn't do anything with any of the elements of the List.
 
Saloon Keeper
Posts: 24514
167
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually, I recommend that wherever possible, you never set a List (Collection) reference variable to null. If you have no data, make it an empty collection. Otherwise you can zap yourself unexpectedly in that one place you forgot to check for null first (plus, being lazy/sloppy, it's that much more logic to mis-code).

Java does have an "optional" facility these days as well, although it post-dates my daily coding era so I'm not very familiar with it. We do have some fans of it here on the Ranch, though.
 
Campbell Ritchie
Marshal
Posts: 74353
334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But Optional isn't a complete replacement for null.
Agree it is a bad idea to set just about anything to null. There are a few exceptions; see Joshua Bloch's example of creating a memory leak by not null‑ing elements in a stack (Effective Java 2nd edition page 24, 3rd edition page 26).

OP: What is wrong with using an empty List? If you use addAll(listX) and listX is empty, then nothing will happen and the method call will complete without problems.
 
Tim Holloway
Saloon Keeper
Posts: 24514
167
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is, actually a specific case where I do set collection properties to null and that's where I have cascading menus on a webpage. Setting the menu collection to null is how I trigger a load of a new menu, but it's tied to specific events (specifically the next request of the menu) so you won't find null in random situations.

On the other hand, ORM collections really should never be null. I think you can give the ORM indigestion, in fact.
 
sai rama krishna
Ranch Hand
Posts: 571
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I see other part of the code developers used CollectionUtils.isEmpty() . Is that is good choice?
 
Carey Brown
Saloon Keeper
Posts: 8738
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

sai rama krishna wrote:For me with null flows are working while passing blank list [] not working.

How to check both null and isEmpty same time?
if list null and if i check isEmpty giving null pointer exception.

I think it's time you post your code. You seem to be struggling with logic that others find straight forward.
 
sai rama krishna
Ranch Hand
Posts: 571
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Above is service call which calls DAO method which eventually call stored procedure. This code works as long as i am testing with ListNormal and ListSpecial by passing  regular valid string data data as null

But when i pass either ListNormal or ListSpacial as blank list like [] i am getting errors.

I am trying to see how to fix these issues
 
Carey Brown
Saloon Keeper
Posts: 8738
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have several bugs in there and I don't think it will even compile.
 
Saloon Keeper
Posts: 1612
52
Eclipse IDE Postgres Database C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Would you make that a static method, Carey? It looks like a 1368 in the most dubious classification of methods known to modern science, and it might be a utility method.



That's one of the funniest classifications I have seen, and looks pretty handy and useful.

I have been thinking about the Religious Wars about static methods in general, having come from a background where static methods were loved, enjoyed, or at least tolerated without blinking an eye.

I think there are two cases that raise the Code Smells around static:
1. static methods that manipulate static data: writable static data is writable global data with fake glasses nose and mustache over it, and is despised for good reason.
2. In Java, static methods can not be overridden (we talked to death about how it is wrong to say they aren't inherited, because they are, but can't be overridden).  Anything that can't be overridden causes a Big Hassle when trying to Mock for Isolation in Testing.

I think utility methods that act solely on their parameters as pure functions to return a result, that we are sure we don't need to mock in testing, are still fine even now.  I think a lot of code that was "Difficult to Retro-Fit Proper Testing For" was lousy with static methods that shouldn't, in light of that need, have been static in the first place.  For that matter, maybe we should go a bit further and add a third?

3. In Java, static methods can not be overridden.  Anything that could plausibly need to be overridden by a sub-class (see: "Why not casually make all my classes final?") should NOT be made static in Java.

With my heightened sensitivity to this issue, I have seen many multi-lingual developers explain why static methods are bad in Java, despite them not having an issue with them in other languages they use which apparently allow you to override static methods polymorphically in sub-classes, or at least the equivalent.

I would feel this was thread-hijacking, but the thread went on rather long for a simple question, and then we started talking about "When are static methods okay in Java?" which I'd been thinking about a lot lately, so there it is.

Countdown to Campbell quoting Wisdom from Joshua Bloch on when it is appropriate to use static methods (from Effective Java, 1st, 2nd and 3rd editions) in five...
 
Carey Brown
Saloon Keeper
Posts: 8738
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need to seriously clean that up.
There's no operator "!==".
"return" statement missing semicolon.
Indentation needs to be re-done.
Variable names MUST begin with a lower case letter (not "ListNormal").
You have IF-ELSE-IFs with the same boolean expressions so only the first one will be true.
 
sai rama krishna
Ranch Hand
Posts: 571
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
when i pass ListNormal valid data, ListSpecial as [] how it is going into if loop. I expected it to go in second/last else if loop.

when i pass ListNormal valid data,  ListSpecial  as null how it is going into first else if loop  which it should not either.

I am confused

 
sai rama krishna
Ranch Hand
Posts: 571
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Sorry for typo earlier. I corrected pseudo code above. Code is compiling fine in eclipse editor
 
Carey Brown
Saloon Keeper
Posts: 8738
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't see you calling isEmpty() anywhere.
 
Carey Brown
Saloon Keeper
Posts: 8738
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

sai rama krishna wrote:when i pass ListNormal valid data, ListSpecial as [] how it is going into if loop. I expected it to go in second/last else if loop.

when i pass ListNormal valid data,  ListSpecial  as null how it is going into first else if loop  which it should not either.

I am confused


First step to clearing up confusion starts with code clean up.

As I said, your variable should not be named "ListNormal".

And what data type is that? You haven't show us.
 
Carey Brown
Saloon Keeper
Posts: 8738
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you have something like this

Only do1() will ever be called IF A is true.
 
Carey Brown
Saloon Keeper
Posts: 8738
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

sai rama krishna wrote:Sorry for typo earlier. I corrected pseudo code above. Code is compiling fine in eclipse editor


This is pseudo code? Why aren't you giving us the code you say is running?
 
Jesse Silverman
Saloon Keeper
Posts: 1612
52
Eclipse IDE Postgres Database C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You still had a remaining:
ListSpecial!==null

which will not compile.

I also agree that it is a bad idea in Java to have anything but the names of Classes, Interfaces, Enums (and ENUM values) @Annotations and maybe CONSTANT_VALUES to be capitalized in Java, it is legal but against convention and confusing.

Other languages like C# like to capitalize more things, even there you won't normally find reference variables to be Capitalized, only Properties and Method Names.

Limit the usage of Capitalized Identifiers to the kinds mentioned above to make your code look more like natural Java...
 
sai rama krishna
Ranch Hand
Posts: 571
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If both lists have valid list of string values i expected to go in if loop
if only ListSpecial is valid list of string values i expected to go in first else if
if only ListNormal is valid list of string values i expected to go in second else if

This is kind of pseudo code. Please ignore naming convention
 
sai rama krishna
Ranch Hand
Posts: 571
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried CollectionUtils.isEmpty() and also with normal list.isEmpty() but having issues still
 
sai rama krishna
Ranch Hand
Posts: 571
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Trying as above
 
sai rama krishna
Ranch Hand
Posts: 571
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I corrected the psuedo code above typo as well
 
Marshal
Posts: 26909
82
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I thought you said you were having "issues" before. Was that code where the "issues" occurred? And if so, what were they?

From my point of view the issue is that you have way too many nulls flying around in that code. It looks to me like "new InvoiceData()" produces a new empty InvoiceData object, and if so, there's no excuse for returning null instead of one of those empty objects.

And perhaps all of the nulls led to this line of code:



But anyway, if you would like to discuss the problem, then starting out by telling us what the problem is, that would be the way to start.
 
Tim Holloway
Saloon Keeper
Posts: 24514
167
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jesse Silverman wrote:
I also agree that it is a bad idea in Java to have anything but the names of Classes, Interfaces, Enums (and ENUM values) @Annotations and maybe CONSTANT_VALUES to be capitalized in Java, it is legal but against convention and confusing.



It can also give many Java tools and resources heartburn. It's better if you consider it to be linguistically legal (compilable) but functionally illegal (may cause run-time problems).

I think that that code example is an excellent argument for why I don't recommend setting collection references to null. ¡Ay, Caramba! ¡Does it ever make things more complicated, unreadable, and unreliable!

Just as a further note - I try to avoid method names that start with "get" unless they are actually property-get methods. It's likely to cause people to make false assumptions. So I try and make stuff like this be named "fetch..", "obtain...", "pillage..." or something like that.
 
Jesse Silverman
Saloon Keeper
Posts: 1612
52
Eclipse IDE Postgres Database C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Stephan and Junilu don't seem to be around right now, so I will channel my inner....them and ask:

Is it possible that either thru simple sub-classing or one of various Design Patterns we can reduce the complexity of this code?

It feels like there are several different ways to implement these different cases that would obviate the minefield of if else else if if else that we have here.

I programmed in C for years, and FORTRAN before that, and we just dealt with code that looked like this and worked on it until everything worked right.

I think here in Java we can do better.  The reason that it is so difficult to get this one method in this one class right is probably because it shouldn't be one method.  We would need to see more context than just this problematic method in isolation.

I haven't had dinner yet, so I am out -- but I wanted to mention this possibility.
 
Tim Holloway
Saloon Keeper
Posts: 24514
167
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, I looked a little more closely at the example and realized that there is some context missing (and I hope it's not static variables!)

Almost certainly this can be simplified. But this is a classic example of "my solution doesn't work - how to fix?" when I think we need to step back and ask "What are we actually trying to do here?" Knowing that, we can probably arrive at a more elegant (and reliable) solution.
 
Paul Clapham
Marshal
Posts: 26909
82
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I believe that it could be simplified to this, provided that those getInvoiceBatchBlahBlah() methods were written to return empty sets instead of null values:

 
sai rama krishna
Ranch Hand
Posts: 571
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry there is type in line 11. I just corrected as below

 
Paul Clapham
Marshal
Posts: 26909
82
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Would you like to comment on these lines of code as well?

 
sai rama krishna
Ranch Hand
Posts: 571
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Sorry one other typo. I just corrected it. If input request field has normal data assign to that to invoiceData and return it. If input request field has special data assign to invoceData and return. If input request has both normal data and special data merge both and return it as invoiceData
 
sai rama krishna
Ranch Hand
Posts: 571
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have validator class trying to create a new method. New method does following thing

If a list was empty i could set it to null to avoid changes in this service class method.

General expectation or approach  is always handle empty as null for list data?
 
sai rama krishna
Ranch Hand
Posts: 571
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
bit confused when to use && and when to use || in this type of object data even though i know these short cut AND and OR operators

 
Paul Clapham
Marshal
Posts: 26909
82
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

sai rama krishna wrote:If input request field has normal data assign to that to invoiceData and return it.



That was the code fragment I just asked you to comment on.  To me it looks like it assigns the data to invoiceSpecial and then returns invoiceData. Is that not how it looks to you?
 
Paul Clapham
Marshal
Posts: 26909
82
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

sai rama krishna wrote:General expectation or approach  is always handle empty as null for list data?



My expectation would be to handle empty data as empty data. I don't see any reason to treat that as a special case. Two invoices, zero invoices, six hundred invoices... those are all numbers of invoices. Treating zero as not a number just leads to confusion.
 
sai rama krishna
Ranch Hand
Posts: 571
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


I fixed one other typo relating to database method calls
So empty means treat as null and fail the test that is the goal i am trying to achieve in most effective manner
 
Paul Clapham
Marshal
Posts: 26909
82
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Then perhaps you could start by explaining what that method is supposed to do.

I'm not asking for an explanation of the code. I'm asking about the purpose of the method. What are the inputs? What are the outputs? What is supposed to be returned given what combination of inputs?
 
sai rama krishna
Ranch Hand
Posts: 571
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



Add three new conditions for blank check similar to above 3 conditions for null check on list object is a good idea?

All i need to check Null as well as blanks

If list blank treat like null itself
 
Tim Holloway
Saloon Keeper
Posts: 24514
167
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Again.

We're not asking for code. We're asking for purpose..

What actually is a "ListNormal" and a "ListSpecial" and where are they kept/how are they managed, in general (NOT programming) terms?

What actually comes back from the database calls?

We're looking to see the forest and you're giving us trees.
 
reply
    Bookmark Topic Watch Topic
  • New Topic