Object reference not set to an instance of an object. - C#
Формулировка задачи:
помогите разобраться как написать правильно :
Листинг программы
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Xml;
- using System.Xml.Serialization;
- using System.IO;
- namespace writtter
- {
- class Program
- {
- static void Main(string[] args)
- {
- ComputersData data = new ComputersData();
- List<ComputersData> CompList = new List<ComputersData>();
- ComputersData[] array = new ComputersData[6];
- array[0].name = "PPS systems";
- array[0].hard = 2500;
- array[0].price = 900;
- array[0].performance = 3;
- CompList.Add(array[0]);
- array[1].name = "Sunny slime";
- array[1].hard = 150;
- array[1].price = 80;
- array[1].performance = 2;
- CompList.Add(array[1]);
- array[2].name = "Sunny Home edition";
- array[2].hard = 300;
- array[2].price = 100;
- array[2].performance = 2.6;
- CompList.Add(array[2]);
- array[3].name = "Sunny PRO";
- array[3].hard = 600;
- array[3].price = 550;
- array[3].performance = 4 ;
- CompList.Add(array[3]);
- array[4].name = "TH";
- array[4].hard = 450;
- array[4].price = 200;
- array[4].performance = 7 ;
- CompList.Add(array[4]);
- array[5].name = "TH Home";
- array[5].hard = 280;
- array[5].price = 90;
- array[5].performance = 2.6;
- CompList.Add(array[5]);
- XmlSerializer SRwriter = new XmlSerializer(typeof(List<ComputersData>));
- StreamWriter file = new StreamWriter(@"D:\education\workSpace 8 lab OOP\file\MyInformationComputers.xml");
- SRwriter.Serialize(file, CompList);
- file.Close();
- }
- }
- [Serializable]
- public class ComputersData
- {
- public string name;
- public double price;
- public double performance;
- public double hard;
- }
- }
разобрался .Нужно для каждого элемента массива вызывать конструктор .Не привычно .
Это можно обойти как-то стороной ?
Решение задачи: «Object reference not set to an instance of an object.»
textual
Листинг программы
- arr[0]=(new ComputerData(){name="dgs", price=00.1d, performance=9910.2d, hard=51d});
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д