[英]Simple CRUD operation with PostgreSQL/MySQL in C# with Mono and Gtk#
[英]Null reference in C# Mono / GTK#
我試圖創建一個簡單的筆記應用程序供我自己使用。 這個想法是學習C#/ Mono XML編輯。
我找不到編譯器抱怨的NullReference。 不知道為什么。 我看不到 經過幾天的搜索,我放棄了...幫助。 :)
這是造成錯誤的代碼。 在我按下按鈕添加新筆記之前,該應用程序運行良好。 它只是崩潰。 當按下按鈕時,add_activated函數將運行,並且應使用AddNote函數。
代碼不完整,並且肯定存在一些邏輯錯誤。 但是我可以解決。 我只是想知道為什么它不運行。
MainActivity.cs:
// (...)
protected void add_activated (object sender, System.EventArgs e)
{
Gtk.TextBuffer buffer;
buffer = textview1.Buffer;
Note note = new Note(entry3.Text, buffer.Text);
AddNote (note);
}
public static void AddNote (Note note)
{
string xmlFile = "/home/tomasz/.keeper/keeper.xml";
XmlDocument xmlDoc = new XmlDocument ();
xmlDoc.Load (xmlFile);
XmlNode xmlRoot = xmlDoc.CreateElement("keeper");
if (!xmlDoc.DocumentElement.Name.Equals("keeper") )
{
xmlDoc.AppendChild (xmlRoot);
}
XmlNode xmlNote = xmlDoc.CreateElement ("note");
XmlNode xmlTitle = xmlDoc.CreateElement ("title");
XmlNode xmlText = xmlDoc.CreateElement ("text");
xmlRoot.InsertAfter (xmlRoot.LastChild, xmlNote);
xmlTitle.InnerText = note.Title;
xmlNote.InsertAfter (xmlNote.LastChild, xmlTitle);
xmlText.InnerText = note.Text;
xmlNote.InsertAfter (xmlNote.LastChild, xmlText);
xmlDoc.Save (xmlFile);
}
protected void remove_activated (object sender, System.EventArgs e)
{
throw new System.NotImplementedException ();
}
}
}
Note.cs:
using System;
namespace Keeper
{
public class Note
{
private string title;
private string text;
public string Title {
get
{
return this.title;
}
set
{
this.title = value;
}
}
public string Text
{
get
{
return this.text;
}
set
{
this.text = value;
}
}
public Note (string title, string text)
{
this.Title = title;
this.Text = text;
}
}
}
直接的問題是您混淆了InserAfter
的參數。 第一個應該是要插入的節點,第二個應該是參考節點。 根元素還沒有子元素,因此LastChild
將為null
,因此成為例外。 使用null
作為參考點是有效的,但不能用作要添加的節點。
還有其他問題,但是您說您能夠解決這些問題,所以就去了。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.