FineUI 官方论坛

标题: 随机报错 [打印本页]

作者: qwery    时间: 2014-1-14 01:09
标题: 随机报错
随机性报错,报错信息如下:
  1. Server Error in '/' Application.

  2. Object reference not set to an instance of an object.

  3. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

  4. Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

  5. Source Error:

  6. An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

  7. Stack Trace:


  8. [NullReferenceException: Object reference not set to an instance of an object.]
  9.    FineUI.CommonResourceHelper.IsHeaderContains(Page page, String controlId) +40
  10.    FineUI.CommonResourceHelper.AddContentToHead(Page page, String controlId, String msg) +19
  11.    FineUI.CommonResourceHelper.RegisterCommonResource(Page page) +87
  12.    FineUI.ResourceManager.SetupFirstLoadResource() +14
  13.    FineUI.ResourceManager.Page_PreRenderComplete(Object sender, EventArgs e) +190
  14.    System.Web.UI.Page.OnPreRenderComplete(EventArgs e) +8695102
  15.    System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1029

  16. Version Information: Microsoft .NET Framework Version:2.0.50727.3053; ASP.NET Version:2.0.50727.3053
复制代码
代码前端为:
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="validate.aspx.cs" Inherits="validate" %>

  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head runat="server">
  5.     <title></title>
  6. </head>
  7. <body>
  8.     <form id="form1" runat="server">
  9.     <div>
  10.    
  11.     </div>
  12.     </form>
  13. </body>
  14. </html>
复制代码
后端为:

  1. using System;
  2. using System.Drawing;
  3. public partial class validate : System.Web.UI.Page
  4. {
  5.     protected void Page_Load(object sender, EventArgs e)
  6.     {
  7.         Random rad = new Random(Guid.NewGuid().GetHashCode());
  8.         string aa = "";
  9.         for (int i = 0; i < 4; i++)
  10.         {
  11.             aa = aa + rad.Next(10).ToString();
  12.         }
  13.         Session["yanzheng"] = aa;
  14.          CreateImages(Convert.ToString(aa));
  15.     }
  16.     /// <summary>
  17.     /// /// 生成验证图片
  18.     /// /// </summary>
  19.     /// /// <param name="checkCode">验证字符</param>
  20.     protected void CreateImages(string checkCode)
  21.     {
  22.         int iwidth = (int)(checkCode.Length * 15);
  23.         System.Drawing.Bitmap image = new System.Drawing.Bitmap(iwidth, 30);
  24.         Graphics g = Graphics.FromImage(image);
  25.         g.Clear(Color.LightCyan);
  26.         //定义颜色
  27.         Color[] c = { Color.Black, Color.Red, Color.DarkBlue, Color.Green, Color.Orange, Color.Brown, Color.DarkCyan, Color.Purple, Color.SkyBlue };
  28.         //定义字体
  29.         string[] font = { "Verdana", "Microsoft Sans Serif", "Comic Sans MS", "Arial", "宋体", "Comic Sans MS" };
  30.         Random rand = new Random(Guid.NewGuid().GetHashCode());
  31.         //随机输出噪点
  32.         for (int i = 0; i < 150; i++)
  33.         {
  34.             int x = rand.Next(image.Width);
  35.             int y = rand.Next(image.Height);
  36.            // g.DrawPie(new Pen(GetRandomColor(), 0), x, y, 6, 6, 1, 1);
  37.         }
  38.         for (int i = 0; i < 12; i++)
  39.         {
  40.             int x1 = rand.Next(image.Width);
  41.             int x2 = rand.Next(image.Width);
  42.             int y1 = rand.Next(image.Height);
  43.             int y2 = rand.Next(image.Height);

  44.             g.DrawLine(new Pen(GetRandomColor()), x1, y1, x2, y2);
  45.         }

  46.         //输出不同字体和颜色的验证码字符
  47.         for (int i = 0; i < checkCode.Length; i++)
  48.         {
  49.             int cindex = rand.Next(7);
  50.             int findex = rand.Next(6);
  51.             Font _font = new System.Drawing.Font(font[findex], 14, System.Drawing.FontStyle.Bold);
  52.             Brush b = new System.Drawing.SolidBrush(c[cindex]);
  53.             int ii = 4;
  54.             if ((i + 1) % 2 == 0)
  55.             {
  56.                 ii = 2;
  57.             }
  58.             g.DrawString(checkCode.Substring(i, 1), _font, b, 3 + (i * 12), ii);
  59.         }
  60.         Session["yanzheng"] = checkCode; //保存验证码
  61.         //画一个边框
  62.         g.DrawRectangle(new Pen(Color.Red, 0), 100, 0, image.Width - 1, image.Height - 1);
  63.         //输出到浏览器
  64.         System.IO.MemoryStream ms = new System.IO.MemoryStream();
  65.         image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);

  66.         Response.ClearContent();//Response.ClearContent();
  67.         Response.ContentType = "image/Jpeg";
  68.         Response.BinaryWrite(ms.ToArray());

  69.         g.Dispose();
  70.         image.Dispose();
  71.     }
  72.     //////////////////////////////////////////////////////////
  73.     /// /// 生成随机颜色
  74.     /// //////////////////////////////////////////////////////////

  75.     public static System.Drawing.Color GetRandomColor()
  76.     {
  77.         Random randomNum_1 = new Random(Guid.NewGuid().GetHashCode());
  78.         System.Threading.Thread.Sleep(randomNum_1.Next(1));
  79.         int int_Red = randomNum_1.Next(255);

  80.         Random randomNum_2 = new Random((int)DateTime.Now.Ticks);
  81.         int int_Green = randomNum_2.Next(255);

  82.         Random randomNum_3 = new Random(Guid.NewGuid().GetHashCode());

  83.         int int_Blue = randomNum_3.Next(255);
  84.         int_Blue = (int_Red + int_Green > 380) ? int_Red + int_Green - 380 : int_Blue;
  85.         int_Blue = (int_Blue > 255) ? 255 : int_Blue;


  86.         return GetDarkerColor(System.Drawing.Color.FromArgb(int_Red, int_Green, int_Blue));
  87.     }

  88.     //获取加深颜色
  89.     public static Color GetDarkerColor(Color color)
  90.     {
  91.         const int max = 255;
  92.         int increase = new Random(Guid.NewGuid().GetHashCode()).Next(255); //还可以根据需要调整此处的值


  93.         int r = Math.Abs(Math.Min(color.R - increase, max));
  94.         int g = Math.Abs(Math.Min(color.G - increase, max));
  95.         int b = Math.Abs(Math.Min(color.B - increase, max));


  96.         return Color.FromArgb(r, g, b);
  97.     }
  98. }
