4 Replies - 464 Views - Last Post: 12 October 2012 - 05:33 AM Rate Topic: -----

#1 HopelessDev  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 136
  • Joined: 10-August 12

NullReferenceException

Posted 12 October 2012 - 04:57 AM

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TaxApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            TaxPayer[] payers = new TaxPayer[5];

            for (int i = 0; i < payers.Length; i++)
            {
                Console.Write("Enter SSN number: ");
                //payers[i].SocialSecurityNumber = (new TaxPayer().SocialSecurityNumber = Console.ReadLine());
                payers[i].SocialSecurityNumber = Console.ReadLine();
                Console.Write("Enter Yearly Gross Income: ");
                payers[i].YearlyGrossIncome = double.Parse(Console.ReadLine());
            }
            for (int i = 0; i < payers.Length; i++)
            {
                payers[i].getRates();
                Console.WriteLine(payers[i]);
            }
            Console.ReadLine();
        }
    }
}



Im getting a NullReferenceException from this line

payers[i].SocialSecurityNumber = Console.ReadLine();



Please help. Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: NullReferenceException

#2 janne_panne  Icon User is offline

  • WinRT Dev
  • member icon

Reputation: 428
  • View blog
  • Posts: 1,047
  • Joined: 09-June 09

Re: NullReferenceException

Posted 12 October 2012 - 05:05 AM

This is what your payers array looks like at the moment you receive the error:
TaxPayer[0] = null;
TaxPayer[1] = null;
TaxPayer[2] = null;
TaxPayer[3] = null;
TaxPayer[4] = null;

And that's the reason for your null reference exception.

So what you must do is initialize the elements of array before trying to use them:
for (int i = 0; i < payers.Length; i++)
  payers[i] = new TaxPayer();


Was This Post Helpful? 0
  • +
  • -

#3 HopelessDev  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 136
  • Joined: 10-August 12

Re: NullReferenceException

Posted 12 October 2012 - 05:10 AM

I see. I though so too . But did you see the one line I commented? I tried that too and it did not work even though I made a new TaxPayer object. Do you know why?
Was This Post Helpful? 0
  • +
  • -

#4 janne_panne  Icon User is offline

  • WinRT Dev
  • member icon

Reputation: 428
  • View blog
  • Posts: 1,047
  • Joined: 09-June 09

Re: NullReferenceException

Posted 12 October 2012 - 05:22 AM

//payers[i].SocialSecurityNumber = (new TaxPayer().SocialSecurityNumber = Console.ReadLine());

The commented line can be deconstructed into three lines, as it is doing three things:
1. It creates a new taxpayer
2. It sets the taxpayer's SSN to console input
3. It sets the taxpayer to payers[i].SSN

And the third line is what's going wrong.
Here is a code sample about what's happening too:
// 1.
TaxPayer tp = new TaxPayer();
// 2.
tp.SocialSecurityNumber = Console.ReadLine();
// 3.
payers[i].SocialSecurityNumber = tp;



So what would be right is of course this:
// 3.
payers[i] = tp;


This post has been edited by janne_panne: 12 October 2012 - 05:23 AM

Was This Post Helpful? 4
  • +
  • -

#5 HopelessDev  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 136
  • Joined: 10-August 12

Re: NullReferenceException

Posted 12 October 2012 - 05:33 AM

Wow. Thank you so much for the help! It's working now!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1