C Newbie: Getting Gtk-Critical Errors

I’m using the GTK4 library on Linux, but I’m getting errors saying: (gtkbuilder:28576): Gtk-CRITICAL **: 23:00:17.527: gtk_label_set_text: assertion 'GTK_IS_LABEL (self)' failed when button1 or button2

My goal is when one of the buttons is pressed, it changes label1 . I’m pretty new to C, so I’m probably making an obvious mistake. Is anyone able to point out a mistake I could have made?

I probably should note that I took an example I found on the GTK documentation and started modifying it.

Thanks!

Code:

#include <gtk/gtk.h>
#include <glib/gstdio.h>

//Define objects
//static GtkBuilder *builder;
//static GObject *window;

/*"hello_world" function
static void
print_hello (GObject *label, GtkBuilder  *builder, GtkWidget *widget, gpointer data)
{
  g_print ("Hello World\n");
  
  GObject *label = gtk_builder_get_object(builder, "output1");
  gtk_label_set_text (GTK_LABEL(label), "Hello World!");
}*/

static void print_hello(GtkWidget *widget, gpointer data) {
  g_print("Hello World\n");
  GObject *label = (GObject *)data;
  gtk_label_set_text(GTK_LABEL(label), "Hello World!");
}

static void print_other_hello(GtkWidget *widget, gpointer data) {
  g_print("Hello Other World\n");
  GObject *label = (GObject *)data;
  gtk_label_set_text(GTK_LABEL(label), "Hello Other World!");
}

static void
quit_cb (GtkWindow *window)
{
  gtk_window_close (window);
}

static void
activate (GtkApplication *app, gpointer user_data)
{
  //create a GTKBuilder instance
  GtkBuilder *builder = gtk_builder_new ();
  //import the XML file for the UI
  gtk_builder_add_from_file (builder, "builder.ui", NULL);

  /* Connect signal handlers to the constructed widgets. */
  GObject *window = gtk_builder_get_object (builder, "window");
  gtk_window_set_application (GTK_WINDOW (window), app);
  
  //change focus to the "button1" button
  GObject *button = gtk_builder_get_object (builder, "button1");
  //when "button1" is pressed, run "hello_world"
  g_signal_connect(button, "clicked",(print_hello), gtk_builder_get_object(builder, "label1"));
  //g_signal_connect (button, "clicked", G_CALLBACK (print_hello), NULL);
  
  //change focus to the "button2" button
  button = gtk_builder_get_object (builder, "button2");
  //when "button1" is pressed, run "hello_other_world"
  g_signal_connect(button, "clicked",(print_other_hello), gtk_builder_get_object(builder, "label1"));
  
  //change focus to the "quit" button
  button = gtk_builder_get_object (builder, "quit");
  //when the "quit" button is pressed, quit the window.
  g_signal_connect_swapped (button, "clicked", G_CALLBACK (quit_cb), window);
  
  //draw the window.
  gtk_widget_show (GTK_WIDGET (window));

  //destroy the builder
  g_object_unref (builder);
}

int
main (int   argc, char *argv[])
{
#ifdef GTK_SRCDIR
  g_chdir (GTK_SRCDIR);
#endif

  GtkApplication *app = gtk_application_new ("org.gtk.example", G_APPLICATION_FLAGS_NONE);
  g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);

  int status = g_application_run (G_APPLICATION (app), argc, argv);
  g_object_unref (app);

  return status;
}

In your builder.ui file, is the object with ID label1 actually a GtkLabel? Like:

<object class="GtkLabel" id="label1">

I have:
<object id="output1" class="GtkLabel">
Did I need to switch id and class?

EDIT: I can’t read. Thanks for your help though.

The code the accessing output1 is commeted-out, so it’s not that.

Your signal event handlers for button1 and button2 each get label1 for their callbacks, so it looks like accessing the object for that ID (label1) is where the error is. If you’re using Glade you can just load the .ui file and check/change easily.