0

So I'm trying to store fields from a text file into an array. But when I reach the line

data = record.Trim().Split('*');

I get an error saying "Object reference not set to an instance of an object."

public bool matchCustomer(string accountID, string record)
    {
        String[] data = new String[5];
        data = null;
        data = record.Trim().Split('*');
        this.accountNumber = data[0];
        if (accountID == this.accountNumber)
            {
                return true;
            }
        else
        {
           return false;
          }
       }

Here is where the method is called:

public bool findCustomer(string accountNumber)
    {
        string record = Global.currentFile.getNextRecord(ref endOfFile);
        bool okay = Global.customer.matchCustomer(accountNumber, record);
        return okay;
    }

Here is getNextRecord:

   public string getNextRecord(ref Boolean endOfFileFlag)
    {
        string nextRecord;

        endOfFileFlag = false;
        nextRecord = reader.ReadLine();

        if (nextRecord == null)
        {
            endOfFileFlag = true;
        }
        else
        {
            recordReadCount += 1;
        } // end if

        return (nextRecord);
    } // end getNextRecord
2
  • Have you put a breakpoint in there? Seems like record is null. Also you call get next record, but what happens if you're at the end of the file? Commented Mar 6, 2016 at 4:19
  • I suspect record must be a null. Commented Mar 6, 2016 at 4:19

2 Answers 2

2

First, you can simplify your code by replacing:

String[] data = new String[5];
data = null;
data = record.Trim().Split('*');

with just a single line:

string[] data = record.Trim().Split('*');

This is a correct statement, because you don't know the max index (elements) of the string[] array returning by Split() function.

Second, make sure that record!=null, and also it has a string containing "*" characters used as delimiter in Split() function.

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

Comments

2

Let's look at this line of code

data = record.Trim().Split('*');

If a NullReferenceException occurred here, that means you must be calling a method on a null object. In this case, the only possible object is record.

Now we know what is null, but why is it null? Let's look at this method:

public bool findCustomer(string accountNumber)
{
    string record = Global.currentFile.getNextRecord(ref endOfFile);
    bool okay = Global.customer.matchCustomer(accountNumber, record);
    return okay;
}

Apparently you are using the return value of getNextRecord to call matchCustomer. This means that getNextRecord must return be returning null! So let's find out why getNextRecord returns null:

public string getNextRecord(ref Boolean endOfFileFlag)
{
    string nextRecord;

    endOfFileFlag = false;
    nextRecord = reader.ReadLine();

    if (nextRecord == null)
    {
        endOfFileFlag = true;
    }
    else
    {
        recordReadCount += 1;
    } // end if

    return (nextRecord);
} // end getNextRecord

If the method return nextRecord, that means nextRecord is null. And how did you get nextRecord? reader.ReadLine!

So the ultimate reason why record is null is that reader.ReadLine is null.

To avoid this exception, you need to first check whether record is null, then call the method on it:

if (record != null) {
    data = record.Trim().Split('*');
} else {
    // do other stuff
}

In C# 6, this can be simplified to

data = record?.Trim().Split('*');

If record is null, data will be null too!

Also, note that this code is redundant:

String[] data = new String[5];
data = null;

You are creating a bunch of strings and then setting the array to null. What's the point? So you can just remove that, and change the next line to:

string[] data = record?.Trim().Split('*');

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.