Sorry, but showing through images is much better, okay? The explanation follows below:
But if you declare the entire variable as a global variable, the following error occurs:
Please, what to do so that the variable value inside the blue rectangle goes to the value inside the red rectangle variable, in the first image, in the Android?
You don't. A variable declared in a method is only visible in that method. You either need to make it a class level variable, or you need to find another way of doing things.
The reason you're seeing the NullPointerException is that you haven't called the function yet when you try to use the class level variable. That means the variable isn't set, so its the default value of null. And obviously you can't dereference a null variable.
Your problem here is you have some circular logic. You're trying to set a change listener to a firebase reference, but trying to get the id of the reference inside the change listener. That's not going to work- how do you get a reference when you don't know what you're trying to get a reference to? Either you missed some other way to get that id, or you need to rethink what you're doing.