String s1="";
s1+="abc";  //这里报错
bool b1=false;
FileStream a1;
while(true)
{
if (b1)
{
    a1.close();   //这一句怎么保证不会出错 我想加个if,但if(a1==null)会出错
    a1=new FileStream("c:\\1.txt",FileMod.Creat,FileAccess.Write);
}
}

解决方案 »

  1.   

    s1+="abc"; 没错的s1='abc'
    a1没有实例化,a1.close();  C要大写,C#很认大小写的,和VB不一样!
      

  2.   

    String s1=""; 
    s1+="abc"; 这样写是没有问题的。if (a1==null) a1.close();   
      

  3.   

    String s1="";  已经实例化了。        public string[] strPath()
            {
                StreamReader sr;
                try
                {
                    path = new string[5];
                    int i = 0;
                    string sLine = "";
                    sr = File.OpenText(Path.GetFileName("Path.ini"));
                    while (sLine != null)
                    {
                        sLine = sr.ReadLine();
                        if (sLine == null)
                            break;
                        path[i] = sLine;
                        i++;
                    }
                }
                catch (Exception e)
                {
                    MessageBox.Show(e.Message, "提示", MessageBoxButtons.OK,
                        MessageBoxIcon.Error);
                    return path;
                }
                sr.Close();
                return path;
            }
      

  4.   

    单纯看 String 那两句是没错,不知是不是被之前的错误影响到的。
    在 C 系列语言中,定义时就初始化是个好习惯。比如说:
    FileStream fs = null;
    ...
    if (fs == null)
    {
        fs = new FileStream(...);
    }
    else
    {
        fs.Close();
    }
      

  5.   

    String s1=""; 
    s1+="abc";  //这里报错 没错啊?怎么会错呢?bool b1=false; 
    FileStream a1; 
    while(true) 

    if (b1) 

        a1.close();   //这一句怎么保证不会出错 我想加个if,但if(a1==null)会出错 
        a1=new FileStream("c:\\1.txt",FileMod.Creat,FileAccess.Write); 

    }
    a1没有实例化,当然出错了
      

  6.   

    FileStream a1 =null;
    if(a1 == null) 就不会报错了
    对象没有实例化,这样指针没有指向具体的位置,当然不能判断!