9 Replies - 436 Views - Last Post: 27 September 2012 - 11:46 PM Rate Topic: -----

#1 wolne  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 28-June 12

Object referance not set to an instance of an object ERROR

Posted 27 September 2012 - 04:41 PM

I have a program which 4 different files. The file to actually run and test all of my methods works perfectly for the
integer array but not for the string array. Can someone please help me with the error? When I run the program, the integer array list prints out, the max, min, remove methods all work. Then the string array prints but none of the methods print until I press enter, which brings up the error message. However, when I press enter, it does print the min, max, and remove method.
HERE IS WHAT I FIRST GET WHEN I RUN THE PROGRAM:

The original list:
5
12
2
29
5
2
33
-4
111
4
17

Maximum value:
111

Minimum value:
-4

Remove element from position 2:
5
17
2
29
5
2
33
-4
111
4

Remove the first element:
4
17
2
29
5
2
33
-4
111

Insertion sort:
111
33
29
17
17
5
4
4
2

String array:
b
a
f
d
c
e

WHEN I PRESS ENTER:

String array:
b
a
f
d
c
e

Maximum value:
f

Minimum value:
a

Remove element from position 2:
b
f
d
c
e




static void Main(string[] args)
        {
            UnorderedArrayList<int> u = new UnorderedArrayList <int>();

            int var = 5;
            u.insert(ref var);
            var = 12;
            u.insert(ref var);
            var = 2;
            u.insert(ref var);
            var = 29;
            u.insert(ref var);
            var = 5;
            u.insert(ref var);
            var = 2;
            u.insert(ref var);
            var = 33;
            u.insert(ref var);
            var = -4;
            u.insert(ref var);
            var = 111;
            u.insert(ref var);
            var = 4;
            u.insert(ref var);
            var = 17;
            u.insert(ref var);
            

            Console.WriteLine("The original list:");
            u.print();
            Console.WriteLine();

            Console.WriteLine("Maximum value:");
            Console.WriteLine(u.max());
            Console.WriteLine();

            Console.WriteLine("Minimum value:");
            Console.WriteLine(u.min());
            Console.WriteLine();

            Console.WriteLine("Remove element from position 2:");
            u.remove(2);
            u.print();
            Console.WriteLine();
          
            Console.WriteLine("Remove the first element:");
            u.removeFirst(5);
            u.print();
            Console.WriteLine();

            Console.WriteLine("Insertion sort:");
            u.InsertionSort(); 
            u.print();
            Console.WriteLine();

            
            UnorderedArrayList<string> x = new UnorderedArrayList<string>();
            Console.WriteLine("String array:");
            string svar = "b";
            x.insert(ref svar);
            svar = "a";
            x.insert(ref svar);
            svar = "f";
            x.insert(ref svar);
            svar = "d";
            x.insert(ref svar);
            svar = "c";
            x.insert(ref svar);
            svar = "e";
            x.insert(ref svar);
            x.print();
            Console.ReadLine();

            Console.WriteLine("Maximum value:");
            Console.WriteLine(x.max());
            Console.WriteLine();

            Console.WriteLine("Minimum value:");
            Console.WriteLine(x.min());
            Console.WriteLine();

            Console.WriteLine("Remove element from position 2:");
            x.remove("f");
            x.print();
            Console.WriteLine();

            Console.WriteLine("Insertion sort:");
            x.InsertionSort();
            x.print();
            Console.WriteLine();
        }

        public int CompareTo(object obj)
        {
            throw new NotImplementedException();
        }
    }
        }
    



Is This A Good Question/Topic? 0
  • +

Replies To: Object referance not set to an instance of an object ERROR

#2 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1012
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: Object referance not set to an instance of an object ERROR

Posted 27 September 2012 - 04:52 PM

You have to press enter because line 72 is reading from the console.

Where is the definition for UnorderedArrayList? What line throws the exception?
Was This Post Helpful? 0
  • +
  • -

#3 wolne  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 28-June 12

Re: Object referance not set to an instance of an object ERROR

Posted 27 September 2012 - 04:58 PM

Unhandled Exception: System.NullReferenceException: Object reference not set to
an instance of an object.
at ArrayListNamespace.ArrayList`1.InsertionSort() in C:\Users\Whitney\documen
ts\visual studio 2010\Projects\ConsoleApplication30\ConsoleApplication30\ArrayLi
st.cs:line 108
at test.Program.Main(String[] args) in C:\Users\Whitney\documents\visual stud
io 2010\Projects\ConsoleApplication30\ConsoleApplication30\Program.cs:line 98
Press any key to continue . . .
Was This Post Helpful? 0
  • +
  • -

#4 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1012
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: Object referance not set to an instance of an object ERROR

Posted 27 September 2012 - 05:13 PM

So the error is in the InsertionSort code, the code you didn't post. Specifically, line 108 of the code.

This post has been edited by Momerath: 27 September 2012 - 05:14 PM

Was This Post Helpful? 0
  • +
  • -

#5 wolne  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 28-June 12

Re: Object referance not set to an instance of an object ERROR

Posted 27 September 2012 - 05:17 PM

        public void InsertionSort()
        {

            int i, j;

            for (i = 1; i < list.Length; i++)
            {
                IComparable value = (IComparable)list[i];
                j = i - 1;
                while ((j >= 0) && (value.CompareTo(list[j]) > 0))
                {
                    list[j + 1] = list[j];
                    j--;
                }
                list[j + 1] = (T)value;
            }

        }



The error is in this line

while ((j >= 0) && (value.CompareTo(list[j]) > 0))

but im not sure what to change to correct the problem
Was This Post Helpful? 0
  • +
  • -

#6 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1012
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: Object referance not set to an instance of an object ERROR

Posted 27 September 2012 - 05:23 PM

In line 8, value should be of whatever type the array is, not IComparable. I'm guessing this is a generic (you use type T in line 15), so the line should read:
T value = list[i];


And your definition of the class should ensure that T implements IComparable.

This post has been edited by Momerath: 27 September 2012 - 05:25 PM

Was This Post Helpful? 1
  • +
  • -

#7 wolne  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 28-June 12

Re: Object referance not set to an instance of an object ERROR

Posted 27 September 2012 - 05:29 PM

Well I used IComparable because this method has to work for strings AND integers.. Maybe there is another way?

Yes it is a generic. So how do I ensure that T implements IComparable?
Was This Post Helpful? 0
  • +
  • -

#8 wolne  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 28-June 12

Re: Object referance not set to an instance of an object ERROR

Posted 27 September 2012 - 05:42 PM

I got it! Thanks so much for your help :)
Was This Post Helpful? 0
  • +
  • -

#9 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2270
  • View blog
  • Posts: 9,496
  • Joined: 29-May 08

Re: Object referance not set to an instance of an object ERROR

Posted 27 September 2012 - 05:59 PM

Have you researched what IComparable interface is? What methods have to implemented?
What the return times are?
Was This Post Helpful? 0
  • +
  • -

#10 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1012
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: Object referance not set to an instance of an object ERROR

Posted 27 September 2012 - 11:46 PM

They aren't implementing the IComparable interface, just ensuring that the objects that other programmers want to use with this code have implemented it, i.e.
class MyClass<T> where T : IComparable { ...

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1