-3

I am getting the exception

NullReferenceException was unhandled

I have tried and tried again to solve this but hours later and I'm coming here for help.

This is where I'm getting the error:

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);
}

I know it's not a lot to work with above, but here is the other section of the code, I am trying to create object references instead of using lists etc, these items are meant to show in a 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;
        }
    }
}
Alex
  • 12,468
  • 28
  • 56
  • 1
    Why are you using a For.. loop with a single, hard coded iteration? – Ron Beyer Apr 25 '15 at 01:44
  • 1
    What is "taskBox"?, also, you for loop is a bit redundant.. and the "Convert.ToString" is also redundant if you're already using a string type object.. – User2012384 Apr 25 '15 at 01:45

1 Answers1

2

You setup your taskItems in FormMain, but there is no call to this method in code that fails.

Possible fix - call the method

Form1 objectForm1 = new Form1();
objectForm1.FormMain();
for (int i = 0; i < 1; i++) ....

For standard steps to investigate NRE check out What is a NullReferenceException, and how do I fix it?

Community
  • 1
  • 1
Alexei Levenkov
  • 94,391
  • 12
  • 114
  • 159