Click here to Skip to main content
15,889,839 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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

using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnorderedArrayListNamespace;

namespace test
{
    class Program : IComparable
    {
         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();
        }
    }
        }
Posted
Comments
Sergey Alexandrovich Kryukov 27-Sep-12 20:02pm    
In what line?
--SA
Member 9417196 27-Sep-12 20:04pm    
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 . . .
Sergey Alexandrovich Kryukov 27-Sep-12 20:17pm    
OK, now you can see... so where is the UnorderedArrayList is defined, go there? UnorderedArrayList.Java or what? You might have added something wrong as the element, such as null.
Please see the instructions in my answer and examine relevant variables under debugger and then resolve the problem.
--SA

This kind of problem is one of the easiest to resolve. Most of your information is irrelevant; you only had to point out the line of code where it happens. But, not to worry: find it now.

Run it under debugger and let the execution stop at the exception. Once you locate the like where this exception is thrown, put a break point on a line above and restart the application. When it stopped again, examine all variables/members of reference types involved in the like where the exception was thrown: the reason of it is that one or more of them is null, but you tried to de-reference it as if it wasn't null.

Located it? Now, think how to fix it: either make sure the object in question is properly initialized and does not becomes null by the moment of time when the exception is thrown, or check it for null and don't execute next line if it is. What you really need depends on the purpose of that line of code; and it may need some redesign of the code.

This is all quite simple.

—SA
 
Share this answer
 
v2
Comments
ridoy 28-Sep-12 1:43am    
+5 SA..
Sergey Alexandrovich Kryukov 28-Sep-12 13:20pm    
Thank you,
--SA
According from the output you showed, the error starts in x.InsertionSort();. That function is defined in your UnorderedArrayListNamespace. The source code is in ArrayList.cs, and it crashes in line 108.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900