0

I have an object of arrays and I am pushing it to an ArrayList like this:

static ArrayList<String> jsonResponse = new ArrayList<>();

//inside the method
strResp = response.body().string(); //the response I get from the server
jsonResponse.clear();  //clear the array first
jsonResponse.add(strResp);

Now from the other class, I am accessing it like this but I can't get it to print because it says NULL:

public class SecondClass extends ReactContextBaseJavaModule {

private FirstClass firstclass;

  public void getLocks(Callback callback) {
  System.out.println("Print object: " + firstclass.jsonResponse);
  }
}

First question, is ArrayList a proper way of achieving what I want in this case?

Second question, why am I not able to print it in the other class?

I do not have any experience in Android/Java so I know this might sound like a dumb question to many of you but please try to understand!

  • Your object have not inited. Add this line `firstclass = Firstclass();` – GHH Feb 10 '20 at 00:23
  • @GuanHongHuang am I not initiating the firstclass like this: private FirstClass firstclass; ? –  Feb 10 '20 at 00:30
  • you need a parcelable if you want to pass an object to other activity in android. https://stackoverflow.com/a/15138146/8109202 – Ezra Lazuardy Feb 10 '20 at 00:32
  • @bmm declare is different from init – GHH Feb 10 '20 at 00:45
  • @GuanHongHuang but why does it work like that when I try to print a hardcoded string but not with the arraylist? I am just declaring it not init –  Feb 10 '20 at 00:46

3 Answers3

0

You are creating a FirstClass variable, but you are not initiating it. FirstClass is an object which contains a jsonResponse list. If an object is not initiated then the list itself will always be null. You need to call the constructor of FirstClass like:

firstclass = FirstClass(<add parameters here if any>);

Hope this helps :)

manzk
  • 586
  • 1
  • 4
  • 11
  • but why am I able to print for example a String but not an array the way I am doing it? –  Feb 10 '20 at 00:36
  • @bmm could you please share some more from your code so we can get a better look at what you are trying to achieve? – manzk Feb 10 '20 at 00:37
0

you create private list of strings in the class and then try to access it from another class. In addition your object have not initialized

1) In the code every time before you insert a string, you clear the list. so if you don't use more than one string just use String.

2) Init your object and add 'public' before the static and it should work. i.e:

firstclass = FirstClass();

and

public static ArrayList<String> jsonResponse = new ArrayList<>();
shu
  • 139
  • 1
  • 4
  • ok so the only change I should make is to add public before static right? –  Feb 10 '20 at 00:44
  • and init your object – shu Feb 10 '20 at 00:45
  • I think I am initializing the object in a proper way because I am in fact able to print lets say a hardcoded string from the other class but when I try to do the same with the ArrayList it doesnt print –  Feb 10 '20 at 00:45
  • then try just to add public and if it run, its ok. – shu Feb 10 '20 at 00:47
  • unfortunately it didnt work, the error says : " cannot convert argument of type class java.util.ArrayList" –  Feb 10 '20 at 00:52
  • I got it! I have to use WritableMap resultData = new WritableNativeMap(); instead of ArrayList in my case because of React native bridge I am using. However, do u know how can I push that object to this? –  Feb 10 '20 at 00:56
0

In Java to access static elements, you don't need instance of the class(FirstClass). You simply do

FirstClass.jsonResponse();

The reason why you are getting null is be because you are trying to print the List object, which is not a String but a list of Strings. So do a the following:

if(!jsonResponse.isEmpty()){
    //print the first String in the List
    jsonResponse.get(0);
}

To answer your other question, LinkedList has more functionality, but List is faster.Check this out for more explanation.

The_Martian
  • 3,684
  • 5
  • 33
  • 61