Sign in to follow this  
Yoyoyoyo

Object reference not set to an instance...

Recommended Posts

Hi im just trying to tidy up my code and place all the Vertex buffer stuff neatly in a class.


So everything is good up until i move the code over..


[code]



public void LoadBuffers(IndexBuffer iBuffer, int nIndices, VertexBuffer vBuffer, int nVertices, short[] indices, GraphicsDevice device, VertexPositionNormalTexture[] vertices)
{



vBuffer = new VertexBuffer(device, typeof(VertexPositionNormalTexture), nVertices, BufferUsage.WriteOnly);
iBuffer = new IndexBuffer(device, IndexElementSize.SixteenBits, nIndices, BufferUsage.WriteOnly);

vBuffer.SetData<VertexPositionNormalTexture>(vertices);
iBuffer.SetData<short>(indices);

device.Indices = iBuffer;
device.SetVertexBuffer(vBuffer);

[/code]


So it refuses to work, saying object reference not set to an instance of an object.

Ive tried passing in already instanced buffers, but still no joy. All this code works fine placed in the game class, i just wanted to tidy it away in a nice class. Any tips appreciated.

I haven't taken any classes in programming, and it's bound to be a simple c# language stumbling block for me.

Share this post


Link to post
Share on other sites
The problem i was having i think, was using the 'Graphics Device' outside of the game class. I put my method into the game class and its all good.
On another note i am running into a lot of 'reference not set to an instance of an object' errors. Here is an example;


[code]


class CelObj
{
public Parent[] parentFaces;




public void RunSetup()
{
parentFaces = new Parent[6];


for (int x = 0; x < 6; x++)
{

parentFaces[x].BuildMesh();

This is where the error points to. Cannot 'BuildMesh' as object is not set to an instance. But as far as i can see i am
creating a new instance inside the buildmesh method. See below.

}


-------
this is inside the Parent class;

public Mesh mesh;


public void BuildMesh()
{

mesh = new Mesh();
mesh.SetUpMeshes(mesh, width, length, cellSize);
mesh.BuildVerticesTest();
mesh.CreateIndices();
mesh.genNormals();



}



[/code]



I figure, i create an array of Parent, which therefore instantiates each individual 'mesh' in the array, then simply call 'new mesh()' when i run my method.
So as far as i can see, i have declared mesh object, and then before doing anything to it, i declare it as a new mesh. But alas the error is still there.

Share this post


Link to post
Share on other sites
I'm guessing that Parent is a reference type ([i]i.e. class, not struct[/i]). When you create an array of a reference type, you've got an array of reference to objects, [i]not[/i] an actual array of objects. By default, each of those references will be [i]null[/i], not actually pointing to an object.
[code]parentFaces = new Parent[6];//make an array of references
for (int x = 0; x < 6; x++){
parentFaces[x] = new Parent();//make the reference point to a new object
parentFaces[x].BuildMesh();//do something with the new object[/code]See the "Value Type and Reference Type Arrays" section [url="http://msdn.microsoft.com/en-us/library/0a7fscd0.aspx"]here[/url].

Whenever you get this error on a line where you've written "something dot", then it's usually because the thing before the 'dot' is a reference set to null.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this