0

今日、私はC#クラスの宿題に取り組んでいました。前の問題へのリンク今、私は何が悪いのか理解していない別の問題を抱えています。したがって、最初の投稿で静的パラメーターに問題があったので、それを説明して修正しました。しかし今、「オブジェクト参照がオブジェクトのインスタンスに設定されていない」というエラーが表示されます。タイプミスがないか確認しましたが、まだ実行できません。PS、問題はコンパイル時ではなく、すべてのデータが入力されたとき、およびデータを入力してEnterキーを押した後にNを押したときに発生します。ご覧ください。

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;
        }
    }
}

原因は何ですか?

よろしく、そしていくつかのアドバイスを期待しています。

PS、displayBooksメソッドはまだ終了していません。

4

2 に答える 2

2

問題は回線にありますstatic string[] authorFirstNames;

配列を初期化していないため、オブジェクトはnullです。次のように初期化する必要がありstatic string[] authorFirstNames=new string[1];ます。配列のサイズを指定する必要があります。これが[1]です。これは、配列が1つの文字列を保持できることを意味します。

Array.Resize()を使用して、後で配列のサイズを変更できます。

プロジェクトで許可されている場合はList<string>、文字列の配列の代わりに使用することをお勧めします。

デバッガーでコードを確認することで、この種のことを自分で理解できます。この場合、Visual Studioを使用していると仮定すると、例外がスローされた行が強調表示されます。の値をauthorFirstNames見ると、nullであることがわかります。これは、初期化されていないことを意味します。

于 2011-09-06T00:55:50.327 に答える
1

コード内にこれらの変数を初期化する場所がありません。

static string[] authorFirstNames;
static string[] authorLastNames;
static string[] publisherNames;
static float[] prices;
于 2011-09-06T00:55:03.010 に答える