[Gtk-sharp-list] GTK TreeView Callback Bug?

Marius M. M. devilx at gdesklets.org
Wed Aug 24 16:03:37 EDT 2005


Hi,

This is my first real C# application I'm trying to write and would like
to know, if that's a bug or if I just did something wrong. This is my
Code:

-----------------------------------------------------------------------------
using System;
using Gtk;
using Glade;
using GLib;

public class YstMain
{
	[Glade.Widget] Window frmMain;
	[Glade.Widget] ScrolledWindow swMystocks;
	TreeView tvMystocks = null;
	NodeStore StockStore = null;
	
	string [] myStocks;
	
	public static void Main (string[] args)
	{
		new YstMain(args);
	}

	public YstMain(string[] args) 
	{
		Application.Init ();
		Glade.XML gxml = new Glade.XML (null, "gui.glade", "frmMain", null);
		gxml.Autoconnect(this);
		
		tvMystocks = new TreeView(YstBuildNodeStore());
		tvMystocks.HeadersVisible = false;
		tvMystocks.AppendColumn("Stock", new CellRendererText(), "text", 0);
		tvMystocks.Selection.Changed += new EventHandler
(OnMainMystocksSelection);
		swMystocks.Add(tvMystocks);		
		
		myStocks = YstLoadStocks();
		for(int i=0;i<myStocks.Length;i++)	
			YstStoreAdd(myStocks[i]);

		frmMain.ShowAll();
		Application.Run ();	
	}
	
	public string[] YstLoadStocks()
	{
		string foo = YstSettings.Mystocks;
		string baa = foo.ToUpper();
		return baa.Split(',');
	}
	
	[TreeNode(ColumnCount=1)]
	private class YstTreeNode : TreeNode
	{
		string level;
		int count;
		
		public YstTreeNode(string level, int count)
		{
			this.level = level;
			this.count = count;
		}
		
		[TreeNodeValue(Column=0)]
		public string Level
		{
			get
			{
				return level;
			}
		}
		
		public int Count
		{
			get
			{
				return count;
			}
		}
	}
	
	private NodeStore YstBuildNodeStore()
	{
		StockStore = new NodeStore(typeof(YstTreeNode));
		return StockStore;
	}

	private void YstStoreAdd(string Stock)
	{
		ITreeNode node = new YstTreeNode(Stock,1);
		StockStore.AddNode(node);
		tvMystocks.QueueDraw();	
	}
	
	private void OnWindowDeleteEvent (object sender, DeleteEventArgs a) 
	{
		Application.Quit ();
		a.RetVal = true;
	}
	
	private void OnMainMystocksSelection (object sender, EventArgs a) 
	{
		TreeSelection ts = (TreeSelection) sender;
		TreeIter ti = new TreeIter();
		TreeModel tm;
 
		if (ts.GetSelected (out tm, out ti))
		{
			Console.WriteLine(tm.GetStringFromIter(ti));
		}
}
-----------------------------------------------------------------------------

For some reasons I do not understand, now the Callback
OnMainMystocksSelection is crashing my application. In the #mono channel
on the GIMPnet, I was told to try this code:

-----------------------------------------------------------------------------
	private void OnMainMystocksSelection (object sender, EventArgs a) 
	{
		TreeSelection ts = (TreeSelection) sender;
		ts.SelectedForeach(delegate (TreeModel model, TreePath path, TreeIter
iter){
			Console.WriteLine (model);
			Console.WriteLine (model.GetStringFromIter (iter));
		});
	}
-----------------------------------------------------------------------------

And check, if anything will be printen in the console. The result was
that nothing but this got printed:

-----------------------------------------------------------------------------
Unhandled Exception: System.NullReferenceException: Object reference not
set to an instance of an object
in <0x0002f> YstMain:<#AnonymousMethod>0 (Gtk.TreeModel,Gtk.TreePath,Gtk.TreeIter)
in <0x0007f> (wrapper delegate-invoke) System.MulticastDelegate:invoke_void_TreeModel_TreePath_TreeIter (Gtk.TreeModel,Gtk.TreePath,Gtk.TreeIter)
in <0x00117> GtkSharp.TreeSelectionForeachFuncWrapper:NativeCallback (intptr,intptr,Gtk.TreeIter&,intptr)
in <0x00037> (wrapper native-to-managed) GtkSharp.TreeSelectionForeachFuncWrapper:NativeCallback (intptr,intptr,Gtk.TreeIter&,intptr)
in (unmanaged) 0xb7118bbe
in <0x00004> (wrapper managed-to-native) Gtk.TreeSelection:gtk_tree_selection_selected_foreach (intptr,GtkSharp.TreeSelectionForeachFuncNative,intptr)
in <0x00041> Gtk.TreeSelection:SelectedForeach (Gtk.TreeSelectionForeachFunc)
in [0x00014] (at /home/devilx/MyProjects/yst/Main.cs:132) YstMain:OnMainMystocksSelection (object,System.EventArgs)
in <0x00043> (wrapper delegate-invoke) System.MulticastDelegate:invoke_void_object_EventArgs (object,System.EventArgs)
in <0x00096> GLib.Signal:voidObjectCallback (intptr,intptr)
in <0x0002a> (wrapper native-to-managed) GLib.Signal:voidObjectCallback (intptr,intptr)
in (unmanaged) 0xb6d4860a
in <0x00004> (wrapper managed-to-native) Gtk.Application:gtk_main ()
in <0x00007> Gtk.Application:Run ()
in [0x000d9] (at /home/devilx/MyProjects/yst/Main.cs:38) YstMain:.ctor (string[])
in [0x00001] (at /home/devilx/MyProjects/yst/Main.cs:19) YstMain:Main (string[])
-----------------------------------------------------------------------------

After I wrote that on #mono, I was told to ask here for help.
Could anybody please tell me what's wrong with the Code or if that's a Bug?
Thank you very much.

Goodbye.



More information about the Gtk-sharp-list mailing list