Search code examples
androidfirebasefirebase-realtime-databasetelephonymanager

Firebase data upload crashing Android application


I am collecting signal data and uploading it to a database.

I have several functions and made a few test functions before trying to implement the rest. It crashes when I upload with the classes I want to use but not with the test cases.

Here is the code for the classes methods and initializations:

Initialize database

FirebaseDatabase database;
DatabaseReference myRef;

classes:

package com.example.mike.maps_example;

/**
 * Created by Mike on 3/10/2017.
 */

public class WcdmaSignalEntry {
    Double Latitude, Longitude;
    String dBm;
    String Cid;
    String Lac;
    String Mcc;
    String Mnc;
    String Psc;
    String Uarfcn;
    String connectionType = "WCDMA";

public WcdmaSignalEntry(){
    // Default constructor required for calls to DataSnapshot.getValue(User.class)

}

public WcdmaSignalEntry(double Latitude, double Longitude, String dBm, String Cid, String Lac, String Mcc, String Mnc, String Psc, String Uarfcn){
    this.Latitude = Latitude;
    this.Longitude = Longitude;
    this.dBm = dBm;
    this.Cid = Cid;
    this.Lac = Lac;
    this.Mcc = Mcc;
    this.Mnc = Mnc;
    this.Psc = Psc;
    this.Uarfcn = Uarfcn;
    }
}

Here is the code for the testing class:

package com.example.mike.maps_example;

/**
 * Created by Mike on 28/9/2017.
 */

public class CellSignalEntry {
    public double Latitude;
    public double Longitude;
    public String dBm;
    public String CI; //cell identity
    public String LAC; //Location Area Code
    public String PSC; //primary scrambling code
    public String TA; //timing advance
    public String PCI_LTE; //physical cell identifier for LTE
    public String PCI_GSM; //physical cell identifier for GSM

public CellSignalEntry() {
    // Default constructor required for calls to DataSnapshot.getValue(User.class)
}

public CellSignalEntry(String dBm, String CI, String LAC, String PSC, String TA, String PCI_LTE, String PCI_GSM, double Latitude, double Longitude) {
    this.dBm = dBm;
    this.CI = CI;
    this.LAC = LAC;
    this.PSC = PSC;
    this.TA = TA;
    this.PCI_GSM = PCI_GSM;
    this.PCI_LTE = PCI_LTE;
    this.Latitude = Latitude;
    this.Longitude = Longitude;
    }
}

I have two methods.

private void writeNewWcdmaEntry(double lon, double lat,  String dBm, String Cid, String Lac,  String Mcc, String Mnc,String Psc, String Uarfcn, long entry){
    WcdmaSignalEntry newEntry = new WcdmaSignalEntry(lon, lat, dBm, Cid, Lac, Mcc, Mnc, Psc, Uarfcn);
    tvView.setText(newEntry.dBm);
    tvView.setText(newEntry.Cid);
    //myRef.child("Entry").child(Long.toString(entry)).setValue(newEntry);
}

and

private void writeNewUser(long userId, String dBm, String CI, String LAC, String PSC, String TA, String PCI_LTE, String PCI_GSM, double currentLat, double currentLon) {
    CellSignalEntry user = new CellSignalEntry(dBm, CI, LAC, PSC, TA, PCI_LTE, PCI_GSM, currentLat, currentLon);

    myRef.child("Entry").child(Long.toString(userId)).setValue(user);
}

My app runs fine when I run the second method but when I run the first method it crashes when uploading data to the firebase database if I uncomment this line:

//myRef.child("Entry").child(Long.toString(entry)).setValue(newEntry);

I am completely stumped. Any help is greatly appreciated.

EDIT: Right after posting this I figured it out. I have no idea why but making all the variables "public" in the WcdmaSignalEntry class fixed the issue but I am not sure why, I am very new to java so an explanation would be nice :)!


Solution

  • Java documentation says:

    If a class has no modifier (the default, also known as package-private), it is visible only within its own package.

    If those two methods(from your question) are not inside the same package where the class WcdmaSignalEntry is, i.e. inside com.example.mike.maps_example then you cannot access those variables unless they are public.

    For more information on access modifier in java, refer this.