• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How come I can't assign values to a reference variable inside an asynchronous listener?

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've attempted to assign values from the asynchronous listener addValueEventListener, and I just noticed that I can assign values to local variables, set text for TextViews but I cannot set values to class variables. Is there a way around this problem?

In this code snippet, I'm trying to assign employee to data retrieved from the the listener. But when I read the value from the employee variable; it reads null
 
Marshal
Posts: 28193
95
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
If I understand the code correctly: You create a listener and tell it to wait for something to happen (and when the something happens it should assign a value to a class-level variable). Then immediately you check the class-level variable, and no surprise, it hasn't had a value assigned to it yet.

Although I don't see where the variable named "employee" is declared, so maybe I'm misunderstanding the question and you're really asking why the code doesn't compile?
 
Marshal
Posts: 4501
572
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Where is employee declared?  I don't see it anywhere in the code that you posted.
 
Elisha Olade
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is the adjusted code
 
Paul Clapham
Marshal
Posts: 28193
95
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
Same answer then:

Paul Clapham wrote:If I understand the code correctly: You create a listener and tell it to wait for something to happen (and when the something happens it should assign a value to a class-level variable). Then immediately you check the class-level variable, and no surprise, it hasn't had a value assigned to it yet.

 
Elisha Olade
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:If I understand the code correctly: You create a listener and tell it to wait for something to happen (and when the something happens it should assign a value to a class-level variable). Then immediately you check the class-level variable, and no surprise, it hasn't had a value assigned to it yet.

Although I don't see where the variable named "employee" is declared, so maybe I'm misunderstanding the question and you're really asking why the code doesn't compile?



I'm using a NoSQL database from Firebase, and I'm using the listener to retrieve all the data in the database to find the specific data for the current user. The problem is that the instance of the Employee class is null after attaching the listener to the reference. The listener reads data when it is first attached or when there is a change in the database. Also, I can assign local variables within the listener's scope without getting null values. It's almost like the compiler doesn't recognize the assignment of the data to the outside variable at runtime. My goal was just to retrieve the user's data from the listener and assign it to an instance variable outside the listener's scope - then detach the listener.

You can get information on the listener from: Read and Write Data
 
Paul Clapham
Marshal
Posts: 28193
95
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
Well, my hypothesis is that line 65 happens before line 50. You could do some logging to test that hypothesis.

It's also possible that snapshot.getValue(Employee.class) returns null, I suppose, but you're already logging that so it's easy to check.
 
Elisha Olade
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I/empCheck: null
Yeah, Logcat shows that employee is null.
 
Elisha Olade
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
snapshot.getValue(Employee.class) returns an Employee object that I used to get the last name of the current user. Its almost as if employee is not null within the listener scope and null outside the listener's scope.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic