Hi guys,

So I'm making a random sentence generator that takes subjects, predicates, etc. from user input, stores them, and then randomly chooses one value from each category and prints it in a textbox. Or at least, that's how it's supposed to work. But I just can't seem to get the text to print in the textbox. Everything compiles fine, but when I click generate, Windows gives an error.

Since I have barely any time, for it is a project at an enrichment course that ends in 2 days, I've been trying everything I could without taking too long.

Here's the code:
`Public Class Form1

Dim list1, list2, list3 As String

Public Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    Dim subjectWords, predicateWords, complementWords As List(Of String)

    subjectWords.Add(TextBox1.Text)
    predicateWords.Add(TextBox2.Text)
    complementWords.Add(TextBox3.Text)

    list1 = String.Join(",", subjectWords.ToArray)
    list2 = String.Join(",", predicateWords.ToArray)
    list3 = String.Join(",", complementWords.ToArray)
End Sub

Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click

    TextBox4.Text = list1 & " " & list2 & " " & list3 & "."
End Sub
End Class`

Since I can't exactly show you guys the GUI (partly because it's in French and partly because it's not my computer), that's all I can give for now.

Recommended Answers

All 7 Replies

If I understand what you are wanting, you will want do something like this:

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    TextBox4.Text = subjectWords(Get_Random(0, subjectWords.Count - 1)) & " " & predicateWords(Get_Random(0, predicateWords.Count - 1)) & " " & _
    complementWords(Get_Random(0, complementWords.Count - 1)) & "."
End Sub

Public Function Get_Random(ByVal Low As Integer, ByVal High As Integer) As Integer
    Dim rand As Integer = Math.Floor((High - Low + 1) * Rnd() + Low)
    Return rand
End Function

Thank you so much! Still haven't tried it yet, but I can see where I went wrong... So horribly wrong -_-. (It's been a while since I last used Visual Basic .NET).

Sorry, but it didn't work, as both buttons, when pressed, still gave an error.

`Public Class Form1

Dim list1, list2, list3 As String
Dim subjectWords, predicateWords, complementWords As List(Of String)

Public Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click

    subjectWords.Add(TextBox1.Text)
    predicateWords.Add(TextBox2.Text)
    complementWords.Add(TextBox3.Text)

    list1 = String.Join(",", subjectWords.ToArray)
    list2 = String.Join(",", predicateWords.ToArray)
    list3 = String.Join(",", complementWords.ToArray)
End Sub

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    TextBox4.Text = subjectWords(Get_Random(0, subjectWords.Count - 1)) & " " & predicateWords(Get_Random(0, predicateWords.Count - 1)) & " " & _
    complementWords(Get_Random(0, complementWords.Count - 1)) & "."
End Sub

Public Function Get_Random(ByVal Low As Integer, ByVal High As Integer) As Integer
    Dim rand As Integer = Math.Floor((High - Low + 1) * Rnd() + Low)
    Return rand
End Function
End Class`

Let me describe the design:
- Three text boxes where you input one subject, one predicate, and one complement at a time (Textbox1, Textbox2, Textbox3).
- Under that is the button that stores the text in the three text boxes in the memory, then clears the boxes (Button2).
- Under that, there is (Textbox4), which displays the randomly chosen subject, predicate, and complement from the memory.
- And finally, (Button1), which picks one randomly chosen subject, predicate, and complement from the memory, and prints it to the (Textbox4).

Removed the list1-3 variables (they were useless), and fixed a few things, but now Visual Studio 2010 (not Windows) gives me a "NullReferenceException was unhandled". Any ideas (I'm reaaaally rusty it seems!)?

`Public Class Form1

Dim subjectWords, predicateWords, complementWords As List(Of String)

Public Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
    subjectWords.Add(TextBox1.Text)
    predicateWords.Add(TextBox2.Text)
    complementWords.Add(TextBox3.Text)
End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    TextBox4.Text = subjectWords(Get_Random(0, subjectWords.Count - 1)) & " " & predicateWords(Get_Random(0, predicateWords.Count - 1)) & " " & _
    complementWords(Get_Random(0, complementWords.Count - 1)) & "."
End Sub

Public Function Get_Random(ByVal Low As Integer, ByVal High As Integer) As Integer
    Dim rand As Integer = Math.Floor((High - Low + 1) * Rnd() + Low)
    Return rand
End Function
End Class`

Where is the NullReferenceException pointing to in the stack trace?

AHA! It's because I forgot to declare a new instance of all the lists of strings before assigning them a string. I changed a few other things too.

Public Class MainForm
    Dim subjectWords, predicateWords, complementWords As List(Of String)

    Private Sub MainForm_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        subjectWords = New List(Of String)()
        predicateWords = New List(Of String)()
        complementWords = New List(Of String)()
    End Sub

    Private Sub Ajouter_Click(sender As System.Object, e As System.EventArgs) Handles Ajouter.Click
        If TextBox1.Text.Length > 0 Then
            subjectWords.Add(TextBox1.Text)
        End If
        If TextBox2.Text.Length > 0 Then
            predicateWords.Add(TextBox2.Text)
        End If
        If TextBox3.Text.Length > 0 Then
            complementWords.Add(TextBox3.Text)
        End If

        TextBox1.Text = ""
        TextBox2.Text = ""
        TextBox3.Text = ""
    End Sub

    Private Sub Generer_Click(sender As Object, e As EventArgs) Handles Generer.Click
        TextBox4.Text = subjectWords(Get_Random(0, subjectWords.Count - 1)) & " " & predicateWords(Get_Random(0, predicateWords.Count - 1)) & " " & _
        complementWords(Get_Random(0, complementWords.Count - 1)) & "."
    End Sub

    Private Sub WordDatabaseToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles WordDatabaseToolStripMenuItem.Click
        Dim list1, list2, list3 As String

        list1 = String.Join(vbCrLf, subjectWords)
        list2 = String.Join(vbCrLf, predicateWords)
        list3 = String.Join(vbCrLf, complementWords)

        RichTextBox1.Text = list1
        RichTextBox2.Text = list2
        RichTextBox3.Text = list3
    End Sub

    Private Sub ExitToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles ExitToolStripMenuItem.Click
        Close()
    End Sub

    Public Function Get_Random(ByVal Low As Integer, ByVal High As Integer) As Integer
        Dim random As Integer
        random = Math.Floor((High - Low + 1) * Rnd() + Low)
        Return random
    End Function
End Class

Now it's my vision of perfect. Thanks for the help!

Np friend, anything else we can help you with?

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.