My App crashes because of Firebase data reading

My App crashes because of Firebase data reading



When I start my app with Android studio it crashes and I get the error:



java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.Integer.intValue()' on a null object reference



at main.MainActivity$3.onDataChange(MainActivity.java:151)



I guess it has to to with collecting the data from firebase. But before I had this error I could read data from firebase easily . I am a beginner and totally feel confused with this problem . I looked at other similair problems but still coudnt solve this.



MY Database (Firebase



MainActivity:


package main;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.CountDownTimer;
import android.preference.PreferenceManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

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.Query;
import com.google.firebase.database.ValueEventListener;

import java.math.BigDecimal;

import calebseeling2.click.R;


public class MainActivity extends AppCompatActivity
private Button click_start;
private double money_plus_value = 0;
private TextView number_final;
private static int clicker = 2;
// private int clicker_custom = shop.getClicker();
private TextView yourscore;
private int money_1 = 0;
private ImageView txtclose;
private int value;
private int value_for_score;
private Button btnFollow;
private int value_money;
private static String TAG = "MainActivity";
private FirebaseDatabase database;
private FirebaseDatabase database_for_score;
private SharedPreferences.Editor editor;
private ImageView shop;
private ImageView Highlist;
private TextView money;
private int Key;
private DatabaseReference myRef;
private DatabaseReference myRef_for_score;
private double money_per_click = 1;
private int number;
private int money_2;
private String name;
private static String Name;
private static SharedPreferences your_score;
private static SharedPreferences prefs_money;
private static SharedPreferences click;
private int bigDecimal;
private int value_clicks;
private static SharedPreferences Name_saver;
private TextView timert;
private TextView restart;
private boolean buttonclick = false;
String numberforsave = number + "";


public static int getClicker()
return clicker;



@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
first();

//PreferenceManager initialized


your_score= PreferenceManager.getDefaultSharedPreferences(this.getBaseContext());
prefs_money= PreferenceManager.getDefaultSharedPreferences(this.getBaseContext());
click= PreferenceManager.getDefaultSharedPreferences(this.getBaseContext());
Name_saver = PreferenceManager.getDefaultSharedPreferences(this.getBaseContext());
//initialized database
database_for_score = FirebaseDatabase.getInstance();
myRef_for_score = database_for_score.getReference("Highscore");
database = FirebaseDatabase.getInstance();
myRef = database.getReference(getName());

// Read from the database
myRef_for_score.addValueEventListener(new ValueEventListener()
@Override
public void onDataChange(DataSnapshot dataSnapshot)
// This method is called once with the initial value and again
// whenever data at this location is updated.
if (dataSnapshot.exists())
value = dataSnapshot.getValue(Integer.class);
Log.d(TAG, "Value is: " + value);
else
Toast.makeText(MainActivity.this, "Error!You are not on the database. Please deinstall and reinstall again", Toast.LENGTH_SHORT).show();








@Override
public void onCancelled(DatabaseError error)
// Failed to read value
Log.w(TAG, "Failed to read value.", error.toException());

);
myRef.addValueEventListener(new ValueEventListener()
@Override
public void onDataChange(DataSnapshot dataSnapshot)
// This method is called once with the initial value and again
// whenever data at this location is updated.
if (dataSnapshot.exists())
value_money = dataSnapshot.child("Money").getValue(Integer.class);
Log.d(TAG, "Value is: " + value);
else
Toast.makeText(MainActivity.this, "Error!You are not on the database. Please deinstall and reinstall again", Toast.LENGTH_SHORT).show();








@Override
public void onCancelled(DatabaseError error)
// Failed to read value
Log.w(TAG, "Failed to read value.", error.toException());

);
myRef.addValueEventListener(new ValueEventListener()
@Override
public void onDataChange(DataSnapshot dataSnapshot)
// This method is called once with the initial value and again
// whenever data at this location is updated.
if (dataSnapshot.exists())
value_clicks = dataSnapshot.child("Click").getValue(Integer.class);
Log.d(TAG, "Value is: " + value);
else
Toast.makeText(MainActivity.this, "Error!You are not on the database. Please deinstall and reinstall again", Toast.LENGTH_SHORT).show();








@Override
public void onCancelled(DatabaseError error)
// Failed to read value
Log.w(TAG, "Failed to read value.", error.toException());

);




//Item initialized
click_start = (Button) findViewById(R.id.Click_Start);
restart = (TextView) findViewById(R.id.restart);
number_final = (TextView) findViewById(R.id.Number_final);
yourscore = (TextView) findViewById(R.id.yourscore) ;
shop = (ImageView) findViewById(R.id.shop);
timert = (TextView) findViewById(R.id.time);
Highlist = (ImageView) findViewById(R.id.Highlist);

//Highscore site intent

Highlist.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
Intent intent = new Intent(MainActivity.this, Highscore.class);
startActivity(intent);

);

