Note: Forums will be making significant UX changes to address key usability improvements surrounding search, discoverability and navigation. To learn more about these changes please visit the announcement which can be found HERE.
Whats Wrong with My Code?? Need Guidance and Help

回答済み Whats Wrong with My Code?? Need Guidance and Help

  • Tuesday, April 10, 2012 6:19 AM
     
      Has Code

    I am Building an application which works somewhat like Form Designer. The Algorithm that Works for Custom Control doesn't Work for Standard Label. For label it gives and error "NullreferenceException was Handled" and Suggests to use "New" Keyword

    Following Codes Works well without any Error (it is to create UserControl Called RBG_TextField.txtField)

    Dim rbgtxt As RBG_TextField.TxtField = New RBG_TextField.TxtField
                            With rbgtxt
                                .Is_Admin = True
                                .ObjectType = RBG_TextField.FType.TextBox
                                .Name = ListView1.SelectedItems(0).SubItems(0).Text
                                .Caption = ListView1.SelectedItems(0).SubItems(2).Text
                                .Location = New Point(ctrlcounter * 5, ctrlcounter * 10)
    
    'This Function is written on ChildForm to Add Events to Control
    'ActiveContentX(curindex).childform addresses to Active Form in Current MDI
    'ListView as Items that are Fields in Table in Database                            
    
    ActiveContentX(curindex).childform.handladder(rbgtxt)
                            End With
                            ActiveContentX(curindex).childform.Tabs.TabPages(TreeView1.SelectedNode.Parent.Index).Controls.Add(rbgtxt)

    But Following Code gives an Error "NullReferanceException"

    Dim rbglbl As Label = New Label With rbglbl .Text ="Unbound Label" : .Name = "LBL" & ctrlcounter

    .Location = New Point(ctrlcounter * 5, ctrlcounter * 10) End With

    ' ActiveContentX(curindex).childform.handladder(rbglbl) ActiveContentX(curindex).childform.Tabs.TabPages(TreeView1.SelectedNode.Parent.Index).Controls.Add(rbglbl) 'Here The Error Occurs at 'rbglbl'


    Algorithm is almost same for both Controls but why Second Code gives an Error.


    Ravindra Gaikwad

All Replies

  • Thursday, April 12, 2012 2:17 AM
    Moderator
     
     

    Hi Ravindra,

      We're doing research on this issue. It might take some time before we get back to you. First of all,Could you give me the more information about ActiveContentX? If possible,please give me the scenario of this code snippet. What is the application of occasions?

    Sincerely,

    Jason wang

     

    Jason Wang [MSFT]
    MSDN Community Support | Feedback to us

  • Thursday, April 12, 2012 6:31 AM
     
     

    Hi Ravindra,

    Have you debugged the codes?   After these lins of codes, is the Label object null?  

            Dim rbglbl As Label = New Label
           
    With rbglbl
               
    .Text ="Unbound Label" : .Name = "LBL" & ctrlcounter

                .Location = New Point(ctrlcounter * 5, ctrlcounter * 10)
           
    End With

    Based on my test, these codes work fine to create the Label instance.   When the error occurs, is the Label instance null or is it possible that other values can be null in this line of codes?   ActiveContentX(curindex).childform.Tabs.TabPages(TreeView1.SelectedNode.Parent.Index).Controls.Add(rbglbl)

    Good day!

    Thanks


    Michael Sun [MSFT]
    MSDN Community Support | Feedback to us

  • Saturday, April 14, 2012 3:25 AM
     
      Has Code

    ActiveContentX is a Class(Structure) represent Child Forms within MDI Parent Form. its only substitute to mdiparent.ActiveChild which allows to Directly handle Functions and Subs Written on Child Form. e.g. I have 'Tabs' Control on my child form i could not address Tabs through Me.ActiveChild.Tabs but i could address it using ActiveContentX(Curindex).Childform.Tabs

    Public Class childwinform
    'Defination of Active Child Forms, These forms can be DataBound Forms or Simple File form Depends on Filepath and BindingData Property. i.e. if Filepath Property is nt null Form data Can directly be saved as File(according to Savetype= 'Unicode' or 'Default'). if BindingData is not null Form Data can be Added/Edited/Modified into Database.
        Public childform As Docform = New Docform
        Public Index As Integer
        Public XName As String
        Public FilePath As String
        Public InterfaceType As String
        Public BindingData As String
        Public NumberOfPages As Integer
        Public SaveType As String
    
    End Class
    
    Dim ActiveContentX() as childwinform
    

    
    
    
    

    Ravindra Gaikwad

  • Saturday, April 14, 2012 3:47 AM
     
     

    Here it is Debug Screen. Treeview1 is Document Outline for Form. and nnode represents Control in Document


    Ravindra Gaikwad

  • Monday, April 16, 2012 2:26 AM
     
     Answered

    Hi Ravindra,

    From your debugging screen, I think we cannot say that rbglbl is null and it cause the exception.  As I said in my last post, any value of this sentence of codes can cause the NullReferenceException, ActiveContentX(curindex).childform.Tabs.TabPages(TreeView1.SelectedNode.Parent.Index).Controls.Add(rbglbl).  

    When the exception is thrown, please first check the rbglbl variable value in the Watch window (open it through Debug --> Windows --> Watch --> Watch1) 

    If rbglbl is not null, please double check other variables like ActiveContentX(curindex).childform, ActiveContentX(curindex).childform.Tabs, ActiveContentX(curindex).childform.Tabs.TabPages(TreeView1.SelectedNode.Parent.Index), TreeView1.SelectedNode.Parent.Index and ActiveContentX(curindex).childform.Tabs.TabPages(TreeView1.SelectedNode.Parent.Index).Controls.   Any of these variables can lead to the exception. 

    Good day!

    Thanks


    Michael Sun [MSFT]
    MSDN Community Support | Feedback to us

  • Monday, April 16, 2012 4:34 AM
     
     

    Thanks Michael Sun

    Got it. That was my foolish mistake.

    While Working on complex Alorithms and codes, i usually overlook Small things..

    Thanks again.

    

    Ravindra Gaikwad

  • Monday, April 16, 2012 5:18 AM
     
     

    While Working on complex Alorithms and codes, i usually overlook Small things..


    Ravindra Gaikwad

    :)  It often occurs to me as well.  

    Have a nice day!


    Michael Sun [MSFT]
    MSDN Community Support | Feedback to us