• Post Reply Bookmark Topic Watch Topic
  • New Topic

Null ArrayList returned  RSS feed

 
Christopher Adams
Greenhorn
Posts: 17
Chrome Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In my FirebaseHelper.java file I Log.i the arrayList but it shows empty before it is returned.
Can someone point me in the right direction as to why?
This is an android app I'm building but I do not think this is an Android thing but more of something I'm missing with ArrayLists and Java.
Or how my POJO Exercises class works.

The relevant parts of my code:
Today.java:


FirebaseHelper.java:


Exercise.java:



 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just one quick note: Your declared types are all using the implementation type, ArrayList<...>; you should use the interface type, List<...> instead.

So, instead of this:

you'd write this:

See this example: https://github.com/googlesamples/androidtv-sample-inputs/blob/master/app/src/main/java/com/example/android/sampletvinput/SampleJobService.java

The principle here is "Program to abstractions, not concretions"
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Christopher Adams wrote:In my FirebaseHelper.java file I Log.i the arrayList but it shows empty before it is returned.


There's a difference between a null reference to a List and an empty List.  If you try to log a reference to a List that is null, chances are, the logs will show "null" and if the list is empty, the logs will show something like "[]" or something other than "null".  What are you seeing in the log exactly?

EDIT: I think I can answer that. Since you're calling the .toString() method on the list reference, it's printing out empty. If the reference were null you'd get a NullPointerException
 
Christopher Adams
Greenhorn
Posts: 17
Chrome Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:
Christopher Adams wrote:In my FirebaseHelper.java file I Log.i the arrayList but it shows empty before it is returned.


There's a difference between a null reference to a List and an empty List.  If you try to log a reference to a List that is null, chances are, the logs will show "null" and if the list is empty, the logs will show something like "[]" or something other than "null".  What are you seeing in the log exactly?


Yes, after I made the subject I realized that. I meant to say "Empty" not null.

I've attached the logcat image screenshot to this message.

logcat.PNG
[Thumbnail for logcat.PNG]
 
Christopher Adams
Greenhorn
Posts: 17
Chrome Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:Just one quick note: Your declared types are all using the implementation type, ArrayList<...>; you should use the interface type, List<...> instead.

So, instead of this:

you'd write this:

See this example: https://github.com/googlesamples/androidtv-sample-inputs/blob/master/app/src/main/java/com/example/android/sampletvinput/SampleJobService.java

The principle here is "Program to abstractions, not concretions"


Thank you for information on that. I changed the code but that did not make any difference.
Thanks,
Chris
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On line 38 in the retrieve() method, you call db.addChildEventListener().  I don't know if you were thinking it was, but that doesn't actually make it retrieve any data. An event needs to trigger an update so that the onChildAdded and onChildChanged methods on that listener are called. Then you can probably call this retrieve() method.
 
Christopher Adams
Greenhorn
Posts: 17
Chrome Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:On line 38 in the retrieve() method, you call db.addChildEventListener().  I don't know if you were thinking it was, but that doesn't actually make it retrieve any data. An event needs to trigger an update so that the onChildAdded and onChildChanged methods on that listener are called. Then you can probably call this retrieve() method.


I'm using Firebase for my database. onChildAdded and onChildChanged are supposed to be called whenever the database is changed and the first time the program loads.
https://firebase.google.com/docs/database/android/read-and-write
 
Paul Clapham
Sheriff
Posts: 22841
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So you've got an empty list. That either means that nothing was ever added to the list, or that the list was emptied at some point.

My money's on the "nothing was ever added to the list" option. That, after all, is what the retrieve() method appears to do. It creates a list (which is empty), it adds a listener to something called "db", and then it returns the list (which is still empty). It looks like the listener is intended to modify that list when an event occurs, but it's extremely unlikely that an event is going to occur within a few nanoseconds of its creation, so calling retrieve() is almost always going to return an empty list.
 
