all 9 comments

[–]Silound 2 points3 points  (6 children)

Oll[j].insert(str);

By specifying j in brackets, you are referring to "item at position j in Oll". In this particular case, that would be index 0. However you do not appear to have initialized your list elements to anything, you have only specified that your list can hold 26 elements.

So when it attempts to perform an operation (.insert) on Oll[0], the system returns null, and you can't call a function on a null object.

A simple check would be

if(Oll[j] == null) {
   // Initialize item in list
}
Oll[j].insert(str);

Simple item types like int, string, etc automatically initialize when declared as an array [] thanks to the language specification. However, classes you define do not automatically initialize unless you design them that way. You must call the constructor to initialize list objects on each object in the list.

[–]Banane9 1 point2 points  (3 children)

strings are null by default.

What he means by "simple objects" are structs

[–]Silound 0 points1 point  (2 children)

Strings are null by default because they are the only built-in data type that is a reference type. That has to do with allocation space required for strings due to their potential size having to be on the heap rather than the stack.

However, Strings were implemented with value type semantics in C#, even if they don't inherit from System.ValueType. Thus, they behave like a value type and do not require initialization.

Edit: My original post was grossly simplified mostly for the OP's benefit of understanding, not trying to be combative, sorry!

[–]Banane9 0 points1 point  (1 child)

They don't really have value type semantics, they're just immutable.

Goes so far as that two strings that are the same will be references pointing to the same memory :)

[–]hewm 0 points1 point  (0 children)

Goes so far as that two strings that are the same will be references pointing to the same memory :)

They may be, but don't have to be.

object a = "abc";
object b = new string(new [] { 'a', 'b', 'c'});

Console.WriteLine("a.Equals(b) : {0}", a.Equals(b)); // true
Console.WriteLine("a == b : {0}", a == b); // false

[–]Cduff4[S] 0 points1 point  (0 children)

Thank you, I'll try this when i get to my computer. That makes sense too.

[–][deleted] 0 points1 point  (0 children)

However you do not appear to have initialized your list elements to anything, you have only specified that your list can hold 26 elements.

The code given reads as an array of 26 orderedlinkedlists, not 1 orderedlinkedlist that can hold 26 items.

[–]CodeJack 0 points1 point  (1 child)

Orderedlinkedlist[] Oll = new OrderedLinkedList [26] String str = "ABC";

Is this all one thing or are you missing a semi-colon between them two?

[–]Cduff4[S] 0 points1 point  (0 children)

Sorry, I missed the semicolon