我的表单上有一个标签,我想要填充字符串的值,但我无法使其正常工作。这是我的代码;
在函数Execute中,我调用函数ShowText((string)result[3])
,result是我从数据库查询中获取的对象。这是ShowText函数;
public void ShowText(string message)
{
label4.Text = message;
}
我收到NullReferenceException错误,我做错了什么?
结果[3]; label4
代码;
Execute();
InitializeComponent();
var aTimer = new System.Timers.Timer();
aTimer.Elapsed += new ElapsedEventHandler((sender, e) => Execute());
aTimer.Interval = 300000;
aTimer.Enabled = true;
}
public void Execute()
{
int Tijd;
int Videolengte;
string resultaat;
string URL = "";
Database db = new Database(CONNECTION_STRING);
object[] result = db.GetFirstRecord();
if (result == null)
{
return;
}
else
{
//laat app steeds 50ms slapen zodat overgang van muziek mooi is
System.Threading.Thread.Sleep(500);
Audio.SetApplicationVolume(APP, 90);
System.Threading.Thread.Sleep(500);
Audio.SetApplicationVolume(APP, 80);
System.Threading.Thread.Sleep(500);
Audio.SetApplicationVolume(APP, 70);
System.Threading.Thread.Sleep(500);
Audio.SetApplicationVolume(APP, 60);
System.Threading.Thread.Sleep(500);
Audio.SetApplicationVolume(APP, 50);
System.Threading.Thread.Sleep(500);
Audio.SetApplicationVolume(APP, 40);
//start youtube video op
URL = (string)result[1];
Process browser = Process.Start(@"chrome.exe", "http:\\www.youtube.com/watch?v=" + URL);
ShowText((string)result[3]);
//laat app steeds 50ms slapen zodat overgang van muziek mooi is
System.Threading.Thread.Sleep(500);
Audio.SetApplicationVolume(APP, 30);
System.Threading.Thread.Sleep(500);
Audio.SetApplicationVolume(APP, 20);
System.Threading.Thread.Sleep(500);
Audio.SetApplicationVolume(APP, 10);
System.Threading.Thread.Sleep(500);
Audio.SetApplicationVolume(APP, 0);
//bepaald tijd die applicatie moet slapen
resultaat = (string)result[2];
Videolengte = Convert.ToInt32(resultaat);
Tijd = Videolengte * 1000;
//laat app slapen tot lengte van liedje
System.Threading.Thread.Sleep(Tijd);
//laat app steeds 50ms slapen zodat overgang van muziek mooi is
System.Threading.Thread.Sleep(500);
Audio.SetApplicationVolume(APP, 10);
System.Threading.Thread.Sleep(500);
Audio.SetApplicationVolume(APP, 20);
System.Threading.Thread.Sleep(500);
Audio.SetApplicationVolume(APP, 30);
System.Threading.Thread.Sleep(500);
Audio.SetApplicationVolume(APP, 40);
System.Threading.Thread.Sleep(500);
Audio.SetApplicationVolume(APP, 50);
System.Threading.Thread.Sleep(500);
Audio.SetApplicationVolume(APP, 60);
System.Threading.Thread.Sleep(500);
Audio.SetApplicationVolume(APP, 70);
System.Threading.Thread.Sleep(500);
Audio.SetApplicationVolume(APP, 80);
System.Threading.Thread.Sleep(500);
Audio.SetApplicationVolume(APP, 90);
System.Threading.Thread.Sleep(500);
Audio.SetApplicationVolume(APP, 100);
//Kill alle google extensies
Process[] localByName = Process.GetProcessesByName("chrome");
foreach (Process p in localByName)
{
p.Kill();
}
//verwijder record uit database
//deleteRow();
}
}
public void ShowText(string message)
{
label5.Text = message;
}
private void deleteRow()
{
try
{
using (SqlConnection conn = new SqlConnection(CONNECTION_STRING))
{
conn.Open();
using (SqlCommand cmd1 = new SqlCommand("SELECT * FROM Tracks ORDER BY Tijdstip", conn))
using (SqlCommand cmd = new SqlCommand("DELETE TOP(1) FROM Tracks", conn))
{
cmd.ExecuteNonQuery();
cmd1.ExecuteNonQuery();
}
}
}
catch (SqlException ex)
{
//Het is niet gelukt om het record uit de database te verwijderen
}
}
private void FormMain_Load(object sender, EventArgs e)
{
//Execute();
}
private Process GetWindowProcess(string windowTitle)
{
foreach (var proc in Process.GetProcessesByName("chrome"))
{
if (proc.MainWindowTitle.IndexOf(windowTitle, StringComparison.OrdinalIgnoreCase) > 0)
{
return proc;
}
}
return null;
}
答案 0 :(得分:5)
你要么在这里得到它:
ShowText((string)result[3])
因为result
是null
,或者你在这里得到它:
label4.Text = message;
因为label4
是null
。
result
null
最可能是Execute
;特别是如果它来自数据库。
更新:现在所有代码都已添加,只需将调用移至InitializeComponent
后调用{ {1}}。你看,InitializeComponent
创建了你在WYSIWYG设计师中删除的所有内容。