0

I am in the process of converting my working VB 2010 forms application to Visual Studio 2013. I've encountered a nullreferenceexception where one does not occur in the old code. Basically, I start with a fresh instance of the form. The form contains a treeview and the user can navigate down through it and select a node. Based on their selection another portion of the form gets populated (a datagridview) with the associated elements. This is an explore screen.

When the user is done exploring, the application captures the node they were on and saves it. Immediately, the form is closed, disposed and set to nothing (NULL).

' Class variable definition
Private XInvenNode As New TreeNode

If fdiaXInven Is Nothing Then fdiaXInven = New diaExploreInven(True, True)
fdiaXInven.tvInven.SelectedNode = XInvenNode
fdiaXInven.ShowDialog()
XInvenNode = .tvInven.SelectedNode
results = .SelectedItems
fdiaXInven.Close()
fdiaXInven.Dispose()
fdiaXInven = Nothing

If the user returns to the explorer screen in the same session, it is instantiated again and the saved tree node is supposed to be restored so the user can continuing exploring in the same area. This is when the nullreferenceexception occurs.

I found this link and understand what a nullreferenceexception is. I've set breakpoints in the code and analyzed the contents of the variables within fdiaInven. It is not Nothing or NULL! For example, the following code worked perfectly on the first pass through but failed the second time:

fdiaInven.tvInven.SelectedNode = XInvenNode

What am I missing here? Are there some new rules in VB since the 2010 version that would cause this?

Ebassador
  • 339
  • 3
  • 20
  • What is "XInvenNode" when you load it? If your saying that "fdiaInven" isn't nothing then the only thing left is your "XInvenNode" which probably is nothing... Set a break point on that line and hover over that variable see what it is? – Trevor Jan 12 '15 at 21:09
  • possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – John Saunders Jan 12 '15 at 21:17
  • 1
    You are telling us that you did `fdiaXInven = Nothing` then `fdiaInven.tvInven.SelectedNode = XInvenNode`? But you already cleared `fidaInven`! – John Saunders Jan 12 '15 at 21:18
  • There is a little bit more to the code that what I originally posted. I'll update the question to provide a little more clarity. – Ebassador Jan 12 '15 at 21:29
  • 2
    `fdiaInven.tvInven.SelectedNode = XInvenNode` this has 3 candidates which can be Nothing. which is it? – Ňɏssa Pøngjǣrdenlarp Jan 12 '15 at 21:38
  • @JohnSaunders you may have been a little too quick on the draw. I reference that article in my question. I read it thoroughly before I post my question. – Ebassador Jan 12 '15 at 21:40
  • I suggest that you look at the stack trace of the exception. You need to determine where exactly the exception is being thrown. It may be on that line of your code, in which case it will be dead easy to find the specific issue, as Plutonix has indicated. It might be, though, that that line causes an event to be raised in the handler for that event. You won't know if you don't actually look at the information provided to you expressly for the purpose of diagnosing the issue. – jmcilhinney Jan 12 '15 at 21:42
  • @Plutonix Thanks for your suggestion. I'm not clear though on what those 3 candidates are. Can you please explain further? – Ebassador Jan 12 '15 at 21:48
  • when you get the exception, hold the mouse over each 'word' in that line of code (as described in the VB answer to the linked dupe). One of them is Nothing. Which is it. You havent narrowed the problem down much. – Ňɏssa Pøngjǣrdenlarp Jan 12 '15 at 21:49
  • @jmcilhinney Stack trace is pretty basic: at Tool.Maint.btnBrowse_Click(Object sender, EventArgs e) in Maint.vb:line 265 – Ebassador Jan 12 '15 at 21:51
  • @Plutonix Duh! fdiaXinven = Text...Explore, fdiaXinven.tvInven = Treeview, Nodes, Count..., fdiaXinven.tvInven.SelectedNode = Nothing (this was true on the first pass too). XInvenNode = Text ... ATA. – Ebassador Jan 12 '15 at 21:57
  • `fdiaInven.tvInven.SelectedNode = XInvenNode` this will not work anyway. Nodes are objects, when you rebuild the TV next time thru, it has all new node objects and `XInvenNode` will not be one of them. You can check using Equals in the immediate window. All you may be doing is preventing all the nodes from being disposed since you are holding a reference. – Ňɏssa Pøngjǣrdenlarp Jan 12 '15 at 22:08
  • @Plutonix Actually, TV is always the same 'catalog' of inventory items but you may be on to something. Thanks. I will explore this further. – Ebassador Jan 12 '15 at 22:19
  • it may be built from the same data, but if you Dispose of stuff, when rebuilt it *will* contain new/different node *objects*. – Ňɏssa Pøngjǣrdenlarp Jan 12 '15 at 22:21
  • I understand your description about XInvenNode and agree with it. How in the world did it work under VS 2010?? @Plutonix Anyway, there is still a problem with fdiaInven. A property named CurrItem is accessible. I set a breakpoint at the first reference to this field and was able to change things in the debugger but when I executed the code to make the same change, it threw the NullReverenceException again. – Ebassador Jan 13 '15 at 17:55
  • Sorry, I have no idea what these things are, how they are designed or how they are used to be able to debug a comment. If something goes out of scope, CurrItem could easily become nothing or be a Property of a Nothing object in between point A and point B. – Ňɏssa Pøngjǣrdenlarp Jan 13 '15 at 18:01

0 Answers0