[Gtk-sharp-list] Problems with TreeView

Todd Berman tberman at off.net
Mon Aug 15 16:50:47 EDT 2005


I think you need to get a Beginning C# type book.

You need to make ListStore static as you are aware.

However, here is why you get that nullreference crash:

		ListStore store = new ListStore (typeof (string), typeof
			(string));

That code right there creates a local (to the method) ListStore.

You want:

store = new ListStore (typeof (string), typeof (string));

That will cause the static ListStore store to be assigned properly.

--Todd

On Mon, 2005-08-15 at 20:35 +0200, André Kuntze wrote:
> Hi everybody,
> 
> i am trying to do the following in mono using c# and gtk#: Having a
> simple two column list with word and numbers. When you edit one field in
> any column, write the new text into the edited field.
> 
> This must sound so newbish but i am struggeling with this a few hours
> now and so i decided to ask some people in the know. Til now i got this:
> 
> ** SNIP: CODE STARTS HERE
> 
> 
> /*
> *	Filename:	TreeViewTryOut.cs
> *	Date:		15.08.2005
> *
> *	Description:	This program is a TryOut for the editable
> *			TreeView - widget.
> *			The rows in the TreeView are editable and should
> *			take the new value, when entered.
> */
> 
> using System;
> using Gtk;
> 
> class MainClass{
> 	
> 	// Make the variable store visible to the whole class to use it
> 	// in the code for the Edited event
> 	ListStore store;
> 	
> 	public static void Main(string [] args){
> 		Application.Init();
> 
> 		// Definition of my window, which contains the editable
> 		// Two - Column - List
> 		Window window = new Window ("TreeView");
> 		window.DeleteEvent += Window_Deleted;
> 
> 		VBox vertBox = new VBox();
> 		vertBox.BorderWidth = 6;
> 
> 		window.Add(vertBox);
> 
> 
> 		// Definition of my TreeView
> 		TreeView treeView = new TreeView();
> 		treeView.HeadersVisible = true;
> 
> 		vertBox.Add(treeView);
> 
> 
> 		// Definition of the colums
> 		TreeViewColumn column = new TreeViewColumn();
> 		column.Title = "Word";
> 		CellRendererText colText = new CellRendererText();
> 
> 		// Here i add the editable flag and the Edited event
> 		colText.Editable = true;
> 		colText.Edited += Word_Edited;
> 		column.PackStart (colText, true);
> 		column.AddAttribute (colText, "text", 0);
> 		treeView.AppendColumn (column);
> 
> 		column = new TreeViewColumn();
> 		column.Title = "Page";
> 		colText = new CellRendererText();
> 
> 		// Here i add the editable flag and the Edited event
> 		colText.Editable = true;
> 		colText.Edited += Word_Edited;
> 		column.PackStart (colText, true);
> 		column.AddAttribute (colText, "text", 1);
> 		treeView.AppendColumn (column);
> 
> 
> 		// Definition of the field types
> 		ListStore store = new ListStore (typeof (string), typeof
> 			(string));
> 		treeView.Model = store;
> 
> 
> 		// Putting stuff in the list
> 		TreeIter iterator = new TreeIter();
> 		iterator = store.AppendValues("ChangeMe", "1");
> 
> 
> 		// Show the not working stuff :-/
> 		window.ShowAll();
> 		Application.Run();
> 	}
> 
> 
> 	// My not working code, when a field is edited
> 	static void Word_Edited (object o, EditedArgs e){
> 		// When edited get me the current field
> 		TreeIter iterator;
> 		TreePath path = new TreePath(e.Path);
> 
> 		// Try to write the new entry to the field and start
> 		// to crash awfully when using static
> 		store.GetIter(out iterator, path);
> 		store.SetValue(iterator, 0, e.NewText);
> 	}
> 
> 
> 	static void Window_Deleted (object o, DeleteEventArgs e){
> 		Application.Quit();
> 	}
> }
> 
> ** SNIP: CODE ENDS HERE
> 
> 
> 
> which gives me a crying compiler like this:
> 
> ** SNIP: STARTS CRYING HERE
> 
> andre at gremLin:~/programming/mono/IndexTool$ mcs -pkg:gtk-sharp
> TreeViewTryOut.csTreeViewTryOut.cs(27) error CS0103: The name `v' could
> not be found in `MainClass'
> TreeViewTryOut.cs(67) warning CS0219: The variable 'iterator' is
> assigned but its value is never used
> TreeViewTryOut.cs(84) error CS0120: An object reference is required for
> the non-static field `store'
> TreeViewTryOut.cs(85) error CS0120: An object reference is required for
> the non-static field `store'
> TreeViewTryOut.cs(15) warning CS0169: The private field
> 'MainClass.store' is never used
> Compilation failed: 3 error(s), 2 warnings
> 
> ** SNIP: STOPS CRYING HERE
> 
> 
> I think the problem so far is, that my store variable is used in Main
> which is static. But when i make store static and edit some entry the
> program crashes hard like:
> 
> 
> ** SNIP: BAD CRASH STARTS HERE
> 
> andre at gremLin:~/programming/mono/IndexTool$ mono TreeViewTryOut.exe
> 
> Unhandled Exception: System.Reflection.TargetInvocationException:
> Exception has been thrown by the target of an invocation. --->
> System.NullReferenceException: Object reference not set to an instance
> of an object
> in <0x0004d> MainClass:Word_Edited (System.Object o, Gtk.EditedArgs e)
> in <0x00000> <unknown method>
> in (wrapper managed-to-native)
> System.Reflection.MonoMethod:InternalInvoke (object,object[])
> in <0x0006f> System.Reflection.MonoMethod:Invoke (System.Object obj,
> BindingFlags invokeAttr, System.Reflection.Binder binder,
> System.Object[] parameters, System.Globalization.CultureInfo culture)---
> End of inner exception stack trace ---
> 
> in <0x00104> System.Reflection.MonoMethod:Invoke (System.Object obj,
> BindingFlags invokeAttr, System.Reflection.Binder binder,
> System.Object[] parameters, System.Globalization.CultureInfo culture)
> in <0x00017> System.Reflection.MethodBase:Invoke (System.Object obj,
> System.Object[] parameters)
> in <0x000b3> System.Delegate:DynamicInvokeImpl (System.Object[] args)
> in <0x00028> System.MulticastDelegate:DynamicInvokeImpl (System.Object[]
> args)
> in <0x0000e> System.Delegate:DynamicInvoke (System.Object[] args)
> in <0x00147>
> GtkSharp.voidObjectstringstringSignal:voidObjectstringstringCallback
> (IntPtr arg0, System.String arg1, System.String arg2, Int32 key)
> in (wrapper native-to-managed)
> GtkSharp.voidObjectstringstringSignal:voidObjectstringstringCallback
> (intptr,intptr,intptr,int)
> in <0x00000> <unknown method>
> in (wrapper managed-to-native) Gtk.Application:gtk_main ()
> in <0x00007> Gtk.Application:Run ()
> in <0x00370> MainClass:Main (System.String[] args)
> 
> ** SNIP: BAD CRASH ENDS HERE
> 
> 
> Any ideas? I am justing starting with mono and gtk# and so far i like it
> a lot but i have so much to learn before stopping to ask newbie
> questions :-/
> 
> I hope this big post is somehow easy to read. I edited a bit in
> evolution so it was more readable for me. Hope that didn't kill the
> formating information.
> 
> Please help and Greetings
> André
> 
> _______________________________________________
> Gtk-sharp-list maillist  -  Gtk-sharp-list at lists.ximian.com
> http://lists.ximian.com/mailman/listinfo/gtk-sharp-list
> 



More information about the Gtk-sharp-list mailing list