pass.Apply() null reference exception

What could cause a null reference exception when calling pass.Apply()?

The effect exists, I set the parameters, when debugging it displays the name of current technique and array with passes has 1 element.

It was working with BasicEffect, but now I have tried adding custom one.

Shader code:


DId you try removing the double forward slashes in the shader code where the pass is defined?

so instead “pass//Pass0” try “pass Pass0” there…

Yes, it was originally with Pass0 and didn’t work.

pass is != null but it still throws nullreference when calling Apply, and I can’t seem to find single mention of this issue on google

Which version of MonoGame are you on?

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace Game1
    public class Game1 : Game
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        Effect effect;
        Matrix world;
        Matrix view;
        Matrix projection;
        IndexBuffer indexBuffer;
        VertexBuffer vertexBuffer;
        VertexPositionColorTexture[] vertices;
        int[] indices;
        Texture2D texture;

        public Game1()
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";

        protected override void Initialize()

        protected override void LoadContent()
            spriteBatch = new SpriteBatch(GraphicsDevice);

            effect = Content.Load<Effect>("File");
            texture = Content.Load<Texture2D>("texture");

            vertexBuffer = new VertexBuffer(GraphicsDevice, VertexPositionColorTexture.VertexDeclaration, 4, BufferUsage.WriteOnly);
            indexBuffer = new IndexBuffer(GraphicsDevice, IndexElementSize.ThirtyTwoBits, 6, BufferUsage.WriteOnly);

            vertices = new VertexPositionColorTexture[4];
            indices = new int[6];

            vertices[0] = new VertexPositionColorTexture(new Vector3(-5, 5, -1.2f), Color.Red, new Vector2(0, 0));
            vertices[1] = new VertexPositionColorTexture(new Vector3(5, 5, -1.2f), Color.Green, new Vector2(1, 0));
            vertices[2] = new VertexPositionColorTexture(new Vector3(5, -5, -1.2f),Color.Green, new Vector2(1, 1));
            vertices[3] = new VertexPositionColorTexture(new Vector3(-5, -5, -1.2f), Color.Red, new Vector2(0, 1));

            indices[0] = 0;
            indices[1] = 3;
            indices[2] = 2;
            indices[3] = 2;
            indices[4] = 1;
            indices[5] = 0;


            world = Matrix.Identity;
            view = Matrix.CreateLookAt(Vector3.Zero, Vector3.Forward, Vector3.Up);
            projection = Matrix.CreatePerspective(10.0f * GraphicsDevice.Viewport.AspectRatio, 10.0f, 1.0f, 1000.0f);

        protected override void UnloadContent()

        protected override void Update(GameTime gameTime)
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))


        protected override void Draw(GameTime gameTime)

            GraphicsDevice.RasterizerState = RasterizerState.CullNone;

            GraphicsDevice.Indices = indexBuffer;


            foreach (EffectPass pass in effect.CurrentTechnique.Passes)

                GraphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, indices.Length / 3);

    #define VS_SHADERMODEL vs_3_0
    #define PS_SHADERMODEL ps_3_0
    #define VS_SHADERMODEL vs_4_0_level_9_1
    #define PS_SHADERMODEL ps_4_0_level_9_1

float4x4 World;
float4x4 View;
float4x4 Projection;
texture Texture;
sampler TextureSampler = sampler_state
    texture = <Texture>;
    magfilter = LINEAR;
    minfilter = LINEAR;
    mipfilter = LINEAR;
    AddressU = mirror;
    AddressV = mirror;
struct VertexToPixel
    float4 Position : POSITION;
    float4 Color : COLOR0;
    float2 TexCoords : TEXCOORD0;
struct PixelToFrame
    float4 Color : COLOR0;
VertexToPixel SimplestVertexShader(float4 inPos : POSITION, float4 color : COLOR, float2 inTexCoords : TEXCOORD0)
    VertexToPixel Output = (VertexToPixel) 0;
    float4 worldPosition = mul(inPos, World);
    float4 viewPosition = mul(worldPosition, View);
    Output.Position = mul(viewPosition, Projection);

    Output.Color = color;

    Output.TexCoords = inTexCoords;
    return Output;
PixelToFrame OurFirstPixelShader(VertexToPixel PSIn)
    PixelToFrame Output = (PixelToFrame) 0;
    Output.Color = PSIn.Color * tex2D(TextureSampler, PSIn.TexCoords);
    return Output;
technique Default
    pass Pass0
        VertexShader = compile VS_SHADERMODEL SimplestVertexShader();
        PixelShader = compile PS_SHADERMODEL OurFirstPixelShader();

I had no time yesterday to post anything but a working example based on the code you provided. I guess you should be able to see from the example code what things needed to change to make it work. Hope it helps you to get started.