GTK Hangs & An Index Error

Please support our C# advertiser: Intel Parallel Studio Home
Reply

Join Date: Jun 2009
Posts: 41
Reputation: pymatio is an unknown quantity at this point 
Solved Threads: 0
pymatio pymatio is offline Offline
Light Poster

GTK Hangs & An Index Error

 
0
  #1
Oct 10th, 2009
This program hangs when trying to generate fixtures when the table is empty:
  1. using System;
  2. using Gtk;
  3. using Mono.Data.Sqlite;
  4. using System.IO;
  5.  
  6. public partial class MainWindow: Gtk.Window
  7. {
  8. //SQLite variables
  9. static SqliteConnection DataConn;
  10. static SqliteCommand DataComm = new SqliteCommand();
  11. static SqliteDataReader DataReader;
  12. string dsn;
  13.  
  14. //tree_1 variables
  15. Gtk.TreeViewColumn name_1 = new Gtk.TreeViewColumn ();
  16. Gtk.ListStore List_1 = new Gtk.ListStore (typeof (string));
  17. Gtk.CellRendererText NameCell_1 = new Gtk.CellRendererText ();
  18.  
  19. //tree_2 variables
  20. Gtk.TreeViewColumn home_team_2 = new Gtk.TreeViewColumn ();
  21. Gtk.TreeViewColumn away_team_2 = new Gtk.TreeViewColumn ();
  22. Gtk.TreeStore List_2 = new Gtk.TreeStore (typeof (string), typeof (string));
  23. Gtk.CellRendererText homeCell_2 = new Gtk.CellRendererText ();
  24. Gtk.CellRendererText awayCell_2 = new Gtk.CellRendererText ();
  25.  
  26. //Any
  27. Gtk.TreeIter iter;
  28.  
  29. int numofteams;
  30. string[] teamsa;
  31. string[] homea;
  32. string[] awaya;
  33. int[,] fixturea;
  34.  
  35. public void generate_fixtures()
  36. {
  37. Random rand = new Random();
  38. int home;
  39. int away;
  40. int numoffix = numofteams/2;
  41. int j = 0;
  42. homea = new string[numoffix];
  43. awaya = new string[numoffix];
  44. int[] teamsused = new int[numofteams];
  45. for (int i = 0; i < numoffix; i++){
  46. do{
  47. home = rand.Next(0, numofteams);
  48. away = rand.Next(0, numofteams);
  49. } while (home == away || fixturea[home,away] != 0 || teamsused[home] != 0 || teamsused[away] != 0);
  50. fixturea[home,away] = 1;
  51. homea[j] = teamsa[home];
  52. awaya[j] = teamsa[away];
  53. teamsused[home] = 1;
  54. teamsused[away] = 1;
  55. j++;
  56. }
  57. }
  58.  
  59. public void generate_all_fixtures()
  60. {
  61. fixturea = new int[numofteams,numofteams];
  62. DataComm.CommandText = "DELETE FROM fixture;";
  63. DataComm.ExecuteNonQuery();
  64. int blah = (numofteams-1) * 2;
  65. for (int i = 0; i <= blah; i++){
  66. generate_fixtures();
  67. for (int j = 0; j < numofteams/2; j++){
  68. DataComm.CommandText = string.Format("INSERT INTO fixture VALUES (null, \"{0}\", \"{1}\", {2});", homea[j], awaya[j], i+1);
  69. DataComm.ExecuteNonQuery();
  70. }
  71. }
  72. tree_2_add_data();
  73. }
  74.  
  75. public void generate_team_array()
  76. {
  77. DataComm.CommandText = "SELECT * FROM teams;";
  78. DataReader = DataComm.ExecuteReader();
  79. teamsa = new string[numofteams];
  80. int i = 0;
  81. string returned;
  82. while (DataReader.Read()){
  83. returned = DataReader["Name"].ToString();
  84. teamsa[i] = returned;
  85. i++;
  86. }
  87. DataReader.Close();
  88. }
  89.  
  90. public void tree_1_add_data()
  91. {
  92. numofteams = 0;
  93. DataComm.CommandText = "SELECT * FROM teams;";
  94. DataReader = DataComm.ExecuteReader();
  95. string returned;
  96. while (DataReader.Read()){
  97. returned = DataReader["Name"].ToString();
  98. List_1.AppendValues(returned);
  99. numofteams += 1;
  100. }
  101. DataReader.Close();
  102. generate_team_array();
  103. }
  104.  
  105. public void tree_2_add_data()
  106. {
  107. string day;
  108. List_2.Clear();
  109. for (int i = 0; i < numofteams/2; i++){
  110. DataComm.CommandText = string.Format("SELECT * FROM fixture where Day = {0};", i+1);
  111. DataReader = DataComm.ExecuteReader();
  112. if (DataReader.HasRows){
  113. day = string.Format("Day {0}", i+1);
  114. iter = List_2.AppendValues(day);
  115. }
  116. while (DataReader.Read()){
  117. List_2.AppendValues(iter, DataReader["Home"].ToString(), DataReader["Away"].ToString());
  118. numofteams += 1;
  119. }
  120. DataReader.Close();
  121. }
  122. }
  123.  
  124. public void tree_conf()
  125. {
  126. //tree_1
  127. name_1.Title = "Name";
  128. tree_1.AppendColumn(name_1);
  129. tree_1.Model = List_1;
  130. name_1.PackStart (NameCell_1, true);
  131. name_1.AddAttribute (NameCell_1, "text", 0);
  132. tree_1_add_data();
  133.  
  134. //tree_2
  135. home_team_2.Title = "Home Team";
  136. away_team_2.Title = "Away Team";
  137. tree_2.AppendColumn(home_team_2);
  138. tree_2.AppendColumn(away_team_2);
  139. tree_2.Model = List_2;
  140. home_team_2.PackStart(homeCell_2, true);
  141. home_team_2.AddAttribute(homeCell_2, "text", 0);
  142. away_team_2.PackStart(awayCell_2, true);
  143. away_team_2.AddAttribute(awayCell_2, "text", 1);
  144. tree_2_add_data();
  145. }
  146.  
  147. public MainWindow (): base (Gtk.WindowType.Toplevel)
  148. {
  149. Build ();
  150. bool first = false;
  151. if (!File.Exists("league.db")){
  152. SqliteConnection.CreateFile("league.db");
  153. first = true;
  154. }
  155. string dsn = "Data Source=league.db;";
  156. DataConn = new SqliteConnection(dsn);
  157. DataConn.Open();
  158. DataComm.Connection = DataConn;
  159. if (first){
  160. DataComm.CommandText = "CREATE TABLE teams (ID INTEGER PRIMARY KEY, Name TEXT);";
  161. DataComm.ExecuteNonQuery();
  162. DataComm.CommandText = "CREATE TABLE fixture (ID INTEGER PRIMARY KEY, Home TEXT, Away TEXT, Day INTEGER);";
  163. DataComm.ExecuteNonQuery();
  164. }
  165. tree_conf();
  166. }
  167.  
  168. protected void OnDeleteEvent (object sender, DeleteEventArgs a)
  169. {
  170. Application.Quit ();
  171. a.RetVal = true;
  172. }
  173.  
  174. protected virtual void on_submit_1 (object sender, System.EventArgs e)
  175. {
  176. //if it's empty don't do anything
  177. string team = team_1.Text;
  178. if (team == ""){
  179. return;
  180. }
  181. //Insert data into the table
  182. DataComm.CommandText = string.Format("INSERT INTO teams VALUES (null, \"{0}\");", team);
  183. DataComm.ExecuteNonQuery();
  184. //Clear tree_1
  185. List_1.Clear();
  186. //Query database for teams
  187. tree_1_add_data();
  188. }
  189.  
  190. protected virtual void on_generate_2 (object sender, System.EventArgs e)
  191. {
  192. generate_all_fixtures();
  193. }
  194. }
