(VB.NET) Have a DLL with a single function. Calling this function throws a nullreferenceexception and I can't understand what causes it

StackOverflow https://stackoverflow.com/questions/22768746

Pergunta

This is the code that calls the function:

Imports Peasantlish

    Public Class main
        Dim TranslateLib As Peasantlish.PeasantlishLib
        Private Sub Translate()
            Label2.Text = "Translator - working"
            TextBox1.Text = TranslateLib.TranslateToEng(TextBox2.Text)
        End Sub

TextBox2 is the textbox that contains the text to translate. TextBox1 contains the result.

And this is the function itself:

Imports System.Text.RegularExpressions

Public Class PeasantlishLib
    Public Function TranslateToEng(ByVal input As String)
        Dim output As String = "test string please ignore"
        output = input
        output = Regex.Replace(output, "full hd", "720p", RegexOptions.IgnoreCase)
        output = Regex.Replace(output, "fullhd", "720p", RegexOptions.IgnoreCase)
        output = Regex.Replace(output, "xbox live", "free service that I paid for", RegexOptions.IgnoreCase)
        output = Regex.Replace(output, "\bxbl\b", "free service that I paid for", RegexOptions.IgnoreCase)
        ' next are single words after phrases
        output = Regex.Replace(output, "\bhd\b", "720p", RegexOptions.IgnoreCase)
        output = Regex.Replace(output, "next gen", "already-was-on-pc-3-years-ago gen", RegexOptions.IgnoreCase)
        output = Regex.Replace(output, "censored", "bundle of sticks", RegexOptions.IgnoreCase)
        output = Regex.Replace(output, "gddr5", "high latency RAM", RegexOptions.IgnoreCase)
        output = Regex.Replace(output, "esram", "level 4 cache", RegexOptions.IgnoreCase)
        output = Regex.Replace(output, "Controller", "poo-ey excuse for an output device", RegexOptions.IgnoreCase)
        output = Regex.Replace(output, "console", "potato", RegexOptions.IgnoreCase)
        output = Regex.Replace(output, "cloud", "butt", RegexOptions.IgnoreCase)
        output = Regex.Replace(output, "optimization", "ultra low settings", RegexOptions.IgnoreCase)
        output = Regex.Replace(output, "optimized", "with ultra low settings", RegexOptions.IgnoreCase)
        ' and a bunch more of these
        Return output
    End Function
End Class

It throws the exception at the TextBox1.Text = TranslateLib.TranslateToEng(TextBox1.Text) line. But I don't see anything wrong there. It worked fine before I migrated the bunch of Regex.Replace's to a .dll for tidiness purposes. Before the output variable was TextBox1.Text

Foi útil?

Solução

Did you instantiate TranslateLib? My VB is a bit rusty, but I thought you needed the New keyword to instantiate a new object:

Dim TranslateLib As New Peasantlish.PeasantlishLib

You can confirm this in your debugger by seeing which object is Nothing (or null). If it's TranslateLib then you never instantiated it.

Outras dicas

You need to instantiate your TranslateLib before using it:

Dim TranslateLib As Peasantlish.PeasantlishLib
...
TranslateLib = New Peasantlish.PeasantlishLib
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top