我有第一个名为:
的班级 Appartments[] appartments;
我想复制“Number”的值并将其粘贴到“AppartmentInfo.cs”类(我猜它们被称为参数)。
public static void FindFloor(Appartments[] appartments, int amount,out int floorToRound,out AppartmentInfo[] AInfo)
{
floorToRound = 0;
AInfo = new AppartmentInfo[Max];
for (int i = 0; i < amount; i++)
{
AInfo[i].Floor1 = Convert.ToDouble(appartments[i].Number);
Console.WriteLine(appartments[i].Number);
if (appartments[i].Number < 27)
{
appartments[i].Number = 1;
}
else
{
appartments[i].Number /= 27;
}
}
}
如果有需要,我可以发布整个代码。我收到错误: NullReferenceException未处理。
该行错误:
AInfo[i].Floor1 = Convert.ToDouble(appartments[i].Number);
完整代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace P3._1
{
class Program
{
public const int Max = 20;
static void Main(string[] args)
{
int amount, AmountOfRooms, price, floor, floorToRound;
Appartments[] appartments;
AppartmentInfo[] AInfo;
//AppartmentInfo AInfo = new AppartmentInfo();
ReadData(out appartments, out amount);
TextImput(out floor, out AmountOfRooms, out price);
FindFloor(appartments, amount,out floorToRound,out AInfo);
Console.ReadKey();
}
public static void ReadData(out Appartments[] appartments, out int amount)
{
amount = 0;
appartments = new Appartments[Max];
using (StreamReader reader = new StreamReader("C:\\Users\\Andrius\\Desktop\\Mokslams\\C#\\Pratybos\\P3\\P3.1 be konstruktoriaus\\Appartments.csv", true))
{
reader.ReadLine();
string line = null;
while (null != (line = reader.ReadLine()))
{
string[] values = line.Split(',');
int number = int.Parse(values[0]);
int area = int.Parse(values[1]);
int rooms = int.Parse(values[2]);
int sellingCost = int.Parse(values[3]);
int phone = int.Parse(values[4]);
Appartments appartmentsObj = new Appartments(number, area, rooms, sellingCost, phone);
appartments[amount++] = appartmentsObj;
}
}
}
public static void TextImput(out int floor, out int AmountOfRooms, out int price)
{
AmountOfRooms = 0;
price = 0;
Console.WriteLine("Iveskite buto aukštą: ");
floor = int.Parse(Console.ReadLine());
Console.WriteLine("Iveskite kambariu skaiciu: ");
AmountOfRooms = int.Parse(Console.ReadLine());
Console.WriteLine("Maksimali kaina: ");
price = int.Parse(Console.ReadLine());
}
public static void FindFloor(Appartments[] appartments, int amount,out int floorToRound,out AppartmentInfo[] AInfo)
{
floorToRound = 0;
AInfo = new AppartmentInfo[Max];
for (int i = 0; i < amount; i++)
{
AInfo[i].Floor1 = Convert.ToDouble(appartments[i].Number);
Console.WriteLine(appartments[i].Number);
if (appartments[i].Number < 27)
{
appartments[i].Number = 1;
}
else
{
appartments[i].Number /= 27;
}
}
}
}
}
答案 0 :(得分:1)
您正在尝试访问AInfo[i].Floor1
属性,但如果AppartmentInfo
是一个类,则AInfo
数组中的每个元素都将为null
。您需要先创建一个AppartmentInfo
对象:
for (int i = 0; i < amount; i++)
{
AInfo[i] = new AppartmentInfo
{
Floor1 = Convert.ToDouble(appartments[i].Number)
};
编辑/更新:完整/可编辑程序以演示语法:
public class Program
{
public static void Main()
{
var meow = new Foo[3];
for (int i = 0; i < 3; ++i)
{
meow[i] = new Foo
{
Bar = Convert.ToDouble("3.141"),
Blah = "Another Test",
};
if (i < 2)
{
meow[i].Buzz = 3;
}
}
}
class Foo
{
public double Bar { get; set; }
public string Blah { get; set; }
public int Buzz { get; set; }
}
}
答案 1 :(得分:0)
你的for循环从零迭代到数量 - 1然后尝试在Appartments数组中查找该索引的数组条目。
金额字段传入的值可能大于公寓数组的大小。
很难说不知道公寓阵列是什么样的,以及金额参数中的值是什么。