CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Nov 2002
    Posts
    82

    Object reference not set to an instance of an objec

    Hallo,

    I'm facing a strange error when trying to run a little code that normally must be correct. I port this code from c# to vb.net to get the website collection from a SharePoint Portal server.

    Running the code get the error description that mentions that "Object reference not set to an instance of an object"

    Follow here the code

    Code:
    Imports System
    Imports System.ComponentModel
    Imports System.Web.UI
    Imports System.Web.UI.WebControls
    Imports System.Xml.Serialization
    Imports Microsoft.SharePoint
    Imports Microsoft.SharePoint.SPWeb
    Imports Microsoft.SharePoint.Utilities
    Imports Microsoft.SharePoint.WebPartPages
    Imports System.Web.UI.HtmlControls
    Imports Microsoft.SharePoint.WebControls
    Imports System.Object
    
    
        Private Sub sCompileTrail(ByVal objWeb As Microsoft.SharePoint.SPWeb)
            If strTrail.Trim = vbNullString Then
                If objWeb.IsRootWeb  Then
                    strTrail = objWeb.Title
                Else
                    strTrail = objWeb.Name
                End If
            Else
                If objWeb.IsRootWeb Then
                    strTrail = "<a href=" & objWeb.Url & ">" & objWeb.Title & "</a> " & strTrail
                Else
                    strTrail = "<a href=" & objWeb.Url & objWeb.Url.ToString & ">" & objWeb.Name & "</a> " & strTrail
                End If
            End If
    
            If objWeb.ParentWeb.ToString <> vbNullString Then
                sCompileTrail(objWeb.ParentWeb)
            End If
        End Sub
    
    
    
        Public Function fGetWebTree() As String
    
            Try
                Dim objWeb As SPWeb = Microsoft.SharePoint.WebControls.SPControl.GetContextWeb(Context)
                sCompileTrail(objWeb)
    
                strTrail = strTrail & "1" & vbCrLf
                If (objWeb.PortalUrl.Trim <> vbNullString) And (objWeb.PortalName.Trim <> vbNullString) Then
                    strTrail = "<a href=" & objWeb.PortalUrl & ">" & objWeb.PortalName() & "</a> " & strTrail
                End If
            Catch ex As Exception
                strTrail = "Error! " & ex.Message
            End Try
    
            fGetWebTree = strTrail
        End Function
    the error occures on the "If objWeb.IsRootWeb" check from the sCompileTrail function.


    Thanks in advance for your help
    james

  2. #2
    Join Date
    Feb 2001
    Location
    Stamford CT USA
    Posts
    2,167

    Re: Object reference not set to an instance of an objec

    That seems to indicate that the line:
    Dim objWeb As SPWeb = Microsoft.SharePoint.WebControls.SPControl.GetContextWeb(Context)
    did not return anything and this is pretty specific to SHAREPOINT. Any SHAREPOINT forum that you can post to?

    -Cool Bizs
    Good Luck,
    -Cool Bizs

  3. #3
    Join Date
    Nov 2002
    Posts
    82

    Re: Object reference not set to an instance of an objec

    Thanks a lot coolbiz,

    Your response is the correct answer.
    the Dim objWeb As SPWeb = Microsoft.SharePoint.WebControls.SPControl.GetContextWeb(Context)
    did not return anything because of the security access setting.
    I just change the way to register the Web Part assembly as SafeControl and it works.

    Thanks once again
    james

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured