user3328039
user3328039

Reputation: 79

"Object reference not set to an instance of an object" error during the compilation

I get the "Object reference not set to an instance of an object" error when i compile the code at string[] values = lineuser.Split(' '); . Any idea?

namespace function
{
    public partial class Form1 : Form
    {

            float userscore,itemscore,result;
            string lineitem, lineuser;
            //float[][] a = new float[89395][100];
            //float[][] b = new float[1143600][100];
            float[][] a = Enumerable.Range(0, 89395).Select(i => new float[100]).ToArray();
            float[][] b = Enumerable.Range(0, 1143600).Select(j => new float[100]).ToArray();
            //float[,] c = new float[89395, 100];
            StreamReader fileitem = new StreamReader("c:\\ITEM_SVD_FULL.txt");
            StreamReader fileuser = new StreamReader("c:\\USER_SVD_FULL.txt");
        public Form1()
        {
            InitializeComponent();

             for (int x = 0; x <= 8939500; x++)
            {
                lineuser = fileuser.ReadLine();
                string[] values = lineuser.Split(' '); //<------the line's error
                int userid, factoriduser;
                foreach (string value in values)
                {
                    userid = Convert.ToInt32(values[0]);
                    factoriduser = Convert.ToInt32(values[1]);
                    userscore = Convert.ToSingle(values[2]);
                    a[userid][factoriduser] = userscore;
                }
            }

            for (int y = 0; y <= 114360000; y++)
            {
                lineitem = fileitem.ReadLine();
                string[] valuesi = lineitem.Split(' ');
                int itemid, factoriditem;
                foreach (string value in valuesi)
                {
                    itemid = Convert.ToInt32(valuesi[0]);
                    factoriditem = Convert.ToInt32(valuesi[1]);
                    itemscore = Convert.ToSingle(valuesi[2]);
                    b[itemid][factoriditem] = itemscore;
                }

            }

        }
        public float dotproduct(int userid,int itemid)
        {

            //get the score of 100 from user and item to dotproduct
            float[] u_f = a[userid];
            float[] i_f = b[itemid];

            for (int i = 0; i <u_f.GetLength(1); i++)
            {
                result += u_f[userid] * i_f[itemid];
            }
            return result;

        }

        private void btn_recomm_Click(object sender, EventArgs e)
        {
            if(txtbx_id.Text==null)
            {
                MessageBox.Show("please insert user id");
            }
         if (txtbx_id.Text != null && txtbx_itemid==null)
          {
           int sc = Convert.ToInt32(txtbx_id.Text);
           if (sc>=0 &&sc<=89395)
            {
              for (int z=0;z<=1143600;z++)
                {
                  dotproduct(sc,z);
                }
               //Hashtable hashtable = new Hashtable();
               //put the result in hashtable
               //foreach (DictionaryEntry entry in hashtable)
                 //{
                   //Console.WriteLine("{0}, {1}", entry.Key, entry.Value);
                // }
            }
         }
            if (txtbx_id!=null &&txtbx_itemid!=null)
            {
                int uid = Convert.ToInt32(txtbx_id.Text);
                int iid = Convert.ToInt32(txtbx_itemid.Text);
                {
                    if (uid>=0 && uid<=89395 && iid>=0 && iid<=1143600)
                    {
                        dotproduct(uid,iid);
                        MessageBox.Show("The Score of user id "+uid+" is "+result);
                    }
                }

Upvotes: 0

Views: 925

Answers (3)

Ragesh Puthiyedath Raju
Ragesh Puthiyedath Raju

Reputation: 3939

Please check you lineuser variable is null

    public Form1()
    {
        InitializeComponent();

         for (int x = 0; x <= 8939500; x++)
         {
            if(!string.IsNullorEmpty(lineuser) //<--- check the string is empty
            {   
                string[] values = lineuser.Split(' '); //<------the line's error
                int userid, factoriduser;
                foreach (string value in values)
                {
                  userid = Convert.ToInt32(values[0]);
                  factoriduser = Convert.ToInt32(values[1]);
                  userscore = Convert.ToSingle(values[2]);
                  a[userid][factoriduser] = userscore;
                }
            }
         }

         for (int y = 0; y <= 114360000; y++)
         {

            lineitem = fileitem.ReadLine();

            if(!string.IsNullorEmpty(lineitem) //<--- check the string is empty
            {

               string[] valuesi = lineitem.Split(' ');
               int itemid, factoriditem;
               foreach (string value in valuesi)
               {
                 itemid = Convert.ToInt32(valuesi[0]);
                 factoriditem = Convert.ToInt32(valuesi[1]);
                 itemscore = Convert.ToSingle(valuesi[2]);
                 b[itemid][factoriditem] = itemscore;
               }
            }
        }

    }

Now you can avoid the error.

Upvotes: 1

Steve
Steve

Reputation: 216243

It is really a bad practice to open the file while you define the global variable in your class.
But as part from this, when you call ReadLine the result could be a null because there are no more lines to read and you don't check for this case.

Looking at the code shown above, there is no need to have a global variable for the filestream, so, the usual pattern when handling resources (OPEN/USE/CLOSE) should be followed for both files

    public Form1()
    {
        InitializeComponent();

        string lineuser;

        // OPEN
        using(StreamReader fileuser = new StreamReader("c:\\USER_SVD_FULL.txt"))
        {
            // USE
            while((lineuser = fileuser.ReadLine()) != null)
            {
                 string[] values = lineuser.Split(' ');
                 ....
            }
        } //CLOSE & DISPOSE
        ....

        string lineitem;
        using(StreamReader fileitem = new StreamReader("c:\\ITEM_SVD_FULL.txt"))
        {
            while((lineitem = fileitem.ReadLine()) != null)
            {
                string[] valuesi = lineitem.Split(' ');
                ....
            }
        }

Upvotes: 0

shree.pat18
shree.pat18

Reputation: 21757

You haven't opened the Stream fileuser so it is still null.

Upvotes: 1

Related Questions