0

I making the game 2048 but as an extra I wanted the users to let them change the height of the square if they wanted to. If the users click on the 'btnSquare' button a inputbox will show up where they can fill in the height of the square. Their input will then be shown at the 'txtSquare' textbox right under the button which is already filled in with 4. The height of the square will then change to the text in that textbox. Now when I try to run this I get an System.Nullreference.Exception but I have no idea why since the textbox is initially filled with a 4. Debugger says this: Object reference not set to an instance of an object.

And also I am a very newbie at Visual Basic so I know that this method of mine might not work. If someone could help me and tell me what method is best to implement a height changer of the board to the game, I would be very thankful. PS: I'm Dutch so I'm sorry for the different language in the code.

Public Class Form1


Public square As Integer = (Convert.ToInt32(txtSquare.Text) - 1)

Dim rooster(square, square) As Label
Dim oldrooster(square, square) As Label

Public clickEnabled As Boolean = False

Public Sub btnOrde_Click(sender As Object, e As EventArgs) Handles btnSquare.Click
    Dim message, title As String
    Dim defaultValue As Integer
    Dim userInput As Object
    ' Set prompt.
    message = "Geef de hoogte van je spelbord in. (vb. 4 voor een 4x4 vierkant)"
    ' Set title.
    title = "Initialisatie spelbord"
    defaultValue = 4   ' Set default value.

    ' Display message, title, and default value.
    userInput = InputBox(message, title, defaultValue)
    ' If user has clicked Cancel, set myValue to defaultValue 
    If userInput = "" Then userInput = defaultValue
    If Not userInput > 1 Then userInput = defaultValue
    square = (Convert.ToInt32(userInput) - 1)
    txtSquare.Text = Convert.ToInt32(userInput)

End Sub
Ayk96
  • 49
  • 1
  • 7
  • possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) l it would help if you told us WHERE it happens; [ask] – Ňɏssa Pøngjǣrdenlarp Nov 02 '14 at 01:08
  • 1
    see [this answer](http://stackoverflow.com/a/26164755/1070452); `Public square As Integer = (Convert.ToInt32(txtSquare.Text) - 1)` you are referencing a control before it has been created – Ňɏssa Pøngjǣrdenlarp Nov 02 '14 at 01:12
  • Hi Plutonix, Thanks for answering, I initialized txtSquare.Text = "4" and I put square = (Convert.ToInt32(txtSquare.Text) - 1) in Form1_load. Now I have no error, but it creates only 1 tile instead of 16. If I debug it it says that the value of square = 3 so I have no idea why it's creating only 1 tile, do you maybe know why? Thanks!! – Ayk96 Nov 02 '14 at 02:00
  • Not related to your question, but `square` and `clickEnabled` are fields and shouldn't be exposed as public - use a property to expose the fields, if you need them accessible from other classes. – Tim Nov 02 '14 at 02:31

1 Answers1

1

The reason that you get only one tile is that the arrays are instantiated before Form_Load when square is still 0. You have to ReDim rooster and oldrooster when the value of square changes.

Example:

ReDim rooster(square, square)
ReDim oldrooster(square, square)

Note: ReDim Preserve won't work in your case, because it is only allowed, if you only change the size of the last dimension and you are changing both. Therefore all elements in your arrays will be lost and you have to create them again.

Karsten
  • 1,624
  • 1
  • 14
  • 29