0

i am trying enter link description here

as to add values to single as well as 2dim array dynamically, but while adding values it shows null pointer ,

here is my code

Arr points1[];
    points1 = new Arr[listItemList.size()];

    for(int i=0;i<listItemList.size();i++)
    {

        ListItemReminderSummary listItem = listItemList.get(i);
        Log.i("listItem.Car_Id", listItem.Car_Type);

        points1[i].Car_Id = listItem.Car_Id;
        points1[i].Car_Type =  listItem.Car_Type;

    }
    for(int i=0;i<listItemList.size();i++)
    {

         System.out.println( points1[i].Car_Id +  points1[i].Car_Type);
    }

Null pointer at points1[i].Car_Id = listItem.Car_Id;

any suggestion, thnx in advance.

Community
  • 1
  • 1
pitu
  • 822
  • 3
  • 11
  • 35

4 Answers4

1

initialize the items in Array...

for (int i = 0; i < listItemList.size(); i++) {
        ListItemReminderSummary listItem = listItemList.get(i);
        Log.i("listItem.Car_Id", listItem.Car_Type);
        points[i] = new Arr();
        points1[i].Car_Id = listItem.Car_Id;
        points1[i].Car_Type = listItem.Car_Type;
}
Gopal Gopi
  • 11,101
  • 1
  • 30
  • 43
  • when i use your second method it give error of 01-02 02:15:35.127: E/AndroidRuntime(960): java.lang.ArrayStoreException: source[0] of type com.example.ListItemReminderSummary cannot be stored in destination array of type com.example.Arr[] – pitu Jan 02 '14 at 13:16
  • first, check your ArrayList contains items. – Gopal Gopi Jan 02 '14 at 13:35
  • yes Log.i("listItem.Car_Id", listItem.Car_Type); it prints the value on log – pitu Jan 02 '14 at 13:36
0

You have to initialize cells of array.

 for(int i=0;i<listItemList.size();i++){
 points[i] = new Arr();
 }
Adam Radomski
  • 2,515
  • 2
  • 17
  • 28
0

You have not Allocated memory to the Arr that is why you're trying to dereference an uninitialised pointer (i.e. writing to a random chunk of memory), which is undefined behaviour.

Change ur starting 2 lines

Arr points1[] = new Arr[listItemList.size()];


for (int i = 0; i < listItemList.size(); i++)
{
    ListItemReminderSummary listItem = listItemList.get(i);
    Log.i("listItem.Car_Id", listItem.Car_Type);
    points1[i].Car_Id = listItem.Car_Id;
    points1[i].Car_Type = listItem.Car_Type;
}
Vivek Warde
  • 1,936
  • 8
  • 47
  • 77
0

Did you make sure that listItemList.get(i) returns a value? Perhaps there is nothing returned from this.

AnOldSoul
  • 4,017
  • 12
  • 57
  • 118