我有一个计算数字的公式,然后我需要把每一项的倍数,并将其存储到一个有序列表,多达数,因为它需要得到300我相信一个for循环是不是最好的这样做的方法,但这就是我得到的.
public List<double> axialLengthFt(double length)
{
fundamental = (1130 / 2) / length;
for (int i = 1; i < 15; i++)
{
double d = fundamental * i;
if (d <= 300)
modes.Add(d); //NullReferenceException here??
else
break;
}
return modes;
}
Run Code Online (Sandbox Code Playgroud)
我不知道为什么我得到NullReferenceException,请帮忙!
我很困惑为什么在尝试返回数据集时我得到一个空引用异常.
我有一个处理程序和DBaccess类,就像在nTier架构中一样.
首先,这是我的casefile处理程序/和访问类的一部分工作正常.我包括他们是因为我无法理解为什么这样做而另一部分没有.
public class CaseFileHandler
{
CaseFileAccess caseaccess = null;
public CaseFileHandler()
{
caseaccess = new CaseFileAccess();
}
public DataSet getCaseFileDataset(int caseID)
{
return caseaccess.GetCaseFileDataSet(caseID);
}
public bool UpdateCasefile(CaseFile casefile)
{
return caseaccess.updateCasefile(casefile);
}
}
Run Code Online (Sandbox Code Playgroud)
这是访问类.返回数据集的方法工作得很好.
public class CaseFileAccess
{
const string CONNECTION_STRING = "Data Source = SQLSERVER;Initial Catalog=CaseManager;Integrated Security=SSPI;";
DBManager _DAL = new DBManager(DataProvider.SqlServer, CONNECTION_STRING);
public CaseFile GetCaseFile(int caseID)
{
CaseFile casefile = new CaseFile();
_DAL.Open();
_DAL.CreateParameters(1);
//stuff
return casefile;
}
public DataSet GetCaseFileDataSet(int caseID)
{
_DAL.Open();
_DAL.CreateParameters(1); …Run Code Online (Sandbox Code Playgroud) int item = (int)Session["item"];我的代码中的这一行给了我一个,NullReferenceException
但我不理解
这是我的代码
它是一个自适应测验的代码,使用自适应算法,从数据库中检索包含重量(水平)和学生水平的问题的问题,因此根据学生水平,将挑选问题,并根据学生的答案,下一个问题将被张贴
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.Data;
using System.Diagnostics;
using System.Data.SqlClient;
using System.Configuration;
namespace WebApplication2
{
partial class completeTest : System.Web.UI.Page
{
int level = 0;
SqlDataSource QuizDataSource = new SqlDataSource();
SqlCommand SqlCmd1;
SqlConnection cn;
SqlDataReader dr;
System.Web.UI.WebControls.RadioButton rb1=new RadioButton();
System.Web.UI.WebControls.RadioButton rb2 = new RadioButton();
System.Web.UI.WebControls.RadioButton rb3 = new RadioButton();
System.Web.UI.WebControls.RadioButton rb4 = new RadioButton();
int weight;
protected void Page_Load(object sender, System.EventArgs …Run Code Online (Sandbox Code Playgroud) 我从"PersonFuntionality"界面创建了"personFuntionality"对象.Interface有一个保存人员详细信息的方法.问题是personFuntionality总是有一个空值.
public PersonFuntionality personFuntionality;
try
{
List<beans.Person> prsn = new List<beans.Person>();
beans.Person psn = new beans.Person();
psn.PersonId = Convert.ToInt32(txtId.Text.Trim());
psn.PersonName = txtName.Text.Trim();
psn.PersonCity = txtCity.Text.Trim();
prsn.Add(psn);
//prsn.PersonId = Convert.ToInt32(txtId.Text.Trim());
//prsn.PersonName = txtName.Text.Trim();
//prsn.PersonCity = txtCity.Text.Trim();
if (personFuntionality != null)
{
bool success = personFuntionality.SavePersonDetails(prsn);
if (success == true)
{
lblResult.Text = "success";
}
else
{
lblResult.Text = "Failed";
}
}
else
{
lblResult.Text = "Object Null";
}
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
Run Code Online (Sandbox Code Playgroud) 我有一个扩展方法ParseItemsToIntegers,它是一个字符串数组的扩展方法,我正在使用它,就像使用任何扩展一样.
var ids = ChkBoxSelected.ParseItemsToIntegers();
Run Code Online (Sandbox Code Playgroud)
我观察到的行为是,如果ChkBoxSelected为null,它将毫无问题地调用扩展方法,但是在扩展方法中它会在空字符串数组上抛出空引用异常.
它是如何解决null上的扩展方法的?
为什么ChkBoxSelected在调用扩展方法时它没有抛出空引用异常?
NullPointerException和NullReferenceException有什么区别?
我试图在C#中实现Knuth-Morris-Pratt算法,类似于纯C中的实现.
请不要告诉我关于正则表达式和C#中的内存工作.因为,本主题的目的不是C#特性的真实和有用,而是关于C#中指针的工作,以及关于学习这些特性的内存工作.
让我向您展示C#和纯C的实现.
Pure C http://ideone.com/FVAzU
我对Pure C中的版本没有任何问题,它没有问题,但没有C#版本.
我在这行上遇到了C#的问题:
int*[] d = new int*[M * sizeof(int)];
*d[0] = 0;
Run Code Online (Sandbox Code Playgroud)
我正在捕捉NullReferenceException是通过将0值设置为*d [0]来处理的.
我看过这个参考:
http://msdn.microsoft.com/en-en/library/system.nullreferenceexception.aspx
尝试取消引用空对象引用时引发的异常.
我完全不明白为什么这个东西在我的C#代码中是空的?
我已经定义了*d指向int []数组的指针并使用new []运算符为此分配了内存,为什么它告诉我,如果已经分配了内存,我将取消引用空对象引用?
还有一个关于在C#中明确删除指针的问题.
我知道,它可以用固定的方式完成,但如果我想要这个而不使用这个关键字?
我记得,我必须为此调用Dispose()方法(但是我没有找到这个方法),设置为null并调用GC.Collect().这是正确的方法吗?
谢谢,
最好的祝福!
我有这个例程:
try
{
CurrentSessionName = SelectedSession?.Name ?? "No Session Selected";
CurrentSessionTolerance = SelectedSession?.Tolerance.ToString() ?? "N/A";
CurrentSessionVerification = SelectedSession?.Verification.Description ?? "N/A";
}
catch (Exception ex)
{
var whatever = ex.GetType(); // hits here with no provided information
Console.WriteLine("Null Reference Exception"); // ignores this
string name = SelectedSession?.Name; // and this
}
Run Code Online (Sandbox Code Playgroud)
那么为什么NullReferenceException即使SelectedSession不是Null ,它仍会抛出错误?
我有条件的
if (item?.Value2?.GetType() != typeof(string) && item.get_Value() == 0)
我相信如果item为null,?.操作将返回null,我认为这将解决为false导致条件短路并且一切都会很好(item.get_Value()不会被调用)
但是我不确定,我想也许我需要这样做
if (item?.Value2?.GetType() ?? 0 != typeof(string) && item.get_Value() == 0)
但我认为这可能是矫枉过正,是否可以安全地避免潜在的空引用异常?
我有一个抛出NullReferenceException的代码段.代码段就是这样的.
if (string.IsNullOrWhiteSpace(user.FirstName) && !(user.FirstName.Length <= 64))
{
// Some Code
}
Run Code Online (Sandbox Code Playgroud)
这里if条件首先要检查是否user.FirstName为Null或WhiteSpace.当值为user.FirstNamenull时,此代码段抛出一个NullReferenceException.我假设通过检查Lengthof 来抛出这个user.FirstName,但由于这是一个短路检查,因此第一个表达式不应满足条件并执行if块内的代码.或者我在这里遗漏了一些东西.