复制代码


web。config为
  1. <?xml version="1.0"?>
  2. <!--
  3.     注意: 除了手动编辑此文件以外,您还可以使用
  4.     Web 管理工具来配置应用程序的设置。可以使用 Visual Studio 中的
  5.      “网站”->“Asp.Net 配置”选项。
  6.     设置和注释的完整列表在
  7.     machine.config.comments 中,该文件通常位于
  8.     \Windows\Microsoft.Net\Framework\v2.x\Config 中
  9. -->
  10. <configuration>
  11.         <configSections>
  12.                 <section name="FineUI" type="FineUI.ConfigSection, FineUI"/>
  13.         </configSections>
  14.         <!-- 可用的配置项(这里列的都是默认值):Language="zh_CN" AjaxTimeout="60" EnableAjax="true" Theme="blue" FormMessageTarget="qtip" FormOffsetRight="20" FormLabelWidth="100" FormLabelSeparator=":" IconBasePath="~/icon" EnableAjaxLoading="true" AjaxLoadingType="default" CustomTheme="" CustomThemeBasePath="~/theme" -->
  15.         <FineUI EnableBigFont="true" DebugMode="false" AjaxTimeout="6000"/>
  16.         <appSettings>
  17.         </appSettings>
  18.         <system.web>
  19.                 <sessionState mode="InProc" timeout="1000"/>
  20.                 <!-- Net3.5以上的项目,一定要为pages节点加上这两个属性:controlRenderingCompatibilityVersion="3.5" clientIDMode="AutoID" -->
  21.                 <pages>
  22.                         <controls>
  23.                                 <add assembly="FineUI" namespace="FineUI" tagPrefix="ext"/>
  24.                         </controls>
  25.                 </pages>
  26.                 <httpModules>
  27.                         <add name="FineUIScriptModule" type="FineUI.ScriptModule, FineUI"/>
  28.                 </httpModules>
  29.                 <httpHandlers>
  30.                         <add verb="GET" path="res.axd" type="FineUI.ResourceHandler, FineUI"/>
  31.                 </httpHandlers>
  32.                 <customErrors mode="Off"/>
  33.                 <compilation debug="true">
  34.                         <assemblies>
  35.                                 <add assembly="System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>
  36.                         </assemblies></compilation>
  37.         </system.web>
  38.         <system.webServer>
  39.                 <modules>
  40.                         <add name="FineUIScriptModule" type="FineUI.ScriptModule, FineUI"/>
  41.                 </modules>
  42.                 <httpHandlers>
  43.                         <add verb="GET" path="res.axd" type="FineUI.ResourceHandler, FineUI"/>
  44.                         <add verb="Get" path="WebResource.axd" type="System.Web.Handlers.AssemblyResourceLoader"/>
  45.                 </httpHandlers>
  46.                 <httpErrors errorMode="Detailed"/>
  47.                 <asp scriptErrorSentToBrowser="true"/>
  48.         </system.webServer>
  49.         <system.codedom>
  50.         </system.codedom>
  51. </configuration>
