0

Im declaring from a big list of custom, multiple level, objects (here called venueCounter) a simple array of custom objects, with just the information needed for filling a list. I do this because its easier to sort my list and create sections in my list.

But when i try it i get a nullpointerexception. I think that is very weird, because when i tried to fill a stringArray it worked fine. Here is my code that fails:

 ListData ld[]= new ListData[venueCounter.size()];
     int selectedPic;
     JJsonResponse e;
     for(int i=0;i < venueCounter.size() ;i++){
         selectedPic = 0;
         e = venueCounter.get(i);
         ld[i]= null;//           data in listdata object   
         ld[i].name=e.venue.name;
         ld[i].tip_nl=e.venue.tip;
         ld[i].tip_en=e.venue.tip_en;
         ld[i].venueID=e.venue.id;
         // foto uitkiezien
         if (e.venue.venue_photos.isEmpty() == false){ // there is a picture
            for(int j=0; j < e.venue.venue_photos.size() ;j++){ // use user selected picture
                 if(e.venue.venue_photos.get(j).selected == true){ 
                     selectedPic = j;
                 }
                ld[i].photoUrl=e.venue.venue_photos.get(selectedPic).medium;
             }
         }else
             {
             ld[i].photoUrl="url for when there is no url";
           }

         Log.i("url nr. " + String.valueOf(i), ld[i].photoUrl);
     }

and the line with the error is:

ld[i].name=e.venue.name;

But the strange thing is, that when i declare a string array and fill it just the same:

String name[]=new String[venueCounter.size()];

and further in the code

name[i]=e.venue.name;

everything just works fine! But i want this array of listdata, so i can put it in my adapter and just work with it. Does anyone knows why my code fails and what im doing wrong?

1 Answer 1

2

I think the main problem here is the line with

 ld[i]= null;

If you make it null it wont be able to save any data. You should do a new ListData()... instead of the null.

It works with Strings because whenever you make a string equal to another, java creates the new object and so on.

Hope I have explained it correctly.

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

1 Comment

You are right! Thank you. I thought that i would create a new (empty) field in my array, and than fill it up in the next lines.

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.