2

Similar questions on stackoverflow that resemble my situation are: First and Second.

The answers to the First question suggest initializing the ArrayList before using it. The Second question does not have an answer, however, the author of the question says that 'after trying a few times, the error solved automatically.

Initially, I hadn't initialized my ArrayList and got the above-mentioned error. However, after initializing too, I get the same error after a few iterations. To clarify more, the following is my situation.

ArrayList1 stores values from different sensors where the value on the first index is the preferable value in my project. Every second (one set of data sampled at every second), the values in the ArrayList1 changes, and I am trying to save the value at index = 0 of the ArrayList1 to the next ArrayList (ArrayList2) by appending it with the following code snippet.

ArrayList2.add(Float.valueOf(String.valueOf(ArrayList1.get(0))));

Note that, both the ArrayLists are declared and initialized as Public. My code runs perfectly for the first FOUR iterations and throws the Attempt to invoke virtual method 'boolean java.util.ArrayList.add(java.lang.Object)' on a null object reference error after that. I verified it by printing both the ArrayLists on the console.

Am I missing something here?

2
  • Can you post the method where you are initializing the ArrayLists? Commented Nov 20, 2020 at 3:00
  • @SarahKhan Both of them are initialized at the beginning of the class as: public class MySensors extends AppCompatActivity{ public List<String> ArrayList1 = new ArrayList<>(); public List<Float>ArrayList2 = new ArrayList<>(); ------------------------------- - - - - - - - - - - - - - - - - - - ---- - - - - - - - - - - - - - - - } Commented Nov 20, 2020 at 3:24

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.