Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# programming
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 27-Sep-12 13:48pm
Comments
Sergey Alexandrovich Kryukov at 27-Sep-12 20:02pm
   
In what line?
--SA
Member 9417196 at 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 at 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
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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
  Permalink  
v2
Comments
ridoy at 28-Sep-12 1:43am
   
+5 SA..
Sergey Alexandrovich Kryukov at 28-Sep-12 13:20pm
   
Thank you,
--SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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.
  Permalink  

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

  Print Answers RSS
0 Maciej Los 245
1 OriginalGriff 210
2 Manfred R. Bihy 190
3 CHill60 180
4 _Amy 155
0 OriginalGriff 7,445
1 Sergey Alexandrovich Kryukov 6,157
2 Maciej Los 3,774
3 Peter Leow 3,448
4 CHill60 2,702


Advertise | Privacy | Mobile
Web01 | 2.8.140721.1 | Last Updated 28 Sep 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100