Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6539637
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T10:52:56+00:00 2026-05-25T10:52:56+00:00

Earlier today I was working on my homework for my C# class LINK TO

  • 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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T10:52:56+00:00Added an answer on May 25, 2026 at 10:52 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

It was working earlier today... :S but now, whenever I click a link in
Earlier today I was working on a PHP 5.3+ application, which meant I was
Earlier today I asked similar problem to find the maximum element which is common
I can't quite figure out what's going wrong. This was working earlier today and
I created this earlier on today, but it is not working. Location manager returns
I have a wordpress website which suddenly stopped working today. When I look at
This code was working earlier today, then randomly stopped, something must of changed, but
I'm running Emacs 22.3 in Windows. Earlier today I had gdb working within Emacs
Working with developer phone earlier, today i tried my hand on emulator i do
Earlier today I was looking for an C# API which provides stock quotes for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.