c#求助,未将对象引用到实例化

xiaolong263 2011-08-11 07:02:07
如题:这是错误代码提示:

未处理 System.NullReferenceException
Message=未将对象引用设置到对象的实例。
Source=testwinfrm
StackTrace:
在 testwinfrm.Mainfrm.DrawTop_Left(Graphics g) 位置 C:\Documents and Settings\Administrator\桌面\testwinfrm\testwinfrm\Mainfrm.cs:行号 126
在 testwinfrm.Mainfrm.OnPaint(PaintEventArgs e) 位置 C:\Documents and Settings\Administrator\桌面\testwinfrm\testwinfrm\Mainfrm.cs:行号 110
在 System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e, Int16 layer, Boolean disposeEventArgs)
在 System.Windows.Forms.Control.WmPaint(Message& m)
在 System.Windows.Forms.Control.WndProc(Message& m)
在 System.Windows.Forms.ScrollableControl.WndProc(Message& m)
在 System.Windows.Forms.ContainerControl.WndProc(Message& m)
在 System.Windows.Forms.Form.WndProc(Message& m)
在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
在 System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
在 System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
在 System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
在 System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
在 System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
在 System.Windows.Forms.Application.Run(Form mainForm)
在 testwinfrm.Program.Main() 位置 C:\Documents and Settings\Administrator\桌面\testwinfrm\testwinfrm\Program.cs:行号 18
在 System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
在 System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
在 System.Threading.ThreadHelper.ThreadStart_Context(Object state)
在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
在 System.Threading.ThreadHelper.ThreadStart()
InnerException:


程序代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Resources;
namespace testwinfrm
{
public partial class Mainfrm : Form
{
///

/// 定义一个资源管理器
///

private ResourceManager rm;
private Image Top_Left;

private Image SysButton_Re;
private int TITLE_WIDTH = 200;
//private bool Resource = false;

private static string Skin_Path = Environment.CurrentDirectory + "\\Skin\\";//皮肤文件夹路径


//创建一个Resource文件
public static void Create()
{
if (!System.IO.File.Exists("skin.Resource"))
{
ResourceWriter rw = new ResourceWriter("skin.Resource");//创建一个资源文件
try
{
Image Top_Left = Image.FromFile(Skin_Path + "Top_Left.png");

rw.AddResource("Top_Left.png", Top_Left);

rw.AddResource("Mystr", "从资源文件中读取字符串!");
rw.Generate();
rw.Close();
}
catch
{
rw.Close();
throw new ArgumentException(Skin_Path);
}
}
}
public Mainfrm()
{
Create();
try
{
rm = new ResourceManager("SkinWindow.Skin", this.GetType().Assembly);
//读取图片
Top_Left = (Image)rm.GetObject("Top_Left.png");

}
catch
{
}
InitializeComponent();
}

protected override void OnPaint(PaintEventArgs e)
{
Graphics g = e.Graphics;
//手工画窗体的各个部分
DrawTop_Left(e.Graphics);//

}
private void DrawTop_Left(Graphics g)
{
//Rectangle Rect1 = new Rectangle(0, 0, Top_Left.Width, Top_Left.Height);
Brush brush = new TextureBrush(Top_Left, new Rectangle(0, 0, Top_Left.Width, Top_Left.Height));

g.FillRectangle(brush, 0, TITLE_WIDTH, Top_Left.Width,Height - Top_Left.Height - TITLE_WIDTH);
}

}
}

就在有颜色地方报错的,急需解决方法,再次谢谢大虾。
本人菜鸟,希望大大们帮我解决下,小弟不胜感激,留下邮箱:pl369@163.com顺便讨点c#编程资料,关于winform和oracle方面的。
...全文
321 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhouwc520 2011-08-11
  • 打赏
  • 举报
