I keep getting NullPointerException when I try to access array elements from another class using the get method. My code uses a constructor to initialize the array, and it works just fine.
MagazineValue.class
public class MagazineValue {
public MagazineValue(){
}
private String[]magazineName;
public MagazineValue(String[] magazineName){
this.magazineName = new String[magazineName.length];
System.arraycopy(magazineName, 0, this.magazineName, 0,
magazineName.length);
}
public String[] getMagazineName(){
return magazineName;
}
}
The constructor here copy's array value from the argument to a private array variable. When I try to invoke getMagazineName() from another class, it shows that the array is still null. Is there any way I can solve this? It sure seems easy but it took me a lot of time.
FirebaseValueListner.class
public class FirebaseValueListner {
public void setMagazineName(){
String[]magazineName={"African Daily" , "African Bussines", "African Women"};
MagazineValue setValue= new MagazineValue(magazineName);
}
}
The FirebaseValueListner gets values from the Firebase database and saves it values to an ArrayList and later copy's the values to arrays. The arrays are passed to the constructor of the MagazineValues.
LibraryFragment.class `
public class LibraryFragment {
public static void main(String[] args) {
// TODO Auto-generated method stub
String[]magazineName;
MagazineValue getMagazineName;
getMagazineName=new MagazineValue();
magazineName=getMagazineName.getMagazineName();
System.out.println("Magazine Name : " +magazineName[1]);
}
}
`