标题:求教啊,C#一直提示"未将对象引用设置到对象的实例",怎么回事啊?
取消只看楼主
卡巴斯
Rank: 2
等 级:论坛游民
帖 子:50
专家分:31
注 册:2012-12-18
结帖率:100%
已结贴  问题点数:20 回复次数:3 
求教啊,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
卡巴斯
Rank: 2
等 级:论坛游民
帖 子:50
专家分:31
注 册:2012-12-18
得分:0 
回复 2楼 yhlvht
使用params声明的参数不是数目可变的参数吗?这样的声明不是可以省略参数的传递吗?这是studio的帮助文档解释

2013-07-07 23:30
卡巴斯
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
卡巴斯
Rank: 2
等 级:论坛游民
帖 子:50
专家分:31
注 册:2012-12-18
得分:0 
回复 8楼 yhlvht
找到问题了,配置文件写错了导致类初始化失败了
2013-07-08 11:41



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-416864-1-1.html




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

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