开发者

More problems with arrays, methods, and ref parameter

Earlier today I was working on my homework for my开发者_Go百科 C# class LINK TO PREVIOUS PROBLEM and now I have another problem which I am not understanding what is wrong. So with the first post I had a problem with static parameters so I got that explained and fixed. But now, I'm getting an error saying than an "object reference not set to an instance of an object". I checked for any typos and still can not run it. PS, problem appears not when compiling, but rather when all data is entered and when I press N after typing in the data and hitting enter. Please take a look.

using System;

public class Repository 
{
    static string[] titles;
    static string[] authorFirstNames;
    static string[] authorLastNames;
    static string[] publisherNames;
    static float[] prices;
    static int number;

    static void Main(string[] args)
    {
        string title = "";
        string authorFirst = "";
        string authorLast = "";
        string publisherName = "";
        float price = 0;

        getBookInfo(ref title, ref authorFirst, ref authorLast, ref publisherName, ref price);      
        displayBooks(titles, authorFirstNames, authorLastNames, publisherNames, prices, number);
    }

    static void getBookInfo(ref string title, ref string authorFirst, 
                            ref string authorLast, ref string publisherName, 
                            ref float price)
    {
        string continued;
        string float_num;
        int i = 0;

        titles = new string[50];

        do
        {
            Console.Write("Title of book: ");
            title = Console.ReadLine();
            Console.Write("Authors first name: ");
            authorFirst = Console.ReadLine();
            Console.Write("Authors last name: ");
            authorLast = Console.ReadLine();
            Console.Write("Publishers Name: ");
            publisherName = Console.ReadLine();
            Console.Write("Price: ");
            float_num = Console.ReadLine();
            Console.Write("Add another book? Y/N ");
            continued = Console.ReadLine().ToLower();

            price = float.Parse(float_num);

            titles[i] = title;
            authorFirstNames[i] = authorFirst;
            authorLastNames[i] = authorLast;
            publisherNames[i] = publisherName;
            prices[i] = price;

            number = i;

            i++;
        }
        while (continued == "y");
    }

    static void displayBooks(string[] titles, string[] authorFirstNames, 
                             string[] authorLastNames, string[] publisherNames, 
                             float[] prices, int number)
    {
        foreach (string title in titles)
        {
            Console.WriteLine(title);
            if(title == null)
                break;
        }
    }
}

What's the cause?

Regards, and hope for some advice.

PS, displayBooks method isn't finished yet.


The problem is with the line static string[] authorFirstNames;

You're not initializing your array, so the object is null. You need to initialize it like this: static string[] authorFirstNames=new string[1]; You have to specify the size of the array, which is what the [1] is. It means the array can hold 1 string.

You can use Array.Resize() to resize the array at a later point.

If the project permits it, you may want to use a List<string> instead of an array of strings.

You can figure this sort of thing out on your own by looking at the code in a debugger. In this case, assuming you're using Visual Studio, it highlights the line where the exception is thrown. If you look at the value of authorFirstNames, you see it's null, which means it was never initialized.


I don't see any place in your code initialize these variable:

static string[] authorFirstNames;
static string[] authorLastNames;
static string[] publisherNames;
static float[] prices;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