& when you try to generate fixtures when the table is full it gives a Index Error at 51:
  1. homea[j] = teamsa[home];
why?
Attached Files
File Type: zip leaguemanager.zip (20.3 KB, 0 views)
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 908
Reputation: DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough 
Solved Threads: 145
DdoubleD DdoubleD is offline Offline
Posting Shark
 
0
  #2
Oct 10th, 2009
This is the second time I've seen this project, which I cannot do anything with because I don't have your 3rd party libraries or data. I've noticed you made a few modifications since the previous thread.

Anyway, these lines look a little suspicious, and "blah" is probably not the best name you could have come up with...

  1. int blah = (numofteams-1) * 2;
  2. for (int i = 0; i <= blah; i++)

I'm wondering whether you meant for "blah" to be = numofteams * 2 - 1 and for condition to be i < blah ?

Is this an application that is available for free that you are modifying that we can download the original somewhere and get the 3rd party libraries installed?
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 41
Reputation: pymatio is an unknown quantity at this point 
Solved Threads: 0
pymatio pymatio is offline Offline
Light Poster
 
0
  #3
Oct 10th, 2009
I've stopped the freezing, but not the index error
  1. using System;
  2. using Gtk;
  3. using Mono.Data.Sqlite;
  4. using System.IO;
  5.  
  6. public partial class MainWindow: Gtk.Window
  7. {
  8. //SQLite variables
  9. static SqliteConnection DataConn;
  10. static SqliteCommand DataComm = new SqliteCommand();
  11. static SqliteDataReader DataReader;
  12. string dsn;
  13.  
  14. //tree_1 variables
  15. Gtk.TreeViewColumn name_1 = new Gtk.TreeViewColumn ();
  16. Gtk.ListStore List_1 = new Gtk.ListStore (typeof (string));
  17. Gtk.CellRendererText NameCell_1 = new Gtk.CellRendererText ();
  18.  
  19. //tree_2 variables
  20. Gtk.TreeViewColumn home_team_2 = new Gtk.TreeViewColumn ();
  21. Gtk.TreeViewColumn away_team_2 = new Gtk.TreeViewColumn ();
  22. Gtk.TreeStore List_2 = new Gtk.TreeStore (typeof (string), typeof (string));
  23. Gtk.CellRendererText homeCell_2 = new Gtk.CellRendererText ();
  24. Gtk.CellRendererText awayCell_2 = new Gtk.CellRendererText ();
  25.  
  26. //Any
  27. Gtk.TreeIter iter;
  28.  
  29. int numofteams;
  30. int numofteamsOLD;
  31. string[] teamsa;
  32. string[] homea;
  33. string[] awaya;
  34. int[,] fixturea;
  35.  
  36. public void generate_fixtures()
  37. {
  38. Random rand = new Random();
  39. int home;
  40. int away;
  41. int numoffix = numofteams/2;
  42. Console.WriteLine("numofteams: {0}", numofteams);
  43. Console.WriteLine("numoffix : {0}", numoffix);
  44. int j = 0;
  45. homea = new string[numoffix];
  46. awaya = new string[numoffix];
  47. int[] teamsused = new int[numofteams];
  48. int k = 0;
  49. for (int i = 0; i < numoffix; i++){
  50. do{
  51. home = rand.Next(0, numofteams);
  52. away = rand.Next(0, numofteams);
  53. k++;
  54. } while (home == away || fixturea[home,away] != 0 || teamsused[home] != 0 || teamsused[away] != 0);
  55. fixturea[home,away] = 1;
  56. Console.WriteLine(j);
  57. homea[j] = teamsa[home];
  58. awaya[j] = teamsa[away];
  59. teamsused[home] = 1;
  60. teamsused[away] = 1;
  61. j++;
  62. }
  63. }
  64.  
  65. public void generate_all_fixtures()
  66. {
  67. fixturea = new int[numofteams,numofteams];
  68. DataComm.CommandText = "DELETE FROM fixture;";
  69. DataComm.ExecuteNonQuery();
  70. int blah = (numofteams-1) * 2;
  71. for (int i = 0; i < blah; i++){
  72. generate_fixtures();
  73. for (int j = 0; j < numofteams/2; j++){
  74. DataComm.CommandText = string.Format("INSERT INTO fixture VALUES (null, \"{0}\", \"{1}\", {2});", homea[j], awaya[j], i+1);
  75. DataComm.ExecuteNonQuery();
  76. }
  77. }
  78. tree_2_add_data();
  79. }
  80.  
  81. public void generate_team_array()
  82. {
  83. DataComm.CommandText = "SELECT * FROM teams;";
  84. DataReader = DataComm.ExecuteReader();
  85. teamsa = new string[numofteams];
  86. int i = 0;
  87. string returned;
  88. while (DataReader.Read()){
  89. returned = DataReader["Name"].ToString();
  90. teamsa[i] = returned;
  91. i++;
  92. }
  93. DataReader.Close();
  94. }
  95.  
  96. public void tree_1_add_data()
  97. {
  98. numofteams = 0;
  99. DataComm.CommandText = "SELECT * FROM teams;";
  100. DataReader = DataComm.ExecuteReader();
  101. string returned;
  102. while (DataReader.Read()){
  103. returned = DataReader["Name"].ToString();
  104. List_1.AppendValues(returned);
  105. numofteams += 1;
  106. }
  107. DataReader.Close();
  108. generate_team_array();
  109. }
  110.  
  111. public void tree_2_add_data()
  112. {
  113. string day;
  114. List_2.Clear();
  115. for (int i = 0; i < numofteams/2; i++){
  116. DataComm.CommandText = string.Format("SELECT * FROM fixture where Day = {0};", i+1);
  117. DataReader = DataComm.ExecuteReader();
  118. if (DataReader.HasRows){
  119. day = string.Format("Day {0}", i+1);
  120. iter = List_2.AppendValues(day);
  121. } else {
  122. DataReader.Close();
  123. return;
  124. }
  125. while (DataReader.Read()){
  126. List_2.AppendValues(iter, DataReader["Home"].ToString(), DataReader["Away"].ToString());
  127. numofteams += 1;
  128. }
  129. DataReader.Close();
  130. }
  131. }
  132.  
  133. public void tree_conf()
  134. {
  135. //tree_1
  136. name_1.Title = "Name";
  137. tree_1.AppendColumn(name_1);
  138. tree_1.Model = List_1;
  139. name_1.PackStart (NameCell_1, true);
  140. name_1.AddAttribute (NameCell_1, "text", 0);
  141. tree_1_add_data();
  142.  
  143. //tree_2
  144. home_team_2.Title = "Home Team";
  145. away_team_2.Title = "Away Team";
  146. tree_2.AppendColumn(home_team_2);
  147. tree_2.AppendColumn(away_team_2);
  148. tree_2.Model = List_2;
  149. home_team_2.PackStart(homeCell_2, true);
  150. home_team_2.AddAttribute(homeCell_2, "text", 0);
  151. away_team_2.PackStart(awayCell_2, true);
  152. away_team_2.AddAttribute(awayCell_2, "text", 1);
  153. tree_2_add_data();
  154. }
  155.  
  156. public MainWindow (): base (Gtk.WindowType.Toplevel)
  157. {
  158. Build ();
  159. bool first = false;
  160. if (!File.Exists("league.db")){
  161. SqliteConnection.CreateFile("league.db");
  162. first = true;
  163. }
  164. string dsn = "Data Source=league.db;";
  165. DataConn = new SqliteConnection(dsn);
  166. DataConn.Open();
  167. DataComm.Connection = DataConn;
  168. if (first){
  169. DataComm.CommandText = "CREATE TABLE teams (ID INTEGER PRIMARY KEY, Name TEXT);";
  170. DataComm.ExecuteNonQuery();
  171. DataComm.CommandText = "CREATE TABLE fixture (ID INTEGER PRIMARY KEY, Home TEXT, Away TEXT, Day INTEGER);";
  172. DataComm.ExecuteNonQuery();
  173. }
  174. tree_conf();
  175. }
  176.  
  177. protected void OnDeleteEvent (object sender, DeleteEventArgs a)
  178. {
  179. Application.Quit ();
  180. a.RetVal = true;
  181. }
  182.  
  183. protected virtual void on_submit_1 (object sender, System.EventArgs e)
  184. {
  185. //if it's empty don't do anything
  186. string team = team_1.Text;
  187. if (team == ""){
  188. return;
  189. }
  190. //Insert data into the table
  191. DataComm.CommandText = string.Format("INSERT INTO teams VALUES (null, \"{0}\");", team);
  192. DataComm.ExecuteNonQuery();
  193. //Clear tree_1
  194. List_1.Clear();
  195. //Query database for teams
  196. tree_1_add_data();
  197. }
  198.  
  199. protected virtual void on_generate_2 (object sender, System.EventArgs e)
  200. {
  201. generate_all_fixtures();
  202. }
  203. }
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 41
Reputation: pymatio is an unknown quantity at this point 
Solved Threads: 0
pymatio pymatio is offline Offline
Light Poster
 
