[Gtk-sharp-list] A TreeStore problem
jacocoder@o2.pl
jacocoder@o2.pl
Mon, 21 Feb 2005 09:29:51 +0100
Hello!
I would like to ask about an error which occurs in my program. I have a TreeStore of two types:
string and MyContainer. MyContainer is a class used to represent objects in the tree.
When OnSelectionChanged event occurs I get an item and the object which is in the second column
(MyContainer) of the iter. Then I call a method on this object. Everything is ok but after several repetitions
of this operation appears an error:
Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object
in (unmanaged) (wrapper managed-to-native) Gtk.TreeStore:gtk_tree_model_get_value (intptr,Gtk.TreeIter&,int,GLib.Value&)
in <0x00004> (wrapper managed-to-native) Gtk.TreeStore:gtk_tree_model_get_value (intptr,Gtk.TreeIter&,int,GLib.Value&)
in <0x00029> Gtk.TreeStore:GetValue (Gtk.TreeIter,int,GLib.Value&)
in <0x00060> Gtk.TreeStore:GetValue (Gtk.TreeIter,int)
in <0x000bc> MyWindow:OnSelectionChanged (object,System.EventArgs)
in <0x00069> (wrapper delegate-invoke) System.MulticastDelegate:invoke_void_object_EventArgs (object,System.EventArgs)
in <0x0012d> GtkSharp.voidObjectSignal:voidObjectCallback (intptr,int)
in <0x0005a> (wrapper native-to-managed) GtkSharp.voidObjectSignal:voidObjectCallback (intptr,int)
in (unmanaged) (wrapper managed-to-native) Gtk.Application:gtk_main ()
in <0x00004> (wrapper managed-to-native) Gtk.Application:gtk_main ()
in <0x00007> Gtk.Application:Run ()
in <0x0002a> MainClass:Main (string[])
I do not understand why it does not happen at the first time? And what is the reason such an error occurs?
My project consists of three files: Main.cs, MyWindow.cs and MyContainer.cs. I use gtk-sharp
version 1.0.0.0 and mono 1.0.5.
Source files are listed below.
I will be very grateful for any help.
Jakub Czeczotka
/* FILE Main.cs */
using System;
using Gtk;
class MainClass {
public static void Main(string[] args) {
Application.Init ();
new MyWindow ();
Application.Run ();
}
}
/* END OF FILE Main.cs */
/* FILE MyWindow.cs */
using System;
using Gtk;
using GLib;
public class MyWindow : Window {
private TreeView treeView = null;
public MyWindow () : base ("MyWindow") {
try {
TreeStore store = new TreeStore (typeof(string), typeof(MyContainer));
for (int i=0; i < 5; i++) {
MyContainer _container = new MyContainer(i);
TreeIter iter = store.AppendValues ("Demo " + i, _container);
}
Window win = new Window ("TreeView List Demo");
win.DeleteEvent += new DeleteEventHandler (delete_cb);
win.SetDefaultSize (400,250);
ScrolledWindow sw = new ScrolledWindow ();
win.Add (sw);
treeView = new TreeView ();
treeView.Model = store;
treeView.HeadersVisible = true;
treeView.Selection.Changed += new EventHandler(this.OnSelectionChanged);
treeView.AppendColumn ("Demo", new CellRendererText (), "text", 0);
sw.Add (treeView);
sw.Show ();
win.ShowAll ();
} catch (Exception ex) {
Console.WriteLine(ex.ToString());
}
}
void OnMyWindowDelete (object o, DeleteEventArgs args) {
Application.Quit ();
}
private void OnSelectionChanged (object o, EventArgs args) {
TreeIter iter;
TreeModel model;
if (((TreeSelection)o).GetSelected (out model, out iter)) {
object val = treeView.Model.GetValue(iter, 1);
if (null != val) {
Console.WriteLine ("{0} <- was selected", val.ToString());
MyContainer mc = val as MyContainer;
mc.Show();
}
}
}
private void delete_cb (System.Object o, DeleteEventArgs args) {
Application.Quit ();
args.RetVal = true;
}
}
/* END OF FILE MyWindow.cs */
/* FILE MyContainer.cs */
using System;
using Gtk;
public class MyContainer : GLib.Object {
static GLib.GType gtype;
public static new GLib.GType GType {
get {
if (gtype == GLib.GType.Invalid)
gtype = RegisterGType (typeof (MyContainer));
return gtype;
}
}
public MyContainer() : base (GType) {
}
public MyContainer(int _value) : base (GType) {
this._value = _value;
}
public override string ToString() {
return "my value is " + this._value.ToString();
}
public void Show () {
Console.WriteLine ("My private value is " + _value.ToString());
}
private int _value = 0;
}
/* END OF FILE MyContainer.cs */