本文介绍了我编写了以下代码来存储SQL Server 2008 db中的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我编写了以下代码来存储 sql server 2008 db,但此代码 throw 的例外情况 对象引用未设置为对象的实例 我尝试过: 创建表格DemoTable ( id int主键标识(1,1), que image, opt1 image, opt2 image, opt3图片, opt4图片, ans varchar(100), sub varchar(100) ) =========================== <%@ Page Title =Language =C#MasterPageFile =〜/ Admin / AdminMaster.masterAutoEventWireup =trueCodeFile =images.aspx.csInherits =Admin_images%> < asp:Content ID =Content1Co ntentPlaceHolderID =ContentPlaceHolder1Runat =Server> < script language =javascript> 函数ShowpImagePreview1(输入){ if(input.files&& input.files [0]){ var reader = new FileReader(); reader.onload = function(e){ $ ('#imgque')。attr('ImageUrl',e.target.result); } reader.readAsDataURL(input.files [0]); b $ b } } < / script> < asp:ScriptManager ID =ScriptManager1runat =server> < asp:UpdatePanel ID =UpdatePanel1runat =server> < contenttemplate> 主题 < asp:DropDownList ID =ddlsubrunat =serverCssClass =form-control> Q uestion < asp:FileUpload ID =fuquerunat =serverCssClass =form-control/> 选项1 < asp:FileUpload ID =fuopt1runat =serverCssClass =form-control /> 选项2 < asp:FileUpload ID =fuopt2 runat =serverCssClass =form-control/> 选项3 < asp:FileUpload ID =fuopt3runat =serverC ssClass =form-control/> 选项4 < asp:FileUpload ID =fuopt4runat =serverCssClass =form-control/> 答案 < asp:DropDownList ID = ddloptrunat =serverCssClass =form-control> < asp:ListItem> opt1 < asp:ListItem> opt2 < asp:ListItem> opt3 < asp:ListItem> opt4 < asp:按钮ID =btnsubmitrunat =serverText =提交 onclick =btnsubmit_Click/> < asp:标签ID =lblmsgrunat =server> ======= ===== 使用System; 使用System.Collections.Generic; 使用System。 Linq; 使用System.Web; 使用System.Web.UI; 使用System.Web.UI.WebControls; 使用System.Configuration; 使用System.Data; 使用System.Data.SqlClient; 使用System.IO ; 公共部分类Admin_images:System.Web.UI.Page { SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings [Con1]。ToString()); SqlCommand com; DataSe t ds = new DataSet(); SqlDataAdapter Adapter; SqlDataReader reader; public void Connect( ) { con.Close(); if(con.State == ConnectionState.Closed) { con.Open(); } } public void关闭() { if(con.State == ConnectionState.Open) { con.Close(); } } protected void Page_Load(object sender,EventArgs e) { try { LoadSubject(); } catch(例外) { Response.Redirect(er.Message); } } public void LoadSubject() { 尝试 { 选择sel = new选择(); DataSet subds = sel.SubjectDistinct( ); if(subds.Tables [0] .Rows.Count> 0) { ddlsub.DataSource = subds.Tables [0]; ddlsub.DataTextField =subject; ddlsub.DataValueField =subject; ddlsub.DataBind(); } } catch(例外) { Response.Write(er.Message); } } public int addimg(byte [] que,byte [] opt1,byte [] opt2,byte [] opt3,byte [] opt4,string ans,string sub) { int a; Connect(); com = new SqlCommand(插入DemoTable([que],[opt1],[opt2],[opt3],[opt4],[ans],[sub])值(@ que,@ opt1,@ opt2,@ opt3,@ opt4,@ ans,@ sub),con); com.Parameters.AddWithValue(@ que,que); com.Parameters。 AddWithValue(@ opt1,opt1); com.Parameters.AddWith值(@ opt2,opt2); com.Parameters.AddWithValue(@ opt3,opt3); com.Parameters.AddWithValue(@ opt4 ,opt4); com.Parameters.AddWithValue(@ ans,ans); com.Parameters.AddWithValue(@ sub,sub); a = com.ExecuteNonQuery(); 关闭(); 返回a; 抛出新的NotImplementedException(); } protected void btnupload_Click(object sender,EventArgs e ) { 试试 { } catch(例外) { Response.Write(er.Message); } } protected void btnsubmit_Click(object sender,EventArgs e) { 试试 { //流fq = fuque.PostedFile.InputStream; // BinaryReader br = new BinaryReader(fq); // Byte [] rawq = br.ReadBytes((Int32)fq.Length); //流f1 = fuque.PostedFile.InputStream; // BinaryReader br1 = new BinaryReader(f1); // Byte [] raw1 = br1.ReadBytes((Int32)f1.Length); //流f2 = fuque.PostedFile.InputStream; // BinaryReader br2 =新的BinaryReader(f2); //字节[] raw2 = br2.ReadBytes((Int32)f2.Length); //流f3 = fuque.PostedFile.InputStream; // BinaryReader br3 = new BinaryReader(f3); // Byte [] raw3 = br3.ReadBytes((Int32)f3.Length); //流f4 = fuque.PostedFile.InputStream; // BinaryReader br4 =新的BinaryReade r(f4); // Byte [] raw4 = br4.ReadBytes((Int32)f4.Length); FileStream fq = new FileStream(fuque.PostedFile.FileName,FileMode.OpenOrCreate,FileAccess.ReadWrite); byte [] rawq = new byte [fq.Length]; fq.Read(rawq,0,Convert.ToInt32(fq.Length)); FileStream f1 = new FileStream(fuopt1.PostedFile。 FileName,FileMode.OpenOrCreate,FileAccess.ReadWrite); byte [] raw1 = new byte [f1.Length]; f1.Read(raw1,0,Convert。 ToInt32(f1.Length)); FileStream f2 =新FileStream(fuopt2.PostedFile.FileName,FileMode.OpenOrCreate,FileAccess.ReadWrite); byte [] raw2 =新字节[f2.Length]; f2.Read(raw2,0,Convert.ToInt32(f2.Length)); FileStream f3 =新FileStream(fuopt3.PostedFile.FileName,FileMode.OpenOr Create,FileAccess.ReadWrite); byte [] raw3 = new byte [f3.Length]; f3.Read(raw3,0,Convert.ToInt32(f3。长度)); FileStream f4 =新FileStream(fuopt4.PostedFile.FileName,FileMode.OpenOrCreate,FileAccess.ReadWrite); byte [] raw4 =新字节[f4.Length]; f4.Read(raw4,0,Convert.ToInt32(f4.Length)); int n = addimg(rawq,raw1,raw2,raw3,raw4,ddlopt.SelectedValue,ddlsub.SelectedItem.Value); if(n == 1) { lblmsg.Text =Q up; } } catch(NullReferenceException ex) { lblmsg.Text =处理器使用率+ ex.Message; } catch (例外情况) { lblmsg.Text =处理器使用情况+ ex.Message; } } } 解决方案 ('#imgque')。attr('ImageUrl',e.target.result); } reader.readAsDataURL(input.files [0]) ; } } < / script> < asp:ScriptManager ID =ScriptManager1runat =server> < asp:UpdatePanel ID =UpdatePanel1runat =server> < contenttemplate> 主题 < asp:DropDownList ID =ddlsubrunat =serverCssClass =form-control> 问题 < asp:FileUpload ID =fuquerunat =serverCssClass =form-control/> 选项1 < asp:FileUpload ID =fuopt1runat =serverCssClass =form-control/> 选项2 < asp:FileUpload ID =fuopt2runat =serverCssClass =form-control/> 选项3 < asp:FileUpload ID = fuopt3runat =serverCssClass =form-control/> 选项4 < asp:FileUpload ID =fuopt4runat =serverCssClass =form-control/> 答案 < asp:DropDownList ID =ddloptrunat =serverCssClass =form-control> < asp:ListItem> opt1 < asp:ListItem> opt2 < asp:ListItem> opt3 < asp:ListItem> opt4 < asp:按钮ID =btnsubmitrunat =serverText =Submit onclick =btnsubmit_Click/> < asp:Label ID =lblmsg runat =server> ============ 使用System; 使用System.Collections.Generic; 使用System.Linq; 使用System.Web; 使用System.Web.UI; 使用System.Web.UI.WebControls; 使用System.Configuration; 使用System.Data; 使用System.Data.SqlClient; 使用System.IO; 公共部分class Admin_images:System.Web.UI.Page { SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings [Con1]。ToString()); SqlCommand com; DataSet ds = new DataSet(); Sq lDataAdapter适配器; SqlDataReader阅读器; public void Connect() { con.Close(); if(con.State == ConnectionState.Closed) { con.Open(); } } public void关闭() { if(con.State == ConnectionState.Open) { con.Close(); } } protected void Page_Load(object sender,EventArgs e) { 试试 { LoadSubject(); } catch(例外情况) { Response.Redirect(er.Message); } } public void LoadSubject() { try { 选择sel = new Select(); DataSet subds = sel.SubjectDistinct(); if(subds.Tables [0] .Rows.Count> 0) { ddlsub.DataSource = subds.Tables [0]; ddlsub.DataTextField =subject; ddlsub.DataValueField =subject; ddlsub.DataBind(); } } catch(例外) { Response.Write(er.Message); } } public int addimg(byte [] que,byte [] opt1,byte [] opt2,byte [] opt3,byte [] opt4,string ans,string sub) { int a; Connect(); com = new SqlCommand(插入DemoTable([que],[opt1],[opt2],[opt3],[opt4],[ans],[sub])值(@ que,@ opt1,@ opt2,@ opt3,@ opt4,@ ans,@ sub),con); com.Parameters.AddWithValue(@ que,que); com.Parameters。 AddWithValue(@ opt1,opt1); com.Parameters.AddWith值(@ opt2,opt2); com.Parameters.AddWithValue(@ opt3,opt3); com.Parameters.AddWithValue(@ opt4 ,opt4); com.Parameters.AddWithValue(@ ans,ans); com.Parameters.AddWithValue(@ sub,sub); a = com.ExecuteNonQuery(); 关闭(); 返回a; 抛出新的NotImplementedException(); } protected void btnupload_Click(object sender,EventArgs e ) { 试试 { } catch(例外) { Response.Write(er.Message); } } protected void btnsubmit_Click(object sender,EventArgs e) { 试试 { //流fq = fuque.PostedFile.InputStream; // BinaryReader br = new BinaryReader(fq); // Byte [] rawq = br.ReadBytes((Int32)fq.Length); //流f1 = fuque.PostedFile.InputStream; // BinaryReader br1 = new BinaryReader(f1); // Byte [] raw1 = br1.ReadBytes((Int32)f1.Length); //流f2 = fuque.PostedFile.InputStream; // BinaryReader br2 =新的BinaryReader(f2); //字节[] raw2 = br2.ReadBytes((Int32)f2.Length); //流f3 = fuque.PostedFile.InputStream; // BinaryReader br3 = new BinaryReader(f3); // Byte [] raw3 = br3.ReadBytes((Int32)f3.Length); //流f4 = fuque.PostedFile.InputStream; // BinaryReader br4 =新的BinaryReade r(f4); // Byte [] raw4 = br4.ReadBytes((Int32)f4.Length); FileStream fq = new FileStream(fuque.PostedFile.FileName,FileMode.OpenOrCreate,FileAccess.ReadWrite); byte [] rawq = new byte [fq.Length]; fq.Read(rawq,0,Convert.ToInt32(fq.Length)); FileStream f1 = new FileStream(fuopt1.PostedFile。 FileName,FileMode.OpenOrCreate,FileAccess.ReadWrite); byte [] raw1 = new byte [f1.Length]; f1.Read(raw1,0,Convert。 ToInt32(f1.Length)); FileStream f2 =新FileStream(fuopt2.PostedFile.FileName,FileMode.OpenOrCreate,FileAccess.ReadWrite); byte [] raw2 =新字节[f2.Length]; f2.Read(raw2,0,Convert.ToInt32(f2.Length)); FileStream f3 =新FileStream(fuopt3.PostedFile.FileName,FileMode.OpenOr Create,FileAccess.ReadWrite); byte [] raw3 = new byte [f3.Length]; f3.Read(raw3,0,Convert.ToInt32(f3。长度)); FileStream f4 =新FileStream(fuopt4.PostedFile.FileName,FileMode.OpenOrCreate,FileAccess.ReadWrite); byte [] raw4 =新字节[f4.Length]; f4.Read(raw4,0,Convert.ToInt32(f4.Length)); int n = addimg(rawq,raw1,raw2,raw3,raw4,ddlopt.SelectedValue,ddlsub.SelectedItem.Value); if(n == 1) { lblmsg.Text =Q up; } } catch(NullReferenceException ex) { lblmsg.Text =处理器使用率+ ex.Message; } catch (例外情况) { lblmsg.Text =处理器使用情况+ ex.Message; } } } 这是其中之一我们得到的最常见的问题,也是我们最不能回答的问题,但你最有能力回答自己。 我来解释一下错误意味着:您已尝试使用变量,属性或方法返回值但它包含null - 这意味着变量中没有类的实例。 它有点像口袋:你的衬衫里有一个口袋,用来握笔。如果你进入口袋并发现那里没有笔,你就不能在一张纸上签名 - 如果你尝试的话,你会得到非常有趣的外观!空口袋给你一个空值(这里没有笔!)所以你不能做任何你检索笔通常做的事情。它为什么空?这就是问题 - 可能是你今天早上离开家时忘了拿起你的笔,或者你昨晚把它拿到昨天的衬衫口袋里时可能会把笔留下来。 我们无法分辨,因为我们不在那里,更重要的是,我们甚至看不到你的衬衫,更不用说口袋里的东西了! 回到计算机,你做了同样的事情,不知何故 - 我们看不到你的代码,更不用说运行它了,找不到包含null的东西。 但是你可以 - 而Visual Studio将在这里帮助你。在调试器中运行您的程序,当它失败时,VS将向您显示它发现问题的行。然后,您可以开始查看它的各个部分,以查看哪个值为null,并开始回顾代码以找出原因。因此,在包含错误行的方法的开头放置一个断点,然后从头再次运行程序。这一次,VS会在错误发生前停止,让你通过查看代码来查看你的价值来检查发生了什么。 但我们做不到那 - 我们没有你的完整代码,如果我们拥有它,我们不知道如何使用它,我们没有你的数据。所以试试吧 - 看看你能找到多少信息! I have written a following code to store images in sql server 2008 db, but this code throw an exception of "object reference not set to an instance of an object"What I have tried:create table DemoTable(id int primary key identity(1,1),que image,opt1 image,opt2 image,opt3 image,opt4 image,ans varchar(100),sub varchar(100))===========================<%@ Page Title="" Language="C#" MasterPageFile="~/Admin/AdminMaster.master" AutoEventWireup="true" CodeFile="images.aspx.cs" Inherits="Admin_images" %><asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server"><script language="javascript"> function ShowpImagePreview1(input) { if (input.files && input.files[0]) { var reader = new FileReader(); reader.onload = function (e) { $('#imgque').attr('ImageUrl', e.target.result); } reader.readAsDataURL(input.files[0]); } }</script> <asp:ScriptManager ID="ScriptManager1" runat="server"> <asp:UpdatePanel ID="UpdatePanel1" runat="server"> <contenttemplate>Subject <asp:DropDownList ID="ddlsub" runat="server" CssClass="form-control"> Question<asp:FileUpload ID="fuque" runat="server" CssClass="form-control" />Option 1<asp:FileUpload ID="fuopt1" runat="server" CssClass="form-control" />Option 2<asp:FileUpload ID="fuopt2" runat="server" CssClass="form-control" />Option 3<asp:FileUpload ID="fuopt3" runat="server" CssClass="form-control" />Option 4<asp:FileUpload ID="fuopt4" runat="server" CssClass="form-control" />Answer <asp:DropDownList ID="ddlopt" runat="server" CssClass="form-control"> <asp:ListItem>opt1 <asp:ListItem>opt2 <asp:ListItem>opt3 <asp:ListItem>opt4 <asp:Button ID="btnsubmit" runat="server" Text="Submit" onclick="btnsubmit_Click" /> <asp:Label ID="lblmsg" runat="server"> ============using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;using System.Configuration;using System.Data;using System.Data.SqlClient;using System.IO;public partial class Admin_images : System.Web.UI.Page{ SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["Con1"].ToString()); SqlCommand com; DataSet ds = new DataSet(); SqlDataAdapter Adapter; SqlDataReader reader; public void Connect() { con.Close(); if (con.State == ConnectionState.Closed) { con.Open(); } } public void Closed() { if (con.State == ConnectionState.Open) { con.Close(); } } protected void Page_Load(object sender, EventArgs e) { try { LoadSubject(); } catch (Exception er) { Response.Redirect(er.Message); } } public void LoadSubject() { try { Select sel = new Select(); DataSet subds = sel.SubjectDistinct(); if (subds.Tables[0].Rows.Count > 0) { ddlsub.DataSource = subds.Tables[0]; ddlsub.DataTextField = "subject"; ddlsub.DataValueField = "subject"; ddlsub.DataBind(); } } catch (Exception er) { Response.Write(er.Message); } } public int addimg(byte[] que,byte[] opt1,byte[] opt2,byte[] opt3,byte[] opt4,string ans,string sub) { int a; Connect(); com = new SqlCommand("insert into DemoTable ([que],[opt1],[opt2],[opt3],[opt4],[ans],[sub]) values(@que,@opt1,@opt2,@opt3,@opt4,@ans,@sub)", con); com.Parameters.AddWithValue("@que", que); com.Parameters.AddWithValue("@opt1", opt1); com.Parameters.AddWithValue("@opt2", opt2); com.Parameters.AddWithValue("@opt3", opt3); com.Parameters.AddWithValue("@opt4", opt4); com.Parameters.AddWithValue("@ans", ans); com.Parameters.AddWithValue("@sub", sub); a = com.ExecuteNonQuery(); Closed(); return a; throw new NotImplementedException(); } protected void btnupload_Click(object sender, EventArgs e) { try { } catch (Exception er) { Response.Write(er.Message); } } protected void btnsubmit_Click(object sender, EventArgs e) { try { //Stream fq = fuque.PostedFile.InputStream; //BinaryReader br = new BinaryReader(fq); //Byte[] rawq = br.ReadBytes((Int32)fq.Length); //Stream f1 = fuque.PostedFile.InputStream; //BinaryReader br1 = new BinaryReader(f1); //Byte[] raw1 = br1.ReadBytes((Int32)f1.Length); //Stream f2 = fuque.PostedFile.InputStream; //BinaryReader br2 = new BinaryReader(f2); //Byte[] raw2 = br2.ReadBytes((Int32)f2.Length); //Stream f3 = fuque.PostedFile.InputStream; //BinaryReader br3 = new BinaryReader(f3); //Byte[] raw3 = br3.ReadBytes((Int32)f3.Length); //Stream f4 = fuque.PostedFile.InputStream; //BinaryReader br4 = new BinaryReader(f4); //Byte[] raw4 = br4.ReadBytes((Int32)f4.Length); FileStream fq = new FileStream(fuque.PostedFile.FileName, FileMode.OpenOrCreate, FileAccess.ReadWrite); byte[] rawq = new byte[fq.Length]; fq.Read(rawq, 0, Convert.ToInt32(fq.Length)); FileStream f1 = new FileStream(fuopt1.PostedFile.FileName, FileMode.OpenOrCreate, FileAccess.ReadWrite); byte[] raw1 = new byte[f1.Length]; f1.Read(raw1, 0, Convert.ToInt32(f1.Length)); FileStream f2 = new FileStream(fuopt2.PostedFile.FileName, FileMode.OpenOrCreate, FileAccess.ReadWrite); byte[] raw2 = new byte[f2.Length]; f2.Read(raw2, 0, Convert.ToInt32(f2.Length)); FileStream f3 = new FileStream(fuopt3.PostedFile.FileName, FileMode.OpenOrCreate, FileAccess.ReadWrite); byte[] raw3 = new byte[f3.Length]; f3.Read(raw3, 0, Convert.ToInt32(f3.Length)); FileStream f4 = new FileStream(fuopt4.PostedFile.FileName, FileMode.OpenOrCreate, FileAccess.ReadWrite); byte[] raw4 = new byte[f4.Length]; f4.Read(raw4, 0, Convert.ToInt32(f4.Length)); int n = addimg(rawq, raw1, raw2, raw3, raw4, ddlopt.SelectedValue, ddlsub.SelectedItem.Value); if (n == 1) { lblmsg.Text = "Q up"; } } catch (NullReferenceException ex) { lblmsg.Text="Processor Usage" + ex.Message; } catch (Exception ex) { lblmsg.Text = "Processor Usage" + ex.Message; } }} 解决方案 ('#imgque').attr('ImageUrl', e.target.result); } reader.readAsDataURL(input.files[0]); } }</script> <asp:ScriptManager ID="ScriptManager1" runat="server"> <asp:UpdatePanel ID="UpdatePanel1" runat="server"> <contenttemplate>Subject <asp:DropDownList ID="ddlsub" runat="server" CssClass="form-control"> Question<asp:FileUpload ID="fuque" runat="server" CssClass="form-control" />Option 1<asp:FileUpload ID="fuopt1" runat="server" CssClass="form-control" />Option 2<asp:FileUpload ID="fuopt2" runat="server" CssClass="form-control" />Option 3<asp:FileUpload ID="fuopt3" runat="server" CssClass="form-control" />Option 4<asp:FileUpload ID="fuopt4" runat="server" CssClass="form-control" />Answer <asp:DropDownList ID="ddlopt" runat="server" CssClass="form-control"> <asp:ListItem>opt1 <asp:ListItem>opt2 <asp:ListItem>opt3 <asp:ListItem>opt4 <asp:Button ID="btnsubmit" runat="server" Text="Submit" onclick="btnsubmit_Click" /> <asp:Label ID="lblmsg" runat="server"> ============using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;using System.Configuration;using System.Data;using System.Data.SqlClient;using System.IO;public partial class Admin_images : System.Web.UI.Page{ SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["Con1"].ToString()); SqlCommand com; DataSet ds = new DataSet(); SqlDataAdapter Adapter; SqlDataReader reader; public void Connect() { con.Close(); if (con.State == ConnectionState.Closed) { con.Open(); } } public void Closed() { if (con.State == ConnectionState.Open) { con.Close(); } } protected void Page_Load(object sender, EventArgs e) { try { LoadSubject(); } catch (Exception er) { Response.Redirect(er.Message); } } public void LoadSubject() { try { Select sel = new Select(); DataSet subds = sel.SubjectDistinct(); if (subds.Tables[0].Rows.Count > 0) { ddlsub.DataSource = subds.Tables[0]; ddlsub.DataTextField = "subject"; ddlsub.DataValueField = "subject"; ddlsub.DataBind(); } } catch (Exception er) { Response.Write(er.Message); } } public int addimg(byte[] que,byte[] opt1,byte[] opt2,byte[] opt3,byte[] opt4,string ans,string sub) { int a; Connect(); com = new SqlCommand("insert into DemoTable ([que],[opt1],[opt2],[opt3],[opt4],[ans],[sub]) values(@que,@opt1,@opt2,@opt3,@opt4,@ans,@sub)", con); com.Parameters.AddWithValue("@que", que); com.Parameters.AddWithValue("@opt1", opt1); com.Parameters.AddWithValue("@opt2", opt2); com.Parameters.AddWithValue("@opt3", opt3); com.Parameters.AddWithValue("@opt4", opt4); com.Parameters.AddWithValue("@ans", ans); com.Parameters.AddWithValue("@sub", sub); a = com.ExecuteNonQuery(); Closed(); return a; throw new NotImplementedException(); } protected void btnupload_Click(object sender, EventArgs e) { try { } catch (Exception er) { Response.Write(er.Message); } } protected void btnsubmit_Click(object sender, EventArgs e) { try { //Stream fq = fuque.PostedFile.InputStream; //BinaryReader br = new BinaryReader(fq); //Byte[] rawq = br.ReadBytes((Int32)fq.Length); //Stream f1 = fuque.PostedFile.InputStream; //BinaryReader br1 = new BinaryReader(f1); //Byte[] raw1 = br1.ReadBytes((Int32)f1.Length); //Stream f2 = fuque.PostedFile.InputStream; //BinaryReader br2 = new BinaryReader(f2); //Byte[] raw2 = br2.ReadBytes((Int32)f2.Length); //Stream f3 = fuque.PostedFile.InputStream; //BinaryReader br3 = new BinaryReader(f3); //Byte[] raw3 = br3.ReadBytes((Int32)f3.Length); //Stream f4 = fuque.PostedFile.InputStream; //BinaryReader br4 = new BinaryReader(f4); //Byte[] raw4 = br4.ReadBytes((Int32)f4.Length); FileStream fq = new FileStream(fuque.PostedFile.FileName, FileMode.OpenOrCreate, FileAccess.ReadWrite); byte[] rawq = new byte[fq.Length]; fq.Read(rawq, 0, Convert.ToInt32(fq.Length)); FileStream f1 = new FileStream(fuopt1.PostedFile.FileName, FileMode.OpenOrCreate, FileAccess.ReadWrite); byte[] raw1 = new byte[f1.Length]; f1.Read(raw1, 0, Convert.ToInt32(f1.Length)); FileStream f2 = new FileStream(fuopt2.PostedFile.FileName, FileMode.OpenOrCreate, FileAccess.ReadWrite); byte[] raw2 = new byte[f2.Length]; f2.Read(raw2, 0, Convert.ToInt32(f2.Length)); FileStream f3 = new FileStream(fuopt3.PostedFile.FileName, FileMode.OpenOrCreate, FileAccess.ReadWrite); byte[] raw3 = new byte[f3.Length]; f3.Read(raw3, 0, Convert.ToInt32(f3.Length)); FileStream f4 = new FileStream(fuopt4.PostedFile.FileName, FileMode.OpenOrCreate, FileAccess.ReadWrite); byte[] raw4 = new byte[f4.Length]; f4.Read(raw4, 0, Convert.ToInt32(f4.Length)); int n = addimg(rawq, raw1, raw2, raw3, raw4, ddlopt.SelectedValue, ddlsub.SelectedItem.Value); if (n == 1) { lblmsg.Text = "Q up"; } } catch (NullReferenceException ex) { lblmsg.Text="Processor Usage" + ex.Message; } catch (Exception ex) { lblmsg.Text = "Processor Usage" + ex.Message; } }}This is one of the most common problems we get asked, and it's also the one we are least equipped to answer, but you are most equipped to answer yourself.Let me just explain what the error means: You have tried to use a variable, property, or a method return value but it contains null - which means that there is no instance of a class in the variable.It's a bit like a pocket: you have a pocket in your shirt, which you use to hold a pen. If you reach into the pocket and find there isn't a pen there, you can't sign your name on a piece of paper - and you will get very funny looks if you try! The empty pocket is giving you a null value (no pen here!) so you can't do anything that you would normally do once you retrieved your pen. Why is it empty? That's the question - it may be that you forgot to pick up your pen when you left the house this morning, or possibly you left the pen in the pocket of yesterdays shirt when you took it off last night.We can't tell, because we weren't there, and even more importantly, we can't even see your shirt, much less what is in the pocket!Back to computers, and you have done the same thing, somehow - and we can't see your code, much less run it and find out what contains null when it shouldn't.But you can - and Visual Studio will help you here. Run your program in the debugger and when it fails, VS will show you the line it found the problem on. You can then start looking at the various parts of it to see what value is null and start looking back through your code to find out why. So put a breakpoint at the beginning of the method containing the error line, and run your program from the start again. This time, VS will stop before the error, and let you examine what is going on by stepping through the code looking at your values.But we can't do that - we don't have your full code, we don't know how to use it if we did have it, we don't have your data. So try it - and see how much information you can find out! 这篇关于我编写了以下代码来存储SQL Server 2008 db中的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-30 00:11