0
  #4
Oct 10th, 2009
"Is this an application that is available for free that you are modifying that we can download the original somewhere and get the 3rd party libraries installed? "
No it's my own app, it is made with Mono & GTK# on Linux instead of .Net
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 908
Reputation: DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough 
Solved Threads: 145
DdoubleD DdoubleD is offline Offline
Posting Shark
 
0
  #5
Oct 10th, 2009
Which index is throwing the error: j or home ?
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 41
Reputation: pymatio is an unknown quantity at this point 
Solved Threads: 0
pymatio pymatio is offline Offline
Light Poster
 
0
  #6
Oct 10th, 2009
"j" is to big for "homea", "homea" is the right size.
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 908
Reputation: DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough 
Solved Threads: 145
DdoubleD DdoubleD is offline Offline
Posting Shark
 
0
  #7
Oct 10th, 2009
Originally Posted by pymatio View Post
"Is this an application that is available for free that you are modifying that we can download the original somewhere and get the 3rd party libraries installed? "
No it's my own app, it is made with Mono & GTK# on Linux instead of .Net
I've installed the Gtk# for .NET (http://www.go-mono.com/mono-downloads/download.html), but I still have one unresolved reference to Mono.Data.Sqlite.

I tried downloading the command-line program Sqlite-3 6 18.zip thinking I could reference the assembly and resolve the above, but no luck. Any suggestions?
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 908
Reputation: DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough 
Solved Threads: 145
DdoubleD DdoubleD is offline Offline
Posting Shark
 
