Click here to Skip to main content
15,883,901 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

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 'Verander spelbord' button a inputbox will show up where they can fill in the height of the square. Their input will then be shown at the 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 to the game, I would be very thankful. PS: I'm Dutch so I'm sorry for the different language in the code and designer.

VB
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
        'Bericht op scherm.
        message = "Geef de hoogte van je spelbord in. (vb. 4 voor een 4x4 vierkant)"
        'Titel scherm.
        title = "Initialisatie spelbord"
        defaultValue = 4   'Standaardwaarde.

        ' Geef bericht, titel, en standaardwaarde.
        userInput = InputBox(message, title, defaultValue)
        'Als er wordt geannuleerd of als input kleiner is dan 2, dan blijft de hooghte de standaardwaarde (4). 
        If userInput = "" Then userInput = defaultValue
        If Not userInput > 1 Then userInput = defaultValue
        square = (Convert.ToInt32(userInput) - 1)
        txtSquare.Text = Convert.ToInt32(userInput)

    End Sub



[1]: http://i.stack.imgur.com/iQ0rc.png
[2]: http://i.stack.imgur.com/ooq3c.png
Posted
Updated 1-Nov-14 15:20pm
v2
Comments
Garth J Lancaster 1-Nov-14 21:09pm    
mate - the 1st link - ok, if you really must ... the second ? that code should be posted here and formatted correctly, rather than asking people to go offsite to the link - please edit your question and add the code - it makes it easier for people to try and help you
Garth J Lancaster 1-Nov-14 21:15pm    
btw - Im wondering if your form is actually loaded at the point you're trying to reference txtSquare.Text - why don't you try splitting it up, leaving the define ie [Public square as Integer] where it is, and moving the [Convert.ToInt32(txtSquare.Text) - 1] into the Form1.Load section as square = ...
Member 11198864 1-Nov-14 21:31pm    
Hi Garth, I'm sorry that I posted a picture first, but I have to make this for school and I thought that they might accuse me of plagiarism if this code showed up in Google. But I did what you told me and I have no error know, but know I see only 1 tile instead of 16, so it still sees the txtSquare.Text as 0 while it has the value 4 in it. And I think that the form loads before the reference to txtSquare.Text. What could I do to let it change the board at any time? Thank you Garth!!
ledtech3 2-Nov-14 14:07pm    
By the looks of your second screen shot it appears that the null reference is that it did not pick up the value of the text box.
and mabey something like this could help.

Public Square as integer
If txtsquare.text = nothing then
msgbox("No Number Added")
exit sub
elseif isnumeric(txtsquare.text) = false then
msgbox("Input is not a number")
exit sub
else
square = txtSquare.Text
endif

Then step thru the code in the debugger to verify what square is equal to
I'm not sure why you are subtracting 1
Member 11198864 2-Nov-14 16:48pm    
Hi, thanks for your answer but I already solved it . I did what Garth J Lancaster told me and I used Redim after it and know it's working. I subtract 1 because if the user wants a 4x4 square and they enter 4, you have to initialize your board as Dim board(3,3) as Label, 3 because it includes 0 and 3 so you have 4 rows and columns.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900