click_start.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)

//clicking
number = number + value_clicks;


// setting text to points
number_final.setText(number + "");









//adding money
money_plus_value = money_plus_value + money_per_click;





Log.d(TAG, "number" + money_plus_value);



//invisibility of icons
shop.setVisibility(View.INVISIBLE);
Highlist.setVisibility(View.INVISIBLE);
money.setVisibility(View.INVISIBLE);


//calling timer and setting buttonclick true
timer();
buttonclick = true;


//restarting activity when button clicked
restart.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
recreate();

);







);


//intent for shop activity
shop.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
Intent intent = new Intent(MainActivity.this , shop.class);
startActivity(intent);
intent.putExtra("money" , money_2);

);

//timer method
public void timer()
if (buttonclick == false)
new CountDownTimer(30000, 1000)

public void onTick(long millisUntilFinished)
timert.setText("Seconds remaining: " + millisUntilFinished / 1000);


public void onFinish()

//clickable false

click_start.setClickable(false);


//setting money
Log.d(TAG , "valuetest" + value_money);
myRef.child("Money").setValue(value_money +money_plus_value);

money_plus_value = 0;

//setting visibility
timert.setVisibility(View.INVISIBLE);
shop.setVisibility(View.VISIBLE);
Highlist.setVisibility(View.VISIBLE);
money.setVisibility(View.VISIBLE);

//Highscore setting
// if (number > value_for_score)
// myRef_for_score.setValue(number);
//







.start();

else







return;









//task when app first time opened

public void first()


SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
if (!prefs.getBoolean("firstTime", false))
ShowPopup();

// mark first time has ran.
SharedPreferences.Editor editor = prefs.edit();
editor.putBoolean("firstTime", true);
editor.commit();





//showpopup dialog
public void ShowPopup()
final Dialog dialog= new Dialog(this);


dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.dialog);
dialog.show();
dialog.setCancelable(false);


final EditText edit = (EditText) dialog.findViewById(R.id.username) ;



Button bt_yes = (Button)dialog.findViewById(R.id.Button_dialog);


bt_yes.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
FirebaseDatabase database1 = FirebaseDatabase.getInstance();
DatabaseReference myRef1 = database1.getReference(edit.getText().toString());

myRef1.setValue(edit.getText().toString());
myRef1.child("Click").setValue(2);
myRef1.child("Money").setValue(200);

editor = Name_saver.edit();
editor.putString("Name" , edit.getText().toString());
editor.apply();
dialog.dismiss();






);


public static String getName()
String nameer = Name_saver.getString("Name" , "ERROR");
return nameer;









I wrote a quick answer based on spelunking the code you shared below. But please read how to create a minimal complete verifiable example, as it is a far more efficient way to get help with code-related questions.
– Frank van Puffelen
Aug 26 at 16:01




1 Answer
1



The error appears on this line:


value_clicks = dataSnapshot.child("Click").getValue(Integer.class);



So it sounds like there isn't a integer value in the Click property of your JSON there.


Click



From scanning the code, you attach a listener to:


myRef = database.getReference(getName());



Which in turn is coming from:


Name_saver.getString("Name" , "ERROR");



Since Name_saver is a SharedPreferences object, it seems likely that you don't have a name saved in the shared preferences anymore.


Name_saver


SharedPreferences





But why isnt there one anymore?I coded the app sothat if the app is opened firsttime, you have to add a name to SharedPrefences.How did the name get lost?
– C.Seeling
Aug 26 at 16:09





I just tried out to take manuell a name from the database but it still crashes with the error: Fatal signal 6 (SIGABRT). I put into myRef:Caleb
– C.Seeling
Aug 26 at 16:15







By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Crossroads (UK TV series)

ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế