“编译期间未将对象引用设置为对象的实例”错误

时间:2014-02-21 10:54:41

标签: c#

当我在string[] values = lineuser.Split(' ');编译代码时,我得到“对象引用没有设置为对象的实例”错误。有什么想法吗?

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);
                    }
                }

3 个答案:

答案 0 :(得分:1)

请检查lineuser变量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;
               }
            }
        }

    }

现在您可以避免错误。

答案 1 :(得分:1)

您尚未打开Stream fileuser,因此它仍然为空。

答案 2 :(得分:0)

在您的班级中定义全局变量时打开文件确实是一种不好的做法 但是作为其中的一部分,当你调用ReadLine时,结果可能是null,因为没有更多的行可以读取,你也不会检查这种情况。

查看上面显示的代码,不需要为文件流提供全局变量,因此,两个文件都应遵循处理资源时常用的模式(OPEN / USE / CLOSE)

    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(' ');
                ....
            }
        }