0

So in a few words i'm getting an error whenever i run this System.NullReferenceException Ball.boxContent.get returned null

i get this error on this line box1.Content.Add(Myballs[5]);

what I'm doing is creating balls of different colors,weight and material adding them into a list called MyBalls then adding them from that list into a box(that has a property that is a list called Content)

after this is done I'm supposed to call some of the box function and calculate the weight and print what is inside. Not yet reached this stage as i cannot

any help would be greatly appreciated

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();
        }
    }
}
Chris A.
  • 35
  • 6
  • Your Content property will always be null. Either write the getter as return content; or remove the backing field content ans initialize the Property with new List() – Klaus Gütter Nov 05 '18 at 14:23

1 Answers1

1

Initialize the Content property, either in the Box class:

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

...or in the Main method before you add any balls:

box1.Content = new List<Ball>();
box1.Content.Add(Myballs[5]);
...
mm8
  • 163,881
  • 10
  • 57
  • 88