将对象列出到作为新项目属性的新列表中

时间:2018-11-05 14:21:29

标签: c# list properties

所以我每次运行此命令时都会出错 System.NullReferenceException Ball.boxContent.get返回null

我在此行收到此错误     box1.Content.Add(Myballs [5]);

我正在做的是制作不同颜色,重量和材料的球 将它们添加到名为MyBalls的列表中 然后 将它们从该列表添加到一个框(该框具有一个名为“内容”的列表的属性)

完成此操作后,我应该调用一些box函数并计算重量并打印里面的内容。我还没达到这个阶段

任何帮助将不胜感激

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;

namespace Ball
{

class Ball
{
    private string color;
    private string material;
    private double weight;

    public string Color { get; set; }
    public string Material { get; set; }
    public double Weight { get; set; }

    public Ball() { }

    public Ball(string color, double weight, string material)
    {
        this.Color = color;
        this.Material = material;
        this.Weight = weight;
    }

    public bool changecolorbymorethanweight(string c, double w)
    {
        if (Weight >= w)
        {
            Color = c;
            return true;
        }

        else return false;
    }

    public void printall()
    {
        Console.WriteLine("Η μπάλα είναι χρώματος {0}\nυλικού {1}\nκαι βάρους {2}", Color, Material, Weight);
    }
}

-

class Box
{
    private double height;
    private double width;
    private double length;
    private string material;
    public List<Ball> content = new List<Ball>();
    private double weight;

    public double Height { get; set; }
    public double Width { get; set; }
    public double Length { get; set; }
    public string Material { get; set; }
    public List<Ball> Content { get; set; }
    public double Weight
    {
        get
        {
            double varos = 0;
            for (int i = 0; i < content.Count; i++)
            {
                varos = varos + content[i].Weight;
            }
            weight = varos;
            return weight;
        }
        set
        {
            Weight = weight;
        }
    }

    public void removefirst3()
    {
        Content.RemoveAt(0);
        Content.RemoveAt(1);
        Content.RemoveAt(2);
    }

    public void removeall()
    {
        Content.Clear();
    }

    public void removeallbycolor(string rbc)
    {
        for (int i = 0; i < Content.Count; i++)
            if (Content[i].Color == rbc)
                Content.RemoveAt(i);
    }

    public int getnumberbycolor(string gnbc)
    {
        int counter = 0;
        for (int i = 0; i < Content.Count; i++)
            if (Content[i].Color == gnbc)
                counter++;
        return counter;
    }

    public bool removeallmorethanbyweight(double rbwb)
    {
        bool result = false;
        for (int i = 0; i < Content.Count; i++)
            if (Content[i].Weight >= rbwb)
            {
                Content.RemoveAt(i);
                result = true;
            }
        return result;
    }

    public bool removealllessthanbyweight(double rbws)
    {
        bool result = false;
        for (int i = 0; i < Content.Count; i++)
            if (Content[i].Weight >= rbws)
            {
                Content.RemoveAt(i);
                result = true;
            }
        return result;
    }

    public bool removeallbymaterial(string rabm)
    {
        bool result = false;
        for (int i = 0; i < Content.Count; i++)
            if (Content[i].Material == rabm)
            {
                Content.RemoveAt(i);
                result = true;
            }
        return result;
    }

    public void changecolorbyposition(int p, string c)
    {

        for (int i = 0; i < Content.Count; i++)
            if (i == p)
                Content[i].Color = c;

    }

    public void printall()
    {
        Console.WriteLine("Tο κουτί μας έχει \n{0} ύψος\n{1} πλάτος\n{2} μήκος\nείναι φτιαγμένο από {3}\nτο βάρος του είναι {4}\n και μέσα του έχει:\n{5}", Height, Width, Length, Material, Weight, Content);
    }

    public Box() { }
    public Box(double length, double width, double height, string material, double weight, List<Ball> content)
    {
        this.Length = length;
        this.Width = width;
        this.Height = height;
        this.Material = material;
        this.Weight = weight;
        this.Content = content;
    }
    public Box(double length, double width, double height, string material)
    {
        this.Length = length;
        this.Width = width;
        this.Height = height;
        this.Material = material;

    }
}

-

class Program
{
    static void Main(string[] args)
    {
        Box box1 = new Box(1.2, 1.3, 1.8, "χάρτινο");
        Box box2 = new Box(1.2, 2.3, 2.5, "ξύλινο");

        Ball b1 = new Ball("Red", 2.5, "μεταλλική");
        Ball b2 = new Ball("Red", 2.5, "μεταλλική");
        Ball b3 = new Ball("Red", 2.5, "μεταλλική");

        Ball b4 = new Ball("Red", 1.5, "μεταλλική");
        Ball b5 = new Ball("Red", 1.5, "μεταλλική");

        Ball b6 = new Ball("Black", 0.5, "πλαστική");
        Ball b7 = new Ball("Black", 0.5, "πλαστική");
        Ball b8 = new Ball("Black", 0.5, "πλαστική");
        Ball b9 = new Ball("Black", 0.5, "πλαστική");
        Ball b10 = new Ball("Black", 0.5, "πλαστική");

        Ball b11 = new Ball("Άσπρο", 1.1, "λαστιχένια");
        Ball b12 = new Ball("Άσπρο", 1.1, "λαστιχένια");
        Ball b13 = new Ball("Άσπρο", 1.1, "λαστιχένια");
        Ball b14 = new Ball("Άσπρο", 1.1, "λαστιχένια");
        Ball b15 = new Ball("Άσπρο", 1.1, "λαστιχένια");

        List<Ball> Myballs = new List<Ball>() { b1, b2 };

        Myballs.Capacity = 15;

        Myballs.Add(b1);
        Myballs.Add(b2);
        Myballs.Add(b3);
        Myballs.Add(b4);
        Myballs.Add(b5);
        Myballs.Add(b6);
        Myballs.Add(b7);
        Myballs.Add(b8);
        Myballs.Add(b9);
        Myballs.Add(b10);
        Myballs.Add(b11);
        Myballs.Add(b12);
        Myballs.Add(b13);
        Myballs.Add(b14);
        Myballs.Add(b15);

        box1.Content.Add(Myballs[5]);
        box1.Content.Add(Myballs[6]);
        box1.Content.Add(Myballs[7]);
        box1.Content.Add(Myballs[8]);
        box1.Content.Add(Myballs[9]);
        box1.Content.Add(Myballs[10]);
        box1.Content.Add(Myballs[11]);
        box1.Content.Add(Myballs[12]);
        box1.Content.Add(Myballs[13]);
        box1.Content.Add(Myballs[14]);

        box2.Content.Add(Myballs[0]);
        box2.Content.Add(Myballs[1]);
        box2.Content.Add(Myballs[2]);
        box2.Content.Add(Myballs[3]);
        box2.Content.Add(Myballs[4]);

        Console.ReadKey();
        }
    }
}

1 个答案:

答案 0 :(得分:1)

Content类中初始化Box属性:

public List<Ball> Content { get; set; } = new List<Ball>();

...或在添加任何球之前使用Main方法:

box1.Content = new List<Ball>();
box1.Content.Add(Myballs[5]);
...