device NullReferenceExceptions

Poncho843

Newcomer
Joined
Nov 4, 2004
Messages
12
Location
Daventry, England
Hi all, was wondering if any of you guys have had this problem before and know of a solution. I keep losing my device and i don't know where it's going. Here is the problem code:

Code:
    Public Function createMesh(ByVal D3DDev As Device, ByRef mesh As Mesh, ByRef meshmaterials() As Material, ByRef meshtextures() As Texture) As Boolean
         Dim i As Integer, j As Integer, v As ms3d_vertex_t
        mesh = New Mesh(nNumTriangles, nNumVertices, MeshFlags.Managed, CustomVertex.PositionNormalTextured.Format, D3DDev)
        Dim vb As VertexBuffer = mesh.VertexBuffer
        Dim data As GraphicsStream = vb.Lock(0, 0, LockFlags.None)
        '        For i = 0 To nNumVertices - 1
        '        v = vertices(i)
        '            data.Write(new CustomVertex.PositionNormalTextured(new Vector3(v.vertex(0),v.vertex(1),v.vertex(2)),
        '        data.Write(New CustomVertex.PositionColored(v.vertex(0), v.vertex(1), v.vertex(2), &HFF))
        '        Debug.WriteLine(v.vertex(0) & ":" & v.vertex(1) & ":" & v.vertex(2))
        '        Next

        Dim t As ms3d_triangle_t
        For i = 0 To nNumTriangles - 1
            t = triangles(i)
            For j = 0 To 2
                v = vertices(CInt(t.vertexIndices(j).ToString))
                Dim pos As Vector3 = New Vector3(v.vertex(0), v.vertex(1), v.vertex(2))
                Dim nor As Vector3 = New Vector3(t.vertexNormals(j, 0), t.vertexNormals(j, 1), t.vertexNormals(j, 2))
                data.Write(New CustomVertex.PositionNormalTextured(pos, nor, t.s(j), t.t(j)))
            Next
        Next

        vb.Unlock()
        Dim ib As IndexBuffer = mesh.IndexBuffer
        Dim indices(nNumTriangles * 3 - 1) As Short
        For i = 0 To nNumTriangles - 1
            '            indices(i * 3) = CShort(triangles(i).vertexIndices(0).ToString)
            '            indices(i * 3 + 1) = CShort(triangles(i).vertexIndices(1).ToString)
            '            indices(i * 3 + 2) = CShort(triangles(i).vertexIndices(2).ToString)
            indices(i * 3) = CShort(i * 3)
            indices(i * 3 + 1) = CShort(i * 3 + 1)
            indices(i * 3 + 2) = CShort(i * 3 + 2)
        Next
        ib.SetData(indices, 0, LockFlags.None)
        'mesh.ComputeNormals()

        ReDim meshmaterials(nNumMaterials - 1)
        ReDim meshtextures(nNumMaterials - 1)
        For i = 0 To nNumMaterials - 1
            meshmaterials(i).Ambient = Color.FromArgb(materials(i).ambient(3), materials(i).ambient(0), materials(i).ambient(1), materials(i).ambient(2))
            meshmaterials(i).Diffuse = Color.FromArgb(materials(i).diffuse(3), materials(i).diffuse(0), materials(i).diffuse(1), materials(i).diffuse(2))
            meshmaterials(i).Emissive = Color.FromArgb(materials(i).emissive(3), materials(i).emissive(0), materials(i).emissive(1), materials(i).emissive(2))
            meshmaterials(i).Specular = Color.FromArgb(materials(i).specular(3), materials(i).specular(0), materials(i).specular(1), materials(i).specular(2))
            meshtextures(i) = TextureLoader.FromFile(D3DDev, materials(i).texture)
        Next


    End Function

on the last line meshtextures(i) = TextureLoader....... i get the error

An unhandled exception of type 'System.NullReferenceException' occured in microsoft.directx.direct3dx.dll - Additional information: Object reference not set to an instance of an object.

both the materials object and the device object appear ok in the watch pane, so i am bewildered about what is null referenced. If i take this line out the error occurs on the next reference to D3DDev further in the app, but if i take the reference to my createMesh function out completely, i don't

The problem started when i changed the mesh vertex format from Position to PositionNormalTextured and associated code changes (commented out above), but that was the only change made before the errors started.

Any ideas would be greatly received.
 
Last edited:
Don't worry about it. Missed a factor of 3 out on numVertices. You can spend 6 hours looking at it and not notice, and the second you post it to a forum it comes to you.

Sorry guys
 
Back
Top