Christopher Adams
Greenhorn
Posts: 17
Chrome Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:So you've got an empty list. That either means that nothing was ever added to the list, or that the list was emptied at some point.

My money's on the "nothing was ever added to the list" option. That, after all, is what the retrieve() method appears to do. It creates a list (which is empty), it adds a listener to something called "db", and then it returns the list (which is still empty). It looks like the listener is intended to modify that list when an event occurs, but it's extremely unlikely that an event is going to occur within a few nanoseconds of its creation, so calling retrieve() is almost always going to return an empty list.


But in my logcat image:
https://coderanch.com/t/676272/a/8181/logcat.PNG
it shows that the ArrayList has the data added to it.
But it is empty before it is returned. And I don't see where it would be emptied.
 
Paul Clapham
Sheriff
Posts: 22841
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Christopher Adams wrote:But in my logcat image:
https://coderanch.com/t/676272/a/8181/logcat.PNG
it shows that the ArrayList has the data added to it.
But it is empty before it is returned. And I don't see where it would be emptied.


Yes, indeed. (BTW why is it an image when logcat produces text? It's a lot easier to deal with text here. Anyway...) The log shows that you get an empty list, and then 1.5 seconds later some data is added to the list. I don't quite understand that FirebaseHelper API but as far as I can tell, after you get the empty array back from the retrieve() method you're supposed to wait until data appears in it. Perhaps there's some documentation which explains how you're supposed to work with it.
 
Christopher Adams
Greenhorn
Posts: 17
Chrome Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Yes, indeed. (BTW why is it an image when logcat produces text? It's a lot easier to deal with text here. Anyway...) The log shows that you get an empty list, and then 1.5 seconds later some data is added to the list. I don't quite understand that FirebaseHelper API but as far as I can tell, after you get the empty array back from the retrieve() method you're supposed to wait until data appears in it. Perhaps there's some documentation which explains how you're supposed to work with it.


I am beginning to think it is time to start from the beginning with this firebase project. The firebase documentation is plentiful but it sure is not easy to decipher some of the things they are talking about. It has been a huge trial and error process.
Thank you for looking over my code. I really just wanted to make sure I wasn't missing something obvious. But I will be going over the Firebase documentation some more and looking for some good resources for Firebase info.
Thanks again.
Chris
 
Paul Clapham
Sheriff
Posts: 22841
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My first impression of that FirebaseHelper code, after looking at it in the light of of your log data, was that I wouldn't have written it that way. BUT... I've worked with Java a lot but not with Android, and it may be that they had good design reasons for doing it their way. So, good luck with your project.
 
Dave Tolls
Ranch Foreman
Posts: 3068
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, what's happening is this.
When your Activity is created, you create you Helper.
Then you call retrieve on that.

If you look at retrieve, excluding the listener code (which does not run as part of the retrieve() method), all that happens is you create a List and return it.
That's why it is empty.

You then turn this into an array (exerciseArray), which is now completely detached from the listener (it is not the same object as the List) so any changes to the List will not be reflected in the array, and that array is used to populate your Spinner.

Now, that's just a quick read through the code as-is.  I'm not too sure what fires the events that the db listener is reacting to, but in any case whatever they are will have no effect on your Spinner.  There's simply nothing connecting the two parts.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can trace from line 47 of that Today.java listing.

(Today) line 47, helper.retrieve() called

(FbH) line 34, enter retrieve()
line 38, addChildEvenListener() called
line 65, Log: "ArrayListBeReturn"
line 67, exit from retrieve()

(Today) line 49, Log: "ArrayList: "

Somehow...

(FbH) line 40, onChildAdded() is called
line 41, fetchData() is called
line 72, Log: "ArrayList2"
line 75, exit from fetchData()

line 42, Log: "ArrayListadded1"

This should correspond with what you're seeing in the logs, with the part after "Somehow..." getting repeated
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!