0

I have a string array :

private String [] arrFilePaths=new String[4];

and I have a funtion which returns strings and im adding that to array:

                   @Override
                    public void accept(List<File> files) throws Exception {
                        int size = files.size();
                        while (size-- > 0) {
                            arrFilePaths[size]=files.get(size).toString();
                        }
                    }

Now i have a button and I am accessing array inside of it:

  @Override
        public void onClick(View v) {
                    if(arrFilePaths[0]==null){
                       //some code
                    }else {
                        //some code
                    }

Now the problem is arrFilePaths only return strings in button click sometime. most of time its returning null.The data always getting inserted to array before button click. so i dont know whats going on.

Yes i have tested in debug mode and arrFilePathsis getting data inserted, but somehow after the button click it saying null(even though I am using same variable). Can anyone explain about this weird output? Thanks!

4
  • 1
    Where is arrFilePaths declared? It most likely loses the value if it's only local to a function and not a global variable Commented Aug 9, 2017 at 13:57
  • I have declared array right after main class. and its above all functions Commented Aug 9, 2017 at 13:59
  • Adding to that i have also tried public static to make it global, Still im getting data sometimes, and sometimes not. Commented Aug 9, 2017 at 14:00
  • Can you please post a minimal code of your class file? We would need to understand the flow of your code to see where the disconnect is. Thanks. Commented Aug 9, 2017 at 14:01

4 Answers 4

2

As far as I can see, you will be getting null every time when your List<File> files has fewer elements than arrFilePaths array.

Imagine this is files list [L1 , L2 , L3] And your array is [A1 , A2 , A3 , A4 , A5 , A6]

As you're writing from end, the last element from the end of files will appear at last position of arrFilePaths so on so force.

Finally, array will look like [null, null, null, L1 , L2 , L3].

Obviously, the 1st element of this array will be null.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks that makes sense!
np :) I'd suggest you re-think how to store these elements. If you want to make it strictly 4, then maybe it makes sense to copy only e.g. first 4 elements from the list and put them into the array starting from index=0 just using for(int i = 0; i < files.size() && i < array.length; i++) { array[i] = files.get(i); }.
2

I am not sure how you are seeing the values sometimes because to me it seems like a logical problem in below code

while (size-- > 0) {
   arrFilePaths[size]=files.get(size).toString();
 } 

Lets assume your size is 3 so you travel here in the reverse order. So you fill the position 3 and 2 and 1 but as soon as size becomes 0, your condition fails and it never sets the value for 0th element.

then your if condition checks -

if(arrFilePaths[0]==null)

So its checking if 0th element is null which is obviously the case because your while loop didn't let it enter there. hence no data.

so if your size is more than 4 in that case your 0th element will get filled as your arrFilePaths can only take 4 elements

2 Comments

Thanks! it makes sense now!
@FajarKhan Great happy to help. Please accept the answer :)
0

In my opinion you should Modify this line

while (size-- > 0)

like this

while (size-- >= 0) {

Comments

0

Just replace that while with a fori, I don't see any reason at all why you would want to populate that array from the end to the front.

Comments

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.