I create the Fraction Input and Output it was fine, and then I try to put the array of the Fraction. Everything work fine until I Compile and execute the Program.

This Error appear: Nullreferenceexception object reference not set to an instance of an object.

Help me this error! Thanks so much!!!

Array.cs

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

namespace Array
{
    using Fraction;

    public class CArray
    {
        private int number;
        private CFraction[]ps;
        
        public CArray()
        {
            this.ps = null;
            this.number = 0;
        }
        public CArray(CArray m)
        {
            this.number = m.number;
            CFraction[]ps = new CFraction[number];
            for (int i = 0; i < number; i++)
            {
                this.ps[i] = m.ps[i];
            }
        }
        
        public void Input()
        {
            Console.Write("Please Input the number: ");
            number = Convert.ToInt32(Console.ReadLine());
            ps = new CFraction[number];
     
            for (int i = 0; i < number; i++)
            {
                Console.Write("ps[{0}]: ", i+1);
                ps[i].Input(); 
            }
        }
        public void Output()
        {
            Console.Write("Result: ");
            for (int i = 0; i < ps.Length; i++)
            {
                ps[i].Output();
            }
        }
    }
}

Fraction.cs

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

namespace Fraction
{
    public class CFraction
    {
        private int numerator;
        private int  denominator;

        public CFraction()
        {
            this.numerator = 0;
            this.denominator = 1;
        }
        public CPhanSo(int numerator, int denominator)
        {
            this.numerator = numerator;
            this.denominator = denominator;
        }
        public override bool Equals(object obj)
        {
            if (obj == null || GetType() != obj.GetType()) return false;
            CPhanSo ps = (CPhanSo)obj;
            return numerator.Equals(ps.numerator) && denominator.Equals(ps.denominator);
        }
        public override int GetHashCode()
        {
            return numerator.GetHashCode()^denominator.GetHashCode();
        }
        public void Input()
        {
            Console.Write("Input numerator: ");
            this.numerator = Convert.ToInt32(Console.ReadLine());
            Console.Write("Input denominator: ");
            this.denominator = Convert.ToInt32(Console.ReadLine());
        }
        public void Output()
        {
            Console.WriteLine("Fraction : {0}/{1}", this.numerator, this.denominator);
        }

    }
}

Program.cs

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

namespace ProGram
{
    using Fraction;
    using Array;

    class Program
    {
        static void Main(string[] args)
        {
           CArray m = new CArray();
            m.Nhap();
            m.Xuat();
        }
        
    }
}

Instead of making me try to figure out all your code, why not tell me which line generates the error?

You are instantiating a CArray class.
Then you call a non existent method of that class, or is this just a translation problem (English <-> Vietnamese)?

Make sure that "number" is actually a number greater than 0. Your number may actually be 0. In which case you would have a zero length array, or null.

The method CPhanSo on line 18 of your CFraction does not return a value (not even void) so it could be a constructor? Could it mean CFraction in Vietnamese?
Copied your code, but it will not compile!
Do not mix too much native language in your code, it wil confuse you.
I once even had a version of Excel in my native language(Dutch), it was pure horror I can tell you!

Yeah I know your point. When I input the "number" It's definitely greater than 0.

The method CPhanSo on line 18 of your CFraction does not return a value (not even void) so it could be a constructor? Could it mean CFraction in Vietnamese?
Copied your code, but it will not compile!
Do not mix too much native language in your code, it wil confuse you.
I once even had a version of Excel in my native language(Dutch), it was pure horror I can tell you!

Sorry It's my fault. The method CPhanSo = CFraction on line 18. It's the constructor.

The null reference exception may be inside a method other than where Visual Studio is saying it is. Check that your Input method is working correctly and you're getting a number from your Convert methods.

EDIT: Curiously I decided to examine your code more closely. Here are some of the more glaring errors. CFraction[]ps = new CFraction[number]; In your CArray constructor. This shouldn't even be allowed, it's classed as re-definition. Console.Write("ps[{0}]: ", i+1); What is the point of this in your Input method. Surely if you want to know which "ps" you're accessing you should just leave it be? ps[i].Output(); In your Output method will fail if ps is set to null, as you do so in your constructor.

Other than that I can't really see much wrong with it without compiling it myself


EDIT: Guh, I'm blind. You're creating the array, but you're not assigning any objects to the array. So you make an array that can contain "number" objects, but you still need to assign each one of them to an instance of that object. It is not auto-magically done for you.

Could you tell me what Nhap and Xuat mean?
Line 16 and 17 of your Main program. I infer input and output but I'm not sure.

It's where he hasn't translated Input and Output into English.

The error is as I specified. He needs to assign an instance of the CFraction class to each of the array members. He hasn't done so.

I resolved this problem by using the new object in the CArray method Input in the loop for. Replace the for of the Input at CArray.cs by this

for (int i = 0; i < number; i++)
{
Console.Write("ps[{0}]: ", i+1);
ps[i] = new CFraction();
ps[i].Input();
}

So you consider this as solved?
giải quyết?

So sorry about my translation. The problem was solved. Thanks for ur helping !!!!

If the problem is solved, please mark the thread as solved ^^

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.