I have an array of TextureBrush() which I call upon to fill a rectangle. However, I am coming up against an error:

System.ArgumentNullException was unhandled
  Message=Value cannot be null.
Parameter name: brush
       at System.Drawing.Graphics.FillRectangle(Brush brush, Int32 x, Int32 y, Int32 width, Int32 height)
       at System.Drawing.Graphics.FillRectangle(Brush brush, Rectangle rect)
So my question: Is it not possible to fill a rectangle with a texture brush?


'Sub to load all the textures called in the onLoad event. 

Private Sub loadTextures()
            Dim Junk1 As Rectangle = New Rectangle(0, 0, 100, 100)
            Dim junkTex1 As TextureBrush = New TextureBrush(junkSprite, Junk1)
            Dim planetRect As Rectangle = New Rectangle(300, 300, 200, 200)
            Dim planetCirc As Bitmap = planet.Clone(planetRect, Imaging.PixelFormat.DontCare)
            Dim tPlanetGreenBrush As TextureBrush = New TextureBrush(planetCirc)
            Dim Junk2 As Rectangle = New Rectangle(100, 0, 100, 100)
            Dim junkTex2 As TextureBrush = New TextureBrush(junkSprite, Junk2)
            Dim Junk3 As Rectangle = New Rectangle(200, 0, 100, 100)
            Dim junkTex3 As TextureBrush = New TextureBrush(junkSprite, Junk3)
            Dim Junk4 As Rectangle = New Rectangle(300, 0, 100, 100)
            Dim junkTex4 As TextureBrush = New TextureBrush(junkSprite, Junk4)
            Dim Junk5 As Rectangle = New Rectangle(0, 100, 100, 100)
            Dim junkTex5 As TextureBrush = New TextureBrush(junkSprite, Junk5)
            Dim Junk6 As Rectangle = New Rectangle(100, 100, 100, 100)
            Dim junkTex6 As TextureBrush = New TextureBrush(junkSprite, Junk6)
            Dim Junk7 As Rectangle = New Rectangle(200, 100, 100, 100)
            Dim junkTex7 As TextureBrush = New TextureBrush(junkSprite, Junk7)
            Dim Junk8 As Rectangle = New Rectangle(300, 100, 100, 100)
            Dim junkTex8 As TextureBrush = New TextureBrush(junkSprite, Junk8)

            tBrushArray(0) = Nothing
            tBrushArray(1) = junkTex1
            tBrushArray(2) = junkTex2
            tBrushArray(3) = junkTex3
            tBrushArray(4) = junkTex4
            tBrushArray(5) = junkTex5
            tBrushArray(6) = junkTex6
            tBrushArray(7) = junkTex7
            tBrushArray(8) = junkTex8
            tBrushArray(9) = Nothing
            tBrushArray(10) = Nothing
            tBrushArray(11) = Nothing
            tBrushArray(12) = Nothing
            tBrushArray(13) = Nothing
            tBrushArray(14) = Nothing
            tBrushArray(15) = Nothing
            tBrushArray(16) = Nothing
            tBrushArray(17) = Nothing
            tBrushArray(18) = Nothing
            tBrushArray(19) = Nothing

        End Sub

'The error is being thrown on line 7. This is executed in the onPaint() event. 

        For a = 0 To 4
            Dim imageID As Int16 = Junk(a, 0)
            If Junk(a, 0) > 0 Then
                g.Graphics.FillRectangle(tBrushArray(imageID), tJunk(a))
            End If

Any help or suggestions would be welcomed.

Err is in this line:

g.Graphics.FillRectangle(tBrushArray(imageID), tJunk(a))

Many of the elements (indices: 0, and 9 thru 19) in tBrushArray are set to Nothing (null).

So if imageID equals one of those indices, you are passing a null (VB Nothing) brush like your error message indicates is the problem.

Would this affect the code even if it was passing indicies 1 thru 8? (0 and 9 thru 19) will be used at a later stage. Presently though the code should only want to call upon 1 thru 8. If that makes any sense.

try changing you code like this to verify that that the brush is not null

        For a = 0 To 4
            Dim imageID As Int16 = Junk(a, 0)
            If tBrushArray(imageID) Is Nothing Then Stop
            If Junk(a, 0) > 0 Then
                g.Graphics.FillRectangle(tBrushArray(imageID), tJunk(a))
            End If

This will cause the debugger to stop if the brush is null and then you can inspect the variables to see what is going on. If it does not stop then something else is going on.

Cheers TnTinMN I will give that a go now.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.