android


Android EventListener not firing


what im trying to do right now is trying to grab a location object that is already in my firebase database eg: Artic base and store it in the localeobj i created but for some reason when i run the program it keeps saying localeobj is null am i doing my listener correctly?
package com.example.spy;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import java.util.ArrayList;
import java.util.Random;
import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
public class GameJoin extends AppCompatActivity implements View.OnClickListener {
private FirebaseDatabase database;
private DatabaseReference myRef;
private FirebaseUser user;
private TextView location;
private TextView role;
private TextView desc;
private Button back;
private Intent extra;
private Bundle data;
private String spy;
private Location localeObj;
private String game_code;
private Random rand;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
rand = new Random();
//Firebase stuff
user = FirebaseAuth.getInstance().getCurrentUser();
database = FirebaseDatabase.getInstance();
myRef = database.getReference();
//Views
role = (TextView) findViewById(R.id.label_role);
location = (TextView) findViewById(R.id.label_location);
desc = (TextView) findViewById(R.id.descriptionText);
//Buttons
back = (Button) findViewById(R.id.button3);
//Attach a listener to the button
findViewById(R.id.button3).setOnClickListener(this);
//Retrieve data from previous activity
extra = getIntent();
data = extra.getBundleExtra("game_data");
game_code = data.getString("game_code");
//TODO: Pull location object from Firebase
myRef.child("lobby").child(game_code).child("location").addValueEventListener( new ValueEventListener() {
#Override
public void onCancelled(DatabaseError error) {
System.out.println("************************IN CANCELLED*************************************");
}
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
System.out.println("************************OUTSIDE IF*************************************");
for (DataSnapshot data : dataSnapshot.getChildren()) {
localeObj = dataSnapshot.getValue(Location.class);
System.out.println("************************IN IF*************************************");
}
}
});
myRef.child("lobby").child(game_code).child("spy").addValueEventListener( new ValueEventListener() {
#Override
public void onCancelled(DatabaseError error) {
}
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot data : dataSnapshot.getChildren()) {
spy = (String) dataSnapshot.getValue(String.class);
}
}
});
//Display a special UI if the user is a spy
if (user.getEmail().equals(spy)) {
role.setText("You are the spy. Don't get caught!");
location.setText("Mystery Location");
desc.setText("Welcome to Spy Fall. Keep your identity hidden for the length of the game, or figure out what the location is to win the game.");
} else {
role.setText(localeObj.getRole(rand.nextInt(3)));
location.setText(localeObj.getLocationName());
desc.setText("Welcome to Spy Fall. Infer who is the spy before time runs out in order to win the game.");
}
}
#Override
public void onClick(View v) {
//Stuff happens when buttons are clicked
int id = v.getId();
if (id == R.id.button3) {
//Clean up old game lobby information
myRef = database.getReference("lobby");
myRef.child(game_code).child("players").child(user.getUid()).removeValue(); //Remove all data associated with the current game
//Return to the main lobby
Intent main_lobby = new Intent(GameJoin.this, MainActivity.class);
startActivity(main_lobby);
}
}
}
Here is the tomcat message i get too
--------- beginning of crash
05-09 11:01:42.718 3083-3083/com.example.spy E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.spy, PID: 3083
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.spy/com.example.spy.GameJoin}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.example.spy.Location.getRole(java.lang.Integer)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.example.spy.Location.getRole(java.lang.Integer)' on a null object reference
at com.example.spy.GameJoin.onCreate(GameJoin.java:112)
at android.app.Activity.performCreate(Activity.java:6679)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726) 
at android.app.ActivityThread.-wrap12(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:154) 
at android.app.ActivityThread.main(ActivityThread.java:6119) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 
https://gyazo.com/2b0c1ee92f365f06b1737c99793c96b3
ive posted how my database structure looks like too
If you want to get the locationName please use the following code:
myRef.child("lobby").child(game_code).child("location").addValueEventListener( new ValueEventListener() {
#Override
public void onCancelled(DatabaseError error) {
System.out.println("************************IN CANCELLED*************************************");
}
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
System.out.println("************************OUTSIDE IF*************************************");
String locationName = (String) dataSnapshot.child("locationName").getValue();
}
})
Hope it helps.

Related Links

How to record audio file using timer
What is the solution to NativeScript “Cannot read property 'match' of null” error after “tns doctor”
How to get the No of track from an Artist in mediastore
Sync “pause/resume” button's image with the actual state
How to enable split screen in my android app
Google Proximity Beacon API gives 403 unauthorised error
Track google my account history
Instagram How can I get users id in Sandbox mode
Calling fragment within a fragment
Latest Google Play Services 9.6.80 cause “FirebaseCrash: Failed to initialize crash reporting”
How to use pocketsphinx in Android Studio
Data Binding class not generated
gradle is not resolving any new dependencies in android studio
How to use Context paramater with acceptCall function?
How to make a layout to behave like AlertDialog in Android
Android: Want to select only one imageview at a time using Fragment

Categories

HOME
fabricjs
jpa
paraview
delphi-7
c++11
google-spreadsheet
pda
rsa
serialization
package
requirements
rendering
cublas
square
gembox-spreadsheet
crystal-reports-2013
webix
flume
character
google-schemas
fhir
enthought
jxl
intercom
azure-powershell
gmm
oledb
googlebot
rubymotion
href
ios10.2
opentk
ms-access-web-app
progress-bar
soot
subclass
testcomplete
panoramas
paragraph
deltaspike
fossil
raft
sim-card
apns-php
verifiable-c
laravel-elixir
android-ibeacon
linqpad
ssrs-2014
stylecop
puredata
fwrite
dynamic-linq
proget
term
configurationmanager
rserve
aquamacs
chromium-os
hadoop-2.7.2
vulcanize
iotivity
tinkerpop3
dup
false-sharing
user-management
spring-retry
avisynth
wiredep
typemock
vacuum
apache-commons-daemon
snoop
ms-access-2000
matlab-deployment
android-broadcast
unix-socket
matrix-inverse
dct
terminate
websphere-esb
menubar
high-resolution
spring-3
solace-mq
merb
mysql-error-2013
fragmenttransaction
inkcanvas
multiplexing
httponly
role
jquery-tools
matlab-load
winmain
listactivity
inline-formset
undefined-index
ajax-polling
feasibility
java1.4
movieplayer

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App