1

I encountered several problems when creating storing objects to an Array and printing in c# after debugging. What is my problem here? The problem started occurring at adding the objects to the Array and printing the object title.

static void Main(string[] args)
    {
        ComputerGame cg1 = new ComputerGame("Age of Empires",49.99);
        Console.WriteLine(cg1.title);

        ComputerGame cg2 = new ComputerGame("Heroes and Generals", 30.00);
        ComputerGame cg3 = new ComputerGame("Team Fortress 2", 19.50);
        ComputerGame[] gameAlbum = new ComputerGame[5];
        for (int i = 0; i < 5;i++)
        {
            gameAlbum[0] = new ComputerGame();
            gameAlbum[1] = new ComputerGame();
            gameAlbum[2] = new ComputerGame();
        }
        foreach(ComputerGame o in gameAlbum)
        {
            Console.WriteLine(o.title);
        }
    }

public class ComputerGame
{
    public string title;
    public double price;
    public ComputerGame(string title, double price)
    {
        this.title = title;
        this.price = price;

    }
}
3
  • whats the problem in code? Commented May 24, 2016 at 4:33
  • you're not putting the cg1, cg2 and cg3 to the array object gameAlbum. by reading i think when you run you're code you get a null pointer error. Commented May 24, 2016 at 4:35
  • You are creating objects correctly, but then you are recreating gameObjects with default constructor in forloop, that means all the members of a class are null. Sign and create instances individually one by one like you did before the loop Commented May 24, 2016 at 4:55

8 Answers 8

2

Do this instead:

static void Main(string[] args)
{
    ComputerGame cg1 = new ComputerGame("Age of Empires",49.99);
    Console.WriteLine(cg1.title);

    ComputerGame cg2 = new ComputerGame("Heroes and Generals", 30.00);
    ComputerGame cg3 = new ComputerGame("Team Fortress 2", 19.50);
    ComputerGame[] gameAlbum = new ComputerGame[5];

    gameAlbum[0] = cg1;
    gameAlbum[1] = cg2;
    gameAlbum[2] = cg3;

    foreach(ComputerGame o in gameAlbum)
    {
        if (o != null)
           Console.WriteLine(o.title);
    }

    double total = gameAlbum.Where(g => g != null).Sum(g => g.price);
}

Simpler way using a list instead of an array:

List<ComputerGame> games = new List<ComputerGame>();
games.Add(new ComputerGame("Age of Empires", 49.99));
games.Add(new ComputerGame("Heroes and Generals", 30.00));
games.Add(new ComputerGame("Team Fortress 2", 19.50));
games.Add(new ComputerGame("Portal", 19.50));
games.Add(new ComputerGame("Portal 2", 29.50));

foreach(ComputerGame game in games)
{
    if (game != null)
        Console.WriteLine($"Title: {game.title}, Price: {game.price}");
}

double total = games.Sum(p => p.price);
Sign up to request clarification or add additional context in comments.

11 Comments

lets say i want to display the total price in the objects how would i do that?
For that I'd ask a separate StackOverflow question :-)
@J.Koh for sum of the computers..try this way.. double total = games.Sum(p => p.price);
@PrasadRaja it throws a null pointer reference after i added double total = gameAlbum.Sum(p => p.price);
@J.Koh i added new answer...check it
|
1

Remove for loop. Create instances with parameters like you did. Sign it to array like gameObjs[0] = cg1, etc.

Comments

0

Gamealbum 0, 1 and 2 are created with a parameterless constructor but you require two parameters.

Try addimg another constructor to your class or include the parameters in the new-statements.

On a sidenote, there is no reason to loop 5 times since the loop just does tje same thing all the time.

Comments

0
ComputerGame[] gameAlbum = new ComputerGame[5];
        for (int i = 0; i < 5;i++)
        {
            gameAlbum[0] = new ComputerGame();
            gameAlbum[1] = new ComputerGame();
            gameAlbum[2] = new ComputerGame();
        }

In the for loop, you are initiating the first three elements of array again and again. You are also calling the default constructor of ComputerGame class. So title element of ComputerGame won't get initialized. So you won't see anything printed on console.

Comments

0

Give this a go:

ComputerGame cg1 = new ComputerGame("Age of Empires",49.99);
ComputerGame cg2 = new ComputerGame("Heroes and Generals", 30.00);
ComputerGame cg3 = new ComputerGame("Team Fortress 2", 19.50);

ComputerGame[] gameAlbum = new ComputerGame[5];
gameAlbum[0] = cg1;
gameAlbum[1] = cg2;
gameAlbum[2] = cg3;

foreach(ComputerGame o in gameAlbum)
{
    if (o != null)
        Console.WriteLine(o.title);
}

Comments

0

I don't see any assignments to the array "gameAlbum" in your for loop.
Try the below one and see.

static void Main(string[] args)
{
    ComputerGame[] gameAlbum = new ComputerGame[3];

    gameAlbum[0] = new ComputerGame("Age of Empires",49.99);
    gameAlbum[1] = new ComputerGame("Heroes and Generals", 30.00);
    gameAlbum[2] = new ComputerGame("Team Fortress 2", 19.50);

    foreach(ComputerGame o in gameAlbum)
    {
       Console.WriteLine(o.title);
    }
}

public class ComputerGame
{
    public string title;
    public double price;
    public ComputerGame(string title, double price)
    {
        this.title = title;
        this.price = price;

    }
}

Comments

0

The problem with your code is that the default constructor for ComputerGame is overridden. There is no constructor such as:

public ComputerGame()
{...
}

Hence, you can do as follows:

gameAlbum[0] = new ComputerGame("Age of Empires",49.99);
gameAlbum[1] = new ComputerGame("Heroes and Generals", 30.00);
gameAlbum[2] = new ComputerGame("Team Fortress 2", 19.50);

You might also get errors as you are making use of only 3 of the 5 array elements. Hence, use List<> instead of arrays for dynamically creating the objects when needed.

Comments

0

Try with this code..

Looping the data & get sum of the price

         ComputerGame[] gameAlbum = new ComputerGame[5];
        gameAlbum[0] = new ComputerGame("Age of Empires", 49.99);
        gameAlbum[1] = new ComputerGame("Heroes and Generals", 30.00);
        gameAlbum[2] = new ComputerGame("Team Fortress 2", 19.50);
        gameAlbum[3] = new ComputerGame("Portal", 19.50);
        gameAlbum[4] = new ComputerGame("Portal 2", 29.50);

        //looping the data
        foreach (ComputerGame item in gameAlbum)
        {
            if (item != null)
                Response.Write("Name : " + item.title + " Price : " + item.price);
        }
        //get the sum of the price
        double total = gameAlbum.Sum(p => p.price);
        Response.Write(total);

4 Comments

is it possible to use just arrays instead of list?
you means instead of List<ComputerGame> ? you want to make it as array ?
the original code uses array object. the List<ComputerGame> is just a suggestion by @gotnull.
i updated the code to array.. it this is useful mark this as answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.