-1

I am just starting to learn C# in In Visual Studios 2010, and using Bob Tabor's LearnVisualStudio.Net to help. I am on Day 3's homework and wanted to get extra practice in creating classes and defining public and private variables in them and then creating string statements that can be collected into one of the classes for a Console.Write method that the program will display. I feel I have created the public and private variables fine, and even the string statements, but when I go to gather them up and print them, I get an error statement: NullReferenceException was unhandled. I don't know what I am doing wrong. I have pasted the important parts (I think) of each class below.

In the Motorcycles class:

public string GetMotorcycleInfo()

    {
        string message = "";
        message = "A " + Make + " " + Model + " " + EngineSize + " " + "is fast, but         `             expensive at " 
        + Cost + "!";
        return message;
    } 

in the Bicycles class:

 public string GetBikeInfo()

    {
        string message = "";
        message = "A " + Brand + " " + BikeType + NumberOfGears + "bicycle";
        return message;
    }

In the Mopeds class:

 public Motorcycles Cycles
    {
        get { return motorcycles; }
        set { motorcycles = value; }
    }

    public Bicycles Bikes
    {
        get { return bicycles; }
        set { bicycles = value; }
    }

    public void DisplayInfo()
    {
        Console.Write("A " + Cycles.GetMotorcycleInfo());
        //Console.Write(" is fast, but expensive.");
        Console.Write("A " + Bikes.GetBikeInfo());
        //Console.Write(" is slow, but steady.");
        Console.Write("But a " + Make + " " + Model + " Moped, with " + Mpg
         + "mpg, and costing only " +    
        Cost + " is a deal you don't want to pass up!");

    }

And, in the Program class:

 class Program
{
    static void Main(string[] args)
    {
        Motorcycles myBBBike = new Motorcycles();
        myBBBike.Make="Harley Davidson";
        myBBBike.Model="Golden Sun";
        myBBBike.EngineSize="650 cc";
        myBBBike.Cost="$30,000";

        Bicycles mySBSBike = new Bicycles();
        mySBSBike.Brand = "Schwin";
        mySBSBike.NumberOfGears = 10;
        mySBSBike.BikeType = "Off Road";
        mySBSBike.Price = "$500.00";

        Mopeds myMopeds = new Mopeds();
        myMopeds.Make = "Renegade";
        myMopeds.Model = "TPGS-805";
        myMopeds.Mpg = "75 mpg";
        myMopeds.Cost = "$599.00";

        myMopeds.DisplayInfo();

        Console.ReadLine();
    }
}

}

Any suggestions would be appreciated. Thank you

user2865039
  • 19
  • 1
  • 3
  • 1
    Put a breakpoint in your code where the error occurs and check your variables to see which one is null. – Kendall Frey May 13 '14 at 14:24
  • Look at the NullReferenceException information. It will tell you the exact line number that it occurs on. – Daniel Mann May 13 '14 at 14:25
  • I suspect that you are not assigning values to your `Cycles` and `Bikes` properties. – Kendall Frey May 13 '14 at 14:25
  • Currently my go-to comment; particularly useful if you are just starting out - Read and understand [this quality advice on debugging](http://ericlippert.com/2014/03/05/how-to-debug-small-programs/) from Eric Lippert –  May 13 '14 at 14:29

2 Answers2

1

You haven't added the bicycles or Motorcycles to the myMopeds instance yet:

add this before your myMopeds.DisplayInt();

myMopeds.Bikes = mySBSBike;
myMopeds.Cycles = myBBBike;

In addition, you should get your naming conventions straight. A single motorcycle is a Motorcycle, not a Motorcyles. This pluralization is confusing for others and for yourself. You should change your class naming conventions to match this as well, so public class Motorcycle,public class Bicycle, etc.

DLeh
  • 23,806
  • 16
  • 84
  • 128
0

You'll need to post the variables. The NullReferenceException happens when you try to reference something that is null (pretty self-explanatory).

My guess is you're never setting your Cycles or Bikes references to an instance of that class.

You'll either need to manually assign them after you instantiate your Mopeds class:

myMopeds.Bikes = mySBSBike;
myMopeds.Cycles = myBBBike;

Or you can add them as a constructor parameter to your Mopeds class

public Mopeds(Bicyles b, Motorcycles m) {
    this.Bikes = b;
    this.Cycles = m;
}

And create your Mopeds class like so:

Mopeds myMopeds = new Mopeds(mySBSBikes, myBBBike);

There are lots of ways to go about it; however you go about it though, you have to actually gives those variables a value otherwise they'll always be null!

Also, like the user above me pointed out, you should definitely get your naming conventions correct, the pluralizing can be a bit confusing.

Ben Black
  • 3,751
  • 2
  • 25
  • 43