asp.net 运行出现System.NullReferenceException: 未将对象引用设置到对象的实例。但我看到我以下的代码没看到有引用或对象实例没对照啊 帮忙看下using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class repaire_insert : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {    }
    protected void Button1_Click(object sender, EventArgs e)
    {
              float f=0.0F;
        float f1=0.0F;
        float f2=0.0F;
        float f3=0.0F;
        SqlConnection myConnection=new SqlConnection (System.Configuration .ConfigurationSettings .AppSettings ["connString"]);
       
            
            myConnection .Open ();           
       string[] str1 = TextBox3.Text.Split(new char[] { ',' });//取出所用到的材料名称以,区分开来
      string[] str2= TextBox4.Text.Split(new char[] { ',' });//取出对应的用材料的数量
 //材料费
        for(int i=0;i<str1 .Length ;i++)
        {
            
            string queryStr="select 单位价格 from 材料表 where 材料名称='str1[i]'";
            SqlCommand myCommand1=new SqlCommand (queryStr ,myConnection );
            f=float .Parse (myCommand1 .ExecuteScalar().ToString ());
             f1=f*float .Parse (str2[i])+f1;
        }       
              
   
        //劳务费
        string queryStr1="select 单位时间费用 from 劳务表 where 项目名称='"+TextBox6 .Text +"'";
        SqlCommand myCommand2=new SqlCommand (queryStr1 ,myConnection );
        f2 =float.Parse (myCommand2 .ExecuteScalar ().ToString ());
      
        f3 =f2 * float.Parse (TextBox5 .Text );
         string insertStr="insert into 房屋修缮表 (业主房号,签收人姓名,用料数量,用料名称,花费时间,项目名称,材料费,劳务费,总计费用,日期) values ('"+TextBox1 .Text +"','"+TextBox2 .Text +"','"+TextBox3 .Text +"','"+TextBox4 .Text +"',"+TextBox5 .Text +",'"+TextBox6 .Text +"',f1,f3,f1+f3,getdate())";
   
        SqlCommand myCommand=new SqlCommand (insertStr ,myConnection );        try 
        {
            myCommand .ExecuteNonQuery ();
        }
         catch 
        {
            Label1 .Text ="fail";
        }
        
        finally 
        {
            myConnection .Close ();
        }
               
    }
}

解决方案 »

  1.   

    调试看看float .Parse (myCommand1 .ExecuteScalar()==null?"0":myCommand1 .ExecuteScalar().ToString ()); 
    里是否有数据。
      

  2.   

    又出现这个错误了System.FormatException: 输入字符串的格式不正确。 
      

  3.   

    若按照3楼的建议做我提到的那个错误没了,但又出现了System.FormatException: 输入字符串的格式不正确。的错误
      

  4.   

    确定一下,TextBox中输入的字符,是否可以进行float.parse();
      

  5.   

    对空对象进行了操作
    比如:Arraylist a=null;
        a.Add();
    此时便会报错,未将对象引用到对象的实例,因为 a为空啊
      

  6.   

    若把我上边的代码这样修改下 f= float.Parse(myCommand1.ExecuteScalar() == null ? "0" : myCommand1.ExecuteScalar().ToString()); 就没有未将对象引用到对象的实例的错误了。这是否说明了我从数据库中取到的是空啊。还有这样改后出现了System.FormatException: 输入字符串的格式不正确的错误
     string[] str1 = TextBox3.Text.Split(new char[] { ',' });//取出所用到的材料名称以,区分开来  string[] str2= TextBox4.Text.Split(new char[] { ',' });//取出对应的用材料的数量 //材料费         for(int i=0;i <str1 .Length ;i++) 
            { 
                
                string queryStr="select 单位价格 from 材料表 where 材料名称='str1[i]'"; 
                SqlCommand myCommand1=new SqlCommand (queryStr ,myConnection ); 
                f=float .Parse (myCommand1.ExecuteScalar() == null ? "0" :myCommand1 .ExecuteScalar().ToString ()); 
                f1=f*float .Parse (str2[i])+f1; 
            } 
      

  7.   

    检查float.Parse()中字符串吧,里面的字符串有问题呀
      

  8.   

    我知道哪里有问题了 是string[] str1 = TextBox3.Text.Split(new char[] { ',' });没起作用因为若TextBox3.Text="1,2,3"则我response.write(str1[0])得到的是1,2,3