Jump to content
  • Advertisement
Sign in to follow this  
Yoyoyoyo

Object reference not set to an instance...

This topic is 2538 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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..






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);




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
Advertisement
i didnt get what you were saying in your post much, but going by the title, id say your trying to access private members of the class directly?

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;





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();



}







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.e. class, not struct). When you create an array of a reference type, you've got an array of reference to objects, not an actual array of objects. By default, each of those references will be null, not actually pointing to an object.
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
See the "Value Type and Reference Type Arrays" section here.

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
Thanks a lot Hodgman, quite right, i just had to say 'new Parent();' Thankyou for the help, it really is so great to have contact with people who know what they are doing :)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!