回复
rm = new ResourceManager("SkinWindow.Skin", this.GetType().Assembly);
Top_Left = (Image)rm.GetObject("Top_Left.png");
改为如下方式就OK了
ResourceSet rs = new ResourceSet("skin.resource");
Top_Left = (Image)rs.GetObject("Top_Left.png");
xiongxyt2 2011-08-11
  • 打赏
  • 举报
回复
未将对象引用到实例化,就是你引用了为实例化的对象,对象你空的,你这里应该就是Top_Left,他应该是一个Image的对象。
zhouwc520 2011-08-11
  • 打赏
  • 举报
回复


//背景图片拆分
private static Bitmap tl = Properties.Resources.Top_Left;
private static Bitmap tm = Properties.Resources.Top_Middle;
private static Bitmap tr = Properties.Resources.Top_Right;

private static Bitmap ml = Properties.Resources.Middle_Left;
private static Bitmap mm = Properties.Resources.Middle_Content;
private static Bitmap mr = Properties.Resources.Middle_Right;

private static Bitmap bl = Properties.Resources.Bottom_Left;
private static Bitmap bm = Properties.Resources.Bottom_Middle;
private static Bitmap br = Properties.Resources.Bottom_Right;


//重绘窗体背景
private void FormPaint(object sender, System.Windows.Forms.PaintEventArgs e)
{
//重绘左上角
e.Graphics.DrawImage(tl, 0, 0, tl.Width, tl.Height);
//重绘上方中间
TextureBrush brush = new TextureBrush(tm, new Rectangle(0, 0, tm.Width, tm.Height));
e.Graphics.FillRectangle(brush, tl.Width, 0, Width - tl.Width - tr.Width, tm.Height);
//重绘右上角
e.Graphics.DrawImage(tr, Width - tr.Width, 0, tr.Width, tr.Height);

//重绘左边框
brush = new TextureBrush(ml);
e.Graphics.FillRectangle(brush, 0, tl.Height, ml.Width, Height - tl.Height - bl.Height);
//重绘中间背景
brush = new TextureBrush(mm);
e.Graphics.FillRectangle(brush, ml.Width, tl.Height, Width - ml.Width - mr.Width, Height - tl.Height - bl.Height);
//重绘右边框
brush = new TextureBrush(mr);
brush.TranslateTransform(Width - mr.Width, tr.Height);//把偏移量补偿回来
e.Graphics.FillRectangle(brush, Width - mr.Width, tl.Height, mr.Width, Height - tr.Height - br.Height);

//重绘左下角
e.Graphics.DrawImage(bl, 0, Height - bl.Height, bl.Width, bl.Height);
//重绘下方中间
brush = new TextureBrush(bm);
brush.TranslateTransform(bl.Width, Height - bm.Height);//把偏移量补偿回来
e.Graphics.FillRectangle(brush, bl.Width, Height - bm.Height, Width - bl.Width - br.Width, bm.Height);
//重绘右下角
e.Graphics.DrawImage(br, Width - br.Width, Height - br.Height, br.Width, br.Height);
}


那个判断直接跳过了,所以没有初始化Top_Left
hui_play 2011-08-11
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 zhouwc520 的回复:]
Brush brush = new TextureBrush(Top_Left, new Rectangle(0, 0, Top_Left.Width, Top_Left.Height));

Top_Left的值是null的,if (!System.IO.File.Exists("skin.Resource"))这后边的语句如果没执行,就没对Top_Left初始化
[/Quote]

应该是没进去条件
zhouwc520 2011-08-11
  • 打赏
  • 举报
回复
Brush brush = new TextureBrush(Top_Left, new Rectangle(0, 0, Top_Left.Width, Top_Left.Height));

Top_Left的值是null的,if (!System.IO.File.Exists("skin.Resource"))这后边的语句如果没执行,就没对Top_Left初始化
xiaolong263 2011-08-11
  • 打赏
  • 举报
回复
谢谢大家,在没有高人回答钱,希望大家顶顶,不要让帖子沉了。

110,537

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