Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
1.78/5 (3 votes)
See more:
I have searched the forums and can't find a solution to this one. I am trying to declare a list(of List(of something). I have tried to copy the other examples I have found but can't get it to work. Here is just a short clip of my code:

VB
Dim SeriesList As List(Of List(Of PlotData))

   Sub Main()

       For x As Integer = 0 To 3
           Dim Series As New List(Of PlotData)
           SeriesList.Add(Series)
       Next


When I get to the SeriesList.Add(Series) I get a NulReferenceException pointing to this line telling me that the "Object reference not set to an instance of an object" and to "use the 'new' keyword to create an object instance." But... I can't figure out where to put the 'new' keyword. Everything I try just causes syntax errors.

I so very much appreciate all the help so willingly given by so many knowledgeable people here, so, thanks in advance.
Posted

I don't see the problem unless the compiler has a problem with the nested list declaration. The code should read:

Dim SeriesList as New List(Of List(Of PlotData))

If this doesn't work, I'd try

Dim SeriesList as New List (Of Object)
I would then cast back when accessing the nested lists
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 20-Aug-14 12:15pm    
Why the would you think the first line may not work? Why "trying" bad things? Why casting, if OP uses generics (which, by the way, I advised answering his original question)?! You see, if you are not sure, not answering is much better. It's apparent that you are not confident with this matter yourself, so why confusing the readers?

"Trying" is fine, but no one should write code by trying. The developer should understand each and every line of what one is doing. Please, don't set a bad model of behavior.

—SA
PhilLenoir 20-Aug-14 12:26pm    
Sergey, if you read his question, he said he had tried "New" in various places. I don't believe there would be any problem with the first line, but depending on his version of VS there may be a parser bug. His reference to his attempts would lead me to assume that he had tried just that. I prefer to give options, that way the asker can get on and solve his problem without bouncing things backwards and forwards. If I believe I know all the facts, my answers are more direct. 1) I didn't realize that you had given an answer to a similar question from him. 2) I offered a solution which will work on my version if VS, however I never assume facts not in evidence, so the second option works. The cast would only be necessary if he had to use a generic and would ensure that his contained list was strongly typed to a list.
Sergey Alexandrovich Kryukov 20-Aug-14 12:31pm    
No, you are not giving any reasonable options, you are doing what I just described: you are not sure about the matter and advise to try. If you were sitting in one room with OP, it would be fine, but suggesting bad, really bad thing (which cannot work) as an option at public forum is not really responsible way of doing thing. Why would you at least did not try writing the code?
—SA
PhilLenoir 20-Aug-14 12:34pm    
Why are yopu trolling. You seem to be denying that the solution does not work. It does.
PhilLenoir 20-Aug-14 12:29pm    
... and we all have to program by trying. Most especially when our toolsets are buggy (which they often are). Workarounds are rarely, if ever, best practice, but sometimes you gotta do what you gotta do.
You should have asked that on the page of your original question, where I already answered: Work with a collection of collections(of objects)[^].

As you could see, the list type is generic, so it can be a list of the elements of any other types, so, why not lists? How can it possibly be a problem? If you, for example, need a list of dictionaries, or a dictionary of lists, would it be another question? :-)

Let's see: you have initialized each list to become and element of the outer list. But you did not initialize the outer list itself. SeriesList is null.

Look, I don't want just to fix your bug, I want to you to think by yourself. It looks like you understand that all objects need to be initialized, eventually. If some object has to be initialized, why not initializing another one? In the case of SeriesList, initialize it at the point of declaration.

That's not all. How about asking questions providing adequate information?

You did not show where the exception with the message "Object reference not set to an instance of an object" is thrown.

Not to worry. This is one of the very easiest cases to detect and fix. It simply means that some member/variable of some reference type is dereferenced by using and of its instance (non-static) members, which requires this member/variable to be non-null, but in fact it appears to be null. Simply execute it under debugger, it will stop the execution where the exception is thrown. Put a break point on that line, restart the application and come to this point again. Evaluate all references involved in next line and see which one is null while it needs to be not null. After you figure this out, fix the code: either make sure the member/variable is properly initialized to a non-null reference, or check it for null and, in case of null, do something else.

Please see also: want to display next record on button click. but got an error in if condition of next record function "object reference not set to an instance of an object"[^].

Sometimes, you cannot do it under debugger, by one or another reason. One really nasty case is when the problem is only manifested if software is built when debug information is not available. In this case, you have to use the harder way. First, you need to make sure that you never block propagation of exceptions by handling them silently (this is a crime of developers against themselves, yet very usual). The you need to catch absolutely all exceptions on the very top stack frame of each thread. You can do it if you handle the exceptions of the type System.Exception. In the handler, you need to log all the exception information, especially the System.Exception.StackTrace:
http://msdn.microsoft.com/en-us/library/system.exception.aspx[^],
http://msdn.microsoft.com/en-us/library/system.exception.stacktrace.aspx[^].

The stack trace is just a string showing the full path of exception propagation from the throw statement to the handler. By reading it, you can always find ends. For logging, it's the best (in most cases) to use the class System.Diagnostics.EventLog:
http://msdn.microsoft.com/en-us/library/system.diagnostics.eventlog.aspx[^].

Good luck,
—SA
 
Share this answer
 
Comments
ScottTheHunter 20-Aug-14 12:21pm    
Wow! what an incredible amount of knowledge and information. Thanks for all of that. Yes it is embarrassing to say that I was just missing a 'new' in the very first declaration. I guess I had been looking at it too long. But this is my first experience with using Generic Lists. I have not kept up with new developments over the years as I might have because VB is only a part of what I normally do. I am a controls engineer in industrial automation and work with a number of PLC and HMI programming software packages. Thanks again for all your help.
Sergey Alexandrovich Kryukov 20-Aug-14 12:41pm    
Actually, I did understand that it was just the random bug.
As I mentioned in the answer, I assumed you actually understand the critical importance of initialization. You just need more confidence, and then it will be at the level of natural reflexes. So, please don't be embarrassed; this little shock is supposed to give up to the confidence pretty soon.

But just think logically: after a generic type is instantiated (that is, all the generic parameters are substituted with some actual types), it becomes a complete type, no different from non-generic types. All type aspects, initialization/instantiation and all other ones, become the same. Then you apply the logic based on understanding of any complete types in general. Separation of concerns!

(Yes, and be careful with wrong answers, like Solution 1, where I can even see some ethical problem :-( )

Good luck, call again.

—SA

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