Textbox.Text returns null value no matter what. I only need to get user input on the textbox

38 Views Asked by At

I'm using Avalonia and I'm just trying to retrieve some text from a Textbox element. In the debugging it shows

System.NullReferenceException: Object reference not set to an instance of an object.
   at recursosHumanos.Views.CatalogoCursos.BotonGrabar_Click(Object sender, RoutedEventArgs e) in /home/isaim/Documentos/Ingeniería de Software/recursosHumanos/recursosHumanos/Views/CatalogoCursos.axaml.cs:line 28
   at Avalonia.Interactivity.Interactive.<AddHandler>g__InvokeAdapter|4_0[TEventArgs](Delegate baseHandler, Object sender, RoutedEventArgs args)
   at Avalonia.Interactivity.Interactive.<>c__4`1.<AddHandler>b__4_1(Delegate baseHandler, Object sender, RoutedEventArgs args)
   at Avalonia.Interactivity.EventRoute.RaiseEventImpl(RoutedEventArgs e)
   at Avalonia.Interactivity.EventRoute.RaiseEvent(Interactive source, RoutedEventArgs e)
   at Avalonia.Interactivity.Interactive.RaiseEvent(RoutedEventArgs e)
   at Avalonia.Controls.Button.OnClick()
   at Avalonia.Controls.Button.OnPointerReleased(PointerReleasedEventArgs e)
   at Avalonia.Input.InputElement.<>c.<.cctor>b__32_9(InputElement x, PointerReleasedEventArgs e)
   at Avalonia.Interactivity.RoutedEvent`1.<>c__DisplayClass1_0`1.<AddClassHandler>g__Adapter|0(Object sender, RoutedEventArgs e)
   at Avalonia.Interactivity.RoutedEvent.<>c__DisplayClass23_0.<AddClassHandler>b__0(ValueTuple`2 args)
   at Avalonia.Reactive.AnonymousObserver`1.OnNext(T value)
   at Avalonia.Reactive.LightweightObservableBase`1.PublishNext(T value)
   at Avalonia.Reactive.LightweightSubject`1.OnNext(T value)
   at Avalonia.Interactivity.RoutedEvent.InvokeRaised(Object sender, RoutedEventArgs e)
   at Avalonia.Interactivity.EventRoute.RaiseEventImpl(RoutedEventArgs e)
   at Avalonia.Interactivity.EventRoute.RaiseEvent(Interactive source, RoutedEventArgs e)
   at Avalonia.Interactivity.Interactive.RaiseEvent(RoutedEventArgs e)
   at Avalonia.Input.MouseDevice.MouseUp(IMouseDevice device, UInt64 timestamp, IInputRoot root, Point p, PointerPointProperties props, KeyModifiers inputModifiers, IInputElement hitTest)
   at Avalonia.Input.MouseDevice.ProcessRawEvent(RawPointerEventArgs e)
   at Avalonia.Input.MouseDevice.ProcessRawEvent(RawInputEventArgs e)
   at Avalonia.Input.InputManager.ProcessInput(RawInputEventArgs e)
   at Avalonia.Controls.TopLevel.HandleInput(RawInputEventArgs e)
   at Avalonia.X11.X11Window.DispatchInput(RawInputEventArgs args)
   at Avalonia.RawEventGrouper.Dispatch(RawInputEventArgs ev)
   at Avalonia.ManualRawEventGrouperDispatchQueue.DispatchNext()
   at Avalonia.X11.X11PlatformThreading.RunLoop(CancellationToken cancellationToken)
   at Avalonia.Threading.DispatcherFrame.Run(IControlledDispatcherImpl impl)
   at Avalonia.Threading.Dispatcher.PushFrame(DispatcherFrame frame)
   at Avalonia.Threading.Dispatcher.MainLoop(CancellationToken cancellationToken)
   at Avalonia.Controls.ApplicationLifetimes.ClassicDesktopStyleApplicationLifetime.Start(String[] args)
   at Avalonia.ClassicDesktopStyleApplicationLifetimeExtensions.StartWithClassicDesktopLifetime(AppBuilder builder, String[] args, Action`1 lifetimeBuilder)
   at recursosHumanos.Program.Main(String[] args) in /home/isaim/Documentos/Ingeniería de Software/recursosHumanos/recursosHumanos/Program.cs:line 14

The problem occurs in this method

public void BotonGrabar_Click(object sender, RoutedEventArgs e)
    {
        List<string> columnas = new List<string>();
        List<string> datos = new List<string>(); 
        columnas.Add("Id_Cursos");
        columnas.Add("NombreCursos");
        datos.Add(IdCursoCampo.Text); <--- Right here, IdCursoCampo returns null value
        datos.Add(CursoCampo.Text);
        ConexionSql conexion = new ConexionSql();
        conexion.GrabarBd("Cursos", columnas, datos);
    }

I don't think I need to bind anything because I just want to get a simple text value from this textbox.
My XAML file looks like this (at least the important part)

         <Grid>
            <StackPanel HorizontalAlignment="Stretch"
                        Margin="10">
                <Label>NoControl</Label>
                <TextBox MinWidth="300"
                         HorizontalAlignment="Left"
                         x:Name="IdCursoCampo"></TextBox>
                <Label>Curso</Label>
                <TextBox MinWidth="300"
                         HorizontalAlignment="Left"
                         x:Name="CursoCampo"></TextBox>
            </StackPanel>
        </Grid>

I've looked everywhere but apparently the IdCursoCampo.Text way should work ok. I don't get what I'm doing different from everyone else.

0

There are 0 best solutions below