在类中调用静态方法的空引用异常 [英] Null reference exception calling a static method in a class

查看:93
本文介绍了在类中调用静态方法的空引用异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好



我有一个多项目Web应用程序,在其Global.asax中调用类上的静态方法。

此调用生成空引用异常。该异常不是由方法本身内部的任何内容引起的(据我所知),而仅仅是通过方法调用(我假设这只是基于通过在方法的第一行放置一个断点,断点是从未到过)。

我在< system.web>中有一个

< compilation debug =true

设置。 Web应用程序web.config的一部分。

这可能与我试图调用的静态方法在调用的另一个项目(在同一个解决方案中)中声明的类有关项目?

可能与调试配置有关吗?



这是代码的基本部分:



 Web应用程序解决方案

Web应用程序项目(Global.Asax.cs):

使用BLL ;
命名空间Agenda2
{

公共类MvcApplication:System.Web.HttpApplication
{
............. .........................
protected void SessionStart()
{
BLL_Configuration.readParameter();

..................
}
............... .......


}

}

商业逻辑项目:

namespace BLL
{
public class BLL_Configuration {

public static void readParameter()
{
.........
}

}





我的尝试:



尝试对被调用方法中的任何代码进行注释

解决方案

然后你需要使用调试器更好 - 从调用方法开始,并确保你接到电话。

直到你可以找到一个有效的断点,你甚至不能开始寻找空值而不添加成堆记录代码到您的应用程序,以找出它不应该是什么值为null。



抱歉 - 但我们不能为你做到这一点!



引用:我到达方法调用(即

 BLL_Configuration.readParameter(); 



line);

每当我尝试介入或跳过时我都会得到异常。

顺便说一句,如果我在第一行代码上设置一个断点

 readParameter 

方法,这是永远不会达到的。





所以检查三件事:

1)BLL_Configuration类是否有静态构造函数?如果是这样,断点即。

2)BLL_Configuration类是否包含任何具有初始值的静态变量?这些都取决于其他事情吗?任何初始化程序都可以抛出异常吗?

3)例外是否包含文件和/或行信息?它通常会...



请记住静态初始化将在调用任何静态方法之前发生,因此问题很可能就在那里。


Hello

I have a multi-project web application which is calling, in its Global.asax, a static method on a class.
This call generates a null reference exception. The exception is not caused (as far as I know) by anything inside the method itself but merely by the method call (I assume this simply on the basis that , by putting a breakpoint on the very first line in the method, the breakpoint is never reached).
I have a

<compilation debug="true" 

setting in the <system.web> section of the web application web.config.
Can it be related to the fact that the class whose static method I am trying to call is declared in a different project (in the same solution) from the calling project ?
Maybe anything related to the debug configuration ?

This is the essential part of the code :

web application solution

web application project (Global.Asax.cs):

using BLL;
namespace Agenda2
{

 public class MvcApplication : System.Web.HttpApplication
 {
      ......................................
      protected void  SessionStart ()
      {
         BLL_Configuration.readParameter() ;

         ..................          
      }
      ......................


 }

}
Business Logic Project :

namespace BLL
{
public class BLL_Configuration {

       public static void readParameter()
       {
            .........
       }

}



What I have tried:

Tried commenting any code in the called method

解决方案

Then you need to use the debugger better - start with the calling method, and make sure you get to the call.
Until you can get to a breakpoint that works, you can't even start looking for the null value without adding piles of logging code to your app to find out what value is null when it shouldn't be.

And sorry - but we can't do that for you!

Quote:

I do reach the method call (that is the

BLL_Configuration.readParameter() ;


line);
whenever I try to step in or step over I get the exception.
By the way , if I put a breakpoint on the first line of code
of the

readParameter

method, this is never reached.



So check three things:
1) Is there a static constructor for the BLL_Configuration class? If so, breakpoint that.
2) Does the BLL_Configuration class contain any static variables with initial values? Do any of those depend on other things? Could any of the initializers be throwing the exception?
3) Does the exception include file and / or line information? It usually does...

Remember that static initialization will occur before any static method is called, so it's very likely that the problem is somewhere there.


这篇关于在类中调用静态方法的空引用异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