复制代码


有时候工作很正常,有时候爆上述错误

作者: sanshi    时间: 2014-1-14 09:43
要相信程序每次执行结果的稳定性,然后再找问题。

请使用空项目重建重现问题的示例,才有助于解决问题
作者: qwery    时间: 2014-1-14 14:20
sanshi 发表于 2014-1-14 09:43
要相信程序每次执行结果的稳定性,然后再找问题。

请使用空项目重建重现问题的示例,才有助于解决问题 ...

找到重现的方法了,第一次打开页面会报错,然后有成功打开页面后,以后的都不会报错。

异常的代码位于

  1.         internal static void RegisterCommonResource(Page page)
  2.         {
  3.             #region powered-by

  4.             //string metaName = "powered-by";
  5.             //string metaContent = String.Format("FineUI v{0} - 基于 ExtJS 的专业 ASP.NET 控件库 - http://fineui.com/", GlobalConfig.ProductVersion);
  6.             //AddContentToHead(page, CONTROL_ID_PREFIX + "comments", String.Format(META_TEMPLATE, metaName, metaContent));

  7.             #endregion

  8.             // ExtJS CSS & JS 版本号,只有升级更新CSS或者JS时才需要更新。
  9.             //string extjsCSSJSVersion = "3";
  10.             //string fineuiVersion = GlobalConfig.ProductVersion;
  11.             string extjsBasePath = page.ResolveUrl(GlobalConfig.GetExtjsBasePath());

  12.             #region css

  13.             if (!String.IsNullOrEmpty(PageManager.Instance.CustomTheme))
  14.             {
  15.                 AddCssPathToHead(page, CONTROL_ID_PREFIX + "notheme.css", String.Format("{0}/res/css/notheme.css", extjsBasePath));
复制代码


     if (!String.IsNullOrEmpty(PageManager.Instance.CustomTheme)) 爆出异常:

第一次打开页面:PageManager.Instance 为NULL,

用户代码未处理 System.NullReferenceException
  Message=未将对象引用设置到对象的实例。
  Source=FineUI
  StackTrace:
       在 FineUI.CommonResourceHelper.RegisterCommonResource(Page page)
       在 FineUI.ResourceManager.SetupFirstLoadResource()
       在 FineUI.ResourceManager.Page_PreRenderComplete(Object sender, EventArgs e)
       在 System.Web.UI.Page.OnPreRenderComplete(EventArgs e)
       在 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
  InnerException:

作者: rookies_lts    时间: 2014-1-14 14:51
你的aspx页面没有加PageManager控件,加上再试试
<x: PageManager ID="PageManager1" runat="server" />

作者: qwery    时间: 2014-1-14 23:11
rookies_lts 发表于 2014-1-14 14:51
你的aspx页面没有加PageManager控件,加上再试试

我是做下载二进制功能,如果加上面代码,会在文件中附加html代码,导致文件损坏




欢迎光临 FineUI 官方论坛 (https://fineui.com/bbs/) Powered by Discuz! X3.4