-3

I'm trying to implement bubble sort using linked list and here is my code:

        itr = head;
        for (int j = 1; j < size; j++) 
        {
            for (int k = 0; k < size - 1; k++) 
            {
                if (itr.item > itr.next.item) 
                {
                    t = itr.item;
                    itr.item = itr.next.item;
                    itr.next.item = t;
                 }
                itr = itr.next;
              }
        }

the line

if (itr.item > itr.next.item)

gives the null pointer exception. I don't know what is wrong with my code. and size is the number of nodes in the linked list.

Roman C
  • 49,761
  • 33
  • 66
  • 176
  • If you have no answer then don't vote the question down! –  Sep 20 '14 at 09:03
  • 1
    I think the downvotes are justified. You show no evidence that you have attempted to debug this for yourself. And it is a poor quality question on other grounds. – Stephen C Sep 20 '14 at 09:18
  • (And I'm sure that the people who down-voted knew the answer too ... but thought that the question didn't *deserve* one.) – Stephen C Sep 20 '14 at 09:22

1 Answers1

1

I can see a couple of blunders. But if I told you what they were, you would lose the benefit of learning how to debug your own code.

However, I will give you a couple of hints:

  1. What is itr.next going to be when when you reach the end of the list?

  2. What is itr going to be pointing after the end of the inner loop?

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216