问题
I'm currently trying to create a simple game using MonoGame.
The problem I'm currently facing is that sometime(like half of the time) my code runs without problem. But there's time when it throw me this exception:
An unhandled exception of type 'System.ArgumentNullException' occurred in MonoGame.Framework.dll
Additional information: Value cannot be null.
So my question here is how do approach this kind of problems as it doesn't happen all the time?
I've tried cleaning my solution. But sometime it still happens directly after I clean my solution.
The stack trace is in my Draw(); Method at line spriteBatch.Draw();
public void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Begin();
spriteBatch.Draw(InitialTexture, InitialPlatformPosition, InitialPlatformSprite, Color.White);
for (int x = 0; x <= 4; x++)
{
spriteBatch.Draw(TextureArray1[x], PlatformPosition1 + new Vector2(platformWidth * x, 0), PlatformSprite, Color.White);
spriteBatch.Draw(TextureArray2[x], PlatformPosition2 + new Vector2(platformWidth * x, 0), PlatformSprite, Color.White);
}
spriteBatch.End();
}
Sometimes it hit the first line sometime it hit the second.
I'm trying to create an infinity running platform which would have a random choice of type1 or type2. Here's the code in the Update(); Method which is on top of my Draw(); Method in case you need it.
public void Update(GameTime gameTime)
{
int speed = -(int)(gameTime.ElapsedGameTime.TotalSeconds * velocity);
InitialPlatformPosition += new Vector2(speed, 0);
PlatformPosition1 += new Vector2(speed, 0);
PlatformPosition2 += new Vector2(speed, 0);
// Create First Array of Random Texture
if (InitialPlatformPosition.X < -datum1)
{
datum1X++;
datum1 = datum1X * 1920;
for (int x = 0; x <= 4; x++)
{
random.Next(2);
if (random.Next(2) == 0)
PlatformChoice1 = Texture;
else if (random.Next(2) == 1)
PlatformChoice1 = TextureFlipped;
// insert random platform into an array of 10 Texture
TextureArray1[x] = PlatformChoice1;
}
if (datum1X != 1)
{
PlatformPosition1 += new Vector2(1920, 0);
}
}
// Create Second Array of Random Texture
if (InitialPlatformPosition.X < datum2)
{
datum2X++;
datum2 = -(1920 - datum2);
if (datum2X == 1 || datum2X >= 3)
{
for (int x = 0; x <= 4; x++)
{
random.Next(2);
if (random.Next(2) == 0)
PlatformChoice2 = Texture;
else if (random.Next(2) == 1)
PlatformChoice2 = TextureFlipped;
// insert random platform into an array of 10 Texture
TextureArray2[x] = PlatformChoice2;
}
}
if (datum2X >= 3)
{
PlatformPosition2 += new Vector2(1920, 0);
}
}
}
Please try to explain to me in simpler term as I am fairly new to the programming world especially to C# and MonoGame.
来源:https://stackoverflow.com/questions/24672666/an-unhandled-exception-of-type-system-argumentnullexception-occurred-in-monoga