0
  #8
Oct 10th, 2009
Originally Posted by pymatio View Post
"j" is to big for "homea", "homea" is the right size.
I don't see how "j" can be the culprit since j is always equal to "i" and "i" is always < numofix and homea is sized at numoffix.

Try adding adding this debug statement and assignment before the homea[j] assignment:

  1. System.Diagnostics.Debug.Assert(i == j && j < numoffix && numoffix == homea.Length);
  2. homea[j] = "";
  3. homea[j] = teamsa[home];

The assert should happen if the empty string assignment (test) is going to fail...
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 41
Reputation: pymatio is an unknown quantity at this point 
Solved Threads: 0
pymatio pymatio is offline Offline
Light Poster
 
0
  #9
Oct 10th, 2009
Originally Posted by DdoubleD View Post
I don't see how "j" can be the culprit since j is always equal to "i" and "i" is always < numofix and homea is sized at numoffix.

Try adding adding this debug statement and assignment before the homea[j] assignment:

  1. System.Diagnostics.Debug.Assert(i == j && j < numoffix && numoffix == homea.Length);
  2. homea[j] = "";
  3. homea[j] = teamsa[home];

The assert should happen if the empty string assignment (test) is going to fail...
The error I get is:
  1. Marshaling clicked signal
  2. Exception in Gtk# callback delegate
  3. Note: Applications can use GLib.ExceptionManager.UnhandledException to handle the exception.
  4. System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.IndexOutOfRangeException: Array index is out of range.
  5. at MainWindow.generate_fixtures () [0x000cf] in /home/matio/Projects/leaguemanager/leaguemanager/MainWindow.cs:68
  6. at MainWindow.generate_all_fixtures () [0x00043] in /home/matio/Projects/leaguemanager/leaguemanager/MainWindow.cs:84
  7. at MainWindow.on_generate_2 (System.Object sender, System.EventArgs e) [0x00000] in /home/matio/Projects/leaguemanager/leaguemanager/MainWindow.cs:258
  8. at (wrapper managed-to-native) System.Reflection.MonoMethod:InternalInvoke (object,object[],System.Exception&)
  9. at System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00000]
  10. --- End of inner exception stack trace ---
  11. at System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00000]
  12. at System.Reflection.MethodBase.Invoke (System.Object obj, System.Object[] parameters) [0x00000]
  13. at System.Delegate.DynamicInvokeImpl (System.Object[] args) [0x00000]
  14. at System.MulticastDelegate.DynamicInvokeImpl (System.Object[] args) [0x00000]
  15. at System.Delegate.DynamicInvoke (System.Object[] args) [0x00000]
  16. at GLib.Signal.ClosureInvokedCB (System.Object o, GLib.ClosureInvokedArgs args) [0x00000]
  17. at GLib.SignalClosure.Invoke (GLib.ClosureInvokedArgs args) [0x00000]
  18. at GLib.SignalClosure.MarshalCallback (IntPtr raw_closure, IntPtr return_val, UInt32 n_param_vals, IntPtr param_values, IntPtr invocation_hint, IntPtr marshal_data) [0x00000]
  19. at GLib.ExceptionManager.RaiseUnhandledException(System.Exception e, Boolean is_terminal)
  20. at GLib.SignalClosure.MarshalCallback(IntPtr raw_closure, IntPtr return_val, UInt32 n_param_vals, IntPtr param_values, IntPtr invocation_hint, IntPtr marshal_data)
  21. at Gtk.Application.gtk_main()
  22. at Gtk.Application.Run()
  23. at leaguemanager.MainClass.Main(System.String[] args) in /home/matio/Projects/leaguemanager/leaguemanager/Main.cs:line 13
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 908
Reputation: DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough 
Solved Threads: 145
DdoubleD DdoubleD is offline Offline
Posting Shark
 
0
  #10
Oct 10th, 2009
Well, you didn't report whether it was actually "j", and I am still not convinced it is.

In this method,

  1. public void generate_team_array()
  2. {
  3. DataComm.CommandText = "SELECT * FROM teams;";
  4. DataReader = DataComm.ExecuteReader();
  5. teamsa = new string[numofteams];
  6. int i = 0;
  7. string returned;
  8. while (DataReader.Read()){
  9. returned = DataReader["Name"].ToString();
  10. teamsa[i] = returned;
  11. i++;
  12. }
  13. DataReader.Close();
  14. numofteamsREAL = i - 1;
  15. }

why are you setting numofteamsREAL = i - 1 ? Shouldn't it be numofteamsREAL = i ?

It's too difficult to figure this out this way... If you tell me how to get the reference to the Mono.Data.Sqlite namespace, I'll help you figure this out.

The error text you posted is only partially useful and the line numbers it shows have probably moved since the code you posted prior.
Last edited by DdoubleD; Oct 10th, 2009 at 1:58 pm.
Reply With Quote Quick reply to this message  
Reply

Tags
gtk, index-error, mono

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC