| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 9762 人关注过本帖
标题:求教啊,C#一直提示"未将对象引用设置到对象的实例",怎么回事啊?
只看楼主 加入收藏
卡巴斯
Rank: 2
等 级:论坛游民
帖 子:50
专家分:31
注 册:2012-12-18
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:9 
求教啊,C#一直提示"未将对象引用设置到对象的实例",怎么回事啊?
图片附件: 游客没有浏览图片的权限,请 登录注册
这是错误截图
下面是代码
程序代码:
 public List<Customer> GetALL()
        {
            List<Customer> customers = new List<Customer>();
            customers=SqlHelper.ExcuteReader("select * from T_Customer");//就是在这里抛出异常的,可是SqlHelper是个静态类啊
            return customers;
        }
这是SqlHelper的
程序代码:
public static List<Customer> ExcuteReader(string str, params SqlParameter[] para)
        {
            using (SqlConnection sql = new SqlConnection(connection))
            {
                sql.Open();
                using (SqlCommand com = sql.CreateCommand())
                {
                     = str;
                    com.Parameters.AddRange(para);
                    using(SqlDataReader reader=com.ExecuteReader())
                    {
                        if (reader==null)
                        {
                            return null;
                        }
                        List<Customer> customers=new List<Customer>();
                        while (reader.Read())
                        {
                            Customer cus = new Customer();
                            cus.id = (long)reader["id"];
                            cus.name = (string)reader["name"];
                            cus.phonenummer = (string)reader["phonenummer"];
                            cus.address = (string)reader["address"];
                            cus.level = (int)reader["level"];
                            cus.birthday = (DateTime)reader["birthday"];
                            customers.Add(cus);
                        }
                        return customers;                 
                    }
                }
            }
        }

求大牛解答啊

搜索更多相关主题的帖子: customers return 
2013-07-07 22:18
yhlvht
Rank: 13Rank: 13Rank: 13Rank: 13
等 级:贵宾
威 望:36
帖 子:707
专家分:4405
注 册:2011-9-30
收藏
得分:0 
你报错的那句并没有直接调用public static List<Customer> ExcuteReader(string str, params SqlParameter[] para)这个方法
因为你调用的时候传入的是一个参数,而这个方法是两个参数的
2013-07-07 23:23
卡巴斯
Rank: 2
等 级:论坛游民
帖 子:50
专家分:31
注 册:2012-12-18
收藏
得分:0 
回复 2楼 yhlvht
使用params声明的参数不是数目可变的参数吗?这样的声明不是可以省略参数的传递吗?这是studio的帮助文档解释
图片附件: 游客没有浏览图片的权限,请 登录注册

2013-07-07 23:30
yhlvht
Rank: 13Rank: 13Rank: 13Rank: 13
等 级:贵宾
威 望:36
帖 子:707
专家分:4405
注 册:2011-9-30
收藏
得分:0 
不好意思,没有注意到params,那这样的话,应该就是para参数为null的原因了,你设置了parameters参数,但是为null,com.Parameters.AddRange(para);所以查询的时候他自动调用SqlHlper.ExecuteNonQuery(String str, SqlParameter param)这个方法的时候就报错了
2013-07-08 00:28
QJlin
Rank: 7Rank: 7Rank: 7
等 级:黑侠
威 望:3
帖 子:186
专家分:560
注 册:2013-5-18
收藏
得分:0 
你试下把参数补全看下还报错不?你既然都不传这个参数为什么你在下面还调用?

慢慢前进走,不求一步登天,只求慢慢前进
2013-07-08 01:10
yhlvht
Rank: 13Rank: 13Rank: 13Rank: 13
等 级:贵宾
威 望:36
帖 子:707
专家分:4405
注 册:2011-9-30
收藏
得分:0 
楼主的ExcuteReader是个公共方法,调用时根据sql语句的不同,可能会有参数,也可能没有
Parameters参数在默认的时候是个空集合(空集合并不是null,而是说集合里面没有值,但集合是存在的)
所以要么在调用的时候,new一个空集合做为参数
或者在ExcuteReader方法中判断para是否为null,不为null时才执行com.Parameters.AddRange(para),个人建议判断para是否为null
2013-07-08 02:10
卡巴斯
Rank: 2
等 级:论坛游民
帖 子:50
专家分:31
注 册:2012-12-18
收藏
得分:0 
回复 6楼 yhlvht
加了判断了,还是不行,这是修改后的代码
程序代码:
public static List<Customer> ExcuteReader(string str, params SqlParameter[] para)
        {
            using (SqlConnection sql = new SqlConnection(connection))
            {
                sql.Open();
                using (SqlCommand com = sql.CreateCommand())
                {
                     = str;
                    if(para!=null&&para.Length>0)
                    {
                        com.Parameters.AddRange(para);
                    }                    
                    using (SqlDataReader reader = com.ExecuteReader())
                    {
                        if (reader == null)
                        {
                            return new List<Customer>(); 
                        }
                        List<Customer> customers = new List<Customer>();
                        while (reader.Read())
                        {
                            Customer cus = new Customer();
                            cus.id = (long)reader["id"];
                            cus.name = (string)reader["name"];
                            cus.phonenummer = (string)reader["phonenummer"];
                            cus.address = (string)reader["address"];
                            cus.level = (int)reader["level"];
                            cus.birthday = (DateTime)reader["birthday"];
                            customers.Add(cus);
                        }
                        return customers;
                    }
                }
            }
        }

下面的是错误的详细信息
程序代码:
未处理 System.TypeInitializationException
  Message=“人事管理练习.DAL.SqlHelper”的类型初始值设定项引发异常。
  Source=人事管理练习
  TypeName=人事管理练习.DAL.SqlHelper
  StackTrace:
       在 人事管理练习.DAL.SqlHelper.ExcuteReader(String str, SqlParameter[] para)
       在 人事管理练习.MainWindow.Window_Loaded(Object sender, RoutedEventArgs e) 位置 F:\编程学习\C#\人事管理练习\人事管理练习\MainWindow.xaml.cs:行号 43
       在 System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
       在 System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
       在 System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)
       在 System.Windows.UIElement.RaiseEvent(RoutedEventArgs e)
       在 System.Windows.BroadcastEventHelper.BroadcastEvent(DependencyObject root, RoutedEvent routedEvent)
       在 System.Windows.BroadcastEventHelper.BroadcastLoadedEvent(Object root)
       在 MS.Internal.LoadedOrUnloadedOperation.DoWork()
       在 System.Windows.Media.MediaContext.FireLoadedPendingCallbacks()
       在 System.Windows.Media.MediaContext.FireInvokeOnRenderCallbacks()
       在 System.Windows.Media.MediaContext.RenderMessageHandlerCore(Object resizedCompositionTarget)
       在 System.Windows.Media.MediaContext.RenderMessageHandler(Object resizedCompositionTarget)
       在 System.Windows.Media.MediaContext.Resize(ICompositionTarget resizedCompositionTarget)
       在 System.Windows.Interop.HwndTarget.OnResize()
       在 System.Windows.Interop.HwndTarget.HandleMessage(WindowMessage msg, IntPtr wparam, IntPtr lparam)
       在 System.Windows.Interop.HwndSource.HwndTargetFilterMessage(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
       在 MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
       在 MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
       在 System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
       在 MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler)
       在 System.Windows.Threading.Dispatcher.InvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs)
       在 MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
       在 MS.Win32.UnsafeNativeMethods.ShowWindow(HandleRef hWnd, Int32 nCmdShow)
       在 System.Windows.Window.ShowHelper(Object booleanBox)
       在 System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
       在 MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler)
       在 System.Windows.Threading.DispatcherOperation.InvokeImpl()
       在 System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(Object state)
       在 System.Threading.ExecutionContext.runTryCode(Object userData)
       在 System.(TryCode code, CleanupCode backoutCode, Object userData)
       在 System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
       在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
       在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       在 System.Windows.Threading.DispatcherOperation.Invoke()
       在 System.Windows.Threading.Dispatcher.ProcessQueue()
       在 System.Windows.Threading.Dispatcher.WndProcHook(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
       在 MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
       在 MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
       在 System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
       在 MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler)
       在 System.Windows.Threading.Dispatcher.InvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs)
       在 MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
       在 MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg)
       在 System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame)
       在 System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame)
       在 System.Windows.Application.RunDispatcher(Object ignore)
       在 System.Windows.Application.RunInternal(Window window)
       在 System.Windows.Application.Run(Window window)
       在 System.Windows.Application.Run()
       在 人事管理练习.App.Main() 位置 F:\编程学习\C#\人事管理练习\人事管理练习\obj\x86\Debug\App.g.cs:行号 0
       在 System.AppDomain._nExecuteAssembly(RuntimeAssembly 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, Boolean ignoreSyncCtx)
       在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       在 System.Threading.ThreadHelper.ThreadStart()
  InnerException: System.NullReferenceException
       Message=未将对象引用设置到对象的实例。
       Source=人事管理练习
       StackTrace:
            在 人事管理练习.DAL.SqlHelper..cctor() 位置 F:\编程学习\C#\人事管理练习\人事管理练习\DAL\SqlHelper.cs:行号 14
       InnerException: 

下了断点调试,异常是在进入方法体之前就抛出了的,不是在方法内部抛出的
2013-07-08 09:54
yhlvht
Rank: 13Rank: 13Rank: 13Rank: 13
等 级:贵宾
威 望:36
帖 子:707
专家分:4405
注 册:2011-9-30
收藏
得分:20 
既然可以调试,那你断点走到customers=SqlHelper.ExcuteReader("select * from T_Customer");这一行的时候,就不要往下走,停在这,然后鼠标移到SqlHelper上,看SqlHelper是不是null
2013-07-08 10:25
卡巴斯
Rank: 2
等 级:论坛游民
帖 子:50
专家分:31
注 册:2012-12-18
收藏
得分:0 
回复 8楼 yhlvht
找到问题了,配置文件写错了导致类初始化失败了
2013-07-08 11:41
Maick
Rank: 9Rank: 9Rank: 9
等 级:贵宾
威 望:32
帖 子:251
专家分:1314
注 册:2012-9-21
收藏
得分:0 
结贴也是一种素质
2013-07-09 14:49
快速回复:求教啊,C#一直提示"未将对象引用设置到对象的实例",怎么回事啊? ...
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.022867 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved