-3
我得到異常的NullReferenceException是unhandles使用Windows窗體C#
NullReferenceException was unhandled
我已經嘗試了一次又一次之後,解決這一點,但小時,我來這裏尋求幫助。
這是我收到的錯誤:
Form1 objectForm1 = new Form1();
for (int i = 0; i < 1; i++)
{
objectForm1.taskBox.Items.Add(
objectForm1.taskItems[i].TaskName +
objectForm1.taskItems[i].TaskDescription +
objectForm1.taskItems[i].TaskPriority +
objectForm1.taskItems[i].TaskDueDate +
objectForm1.taskItems[i].TaskCompletion);
}
我知道這是不是很多與上述工作,但這裏是代碼的另一部分,我想創建對象的引用來使用列表等,這些物品是指在ListBox
顯示:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Programming_and_Data_Structures_ToDoList
{
public partial class Form1 : Form
{
public ObjectFile[] taskItems = new ObjectFile[10];
public Form1()
{
InitializeComponent();
}
public void FormMain()
{
//creating the form links----|
Form2 objectForm2 = new Form2();
Form1 objectForm1 = new Form1();
//---------------------------|
//ABOVE IS CREATING THE OBJECT REFERENCE
//BELOW IS SETTING THE VALUES
string taskName = objectForm2.TaskNameBox.Text;
string taskDescription = objectForm2.TaskDescBox.Text;
decimal taskPriority = objectForm2.PriorityUpDown.Value;
string taskDueDate = objectForm2.DateTimePicker.Value.ToShortDateString();
string taskCompletion = objectForm2.completion;
for (int i = 0; i < 1; i++)
{
taskItems[i] = new ObjectFile(Convert.ToString(taskName), Convert.ToString(taskDescription), Convert.ToInt32(taskPriority), Convert.ToString(taskDueDate), taskCompletion);
}
}
private void newToolStripMenuItem_Click(object sender, EventArgs e)
{
Form2 Form2Object = new Form2(); //creates the object link form2
Form2Object.ShowDialog(); //Shows Form2 (Task input menu)
}
private void editToolStripMenuItem_Click(object sender, EventArgs e)
{
Environment.Exit(0); //exits the whole application
}
public void optionsToolStripMenuItem_Click(object sender, EventArgs e)
{
}
}
//OBJECT CLASS
public class ObjectFile
{
public string TaskName { get; set; }
public string TaskDescription { get; set; }
public decimal TaskPriority { get; set; }
public string TaskDueDate { get; set; }
public string TaskCompletion { get; set; }
public ObjectFile(string taskName, string taskDescription, decimal taskPriority, string taskDueDate, string taskCompletion)
{
TaskName = taskName;
TaskDescription = taskDescription;
TaskPriority = taskPriority;
TaskDueDate = taskDueDate;
TaskCompletion = taskCompletion;
}
}
}
? –
什麼是「taskBox」?,此外,for循環有點冗餘..如果您已經使用字符串類型對象,則「Convert.ToString」也是多餘的。 – User2012384