0

Earlier today I was working on my homework for my 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.

5
  • 1
    What line do you get the exception at? Commented Sep 6, 2011 at 0:47
  • 1
    Make sure to tag your homework questions with the homework tag. Then people know to help you as opposed to just telling the answer =) Commented Sep 6, 2011 at 0:48
  • It is line 52, and I will add another tag next time, thanks. Commented Sep 6, 2011 at 0:52
  • When are you initializing any of the arrays (besides titles)? Commented Sep 6, 2011 at 0:54
  • Ahaa! There's my problem! I need to add initializers right under line 32. Good eye! Commented Sep 6, 2011 at 0:57

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Yes, I'm looking at this right now, and no, I'm using MonoDevelop. Trying to stay out of Micro$oft.
You're using MonoDevelop purely because you don't like Microsoft - even when there's a free version of Visual Studio.. and yet you're using C#?
@Rob: It's possible that he's forced to use C#(as in, the course at his school/college is taught using C#), so he preferred mono as a way of staying as far as HE could from Microsoft =) Same tough luck follows me in my university :(
@Abel Even so, deliberatly going out of your way to use an inferior (Don't get me wrong, Mono is incredible, but it's not perfect) product purely out of spite is a bit counter-productive. It's like buying a Mac and running every single application under WINE just to avoid microsoft.
1

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

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

Comments

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.