最近我正在开发一种解析和显示网站XML信息的软件.够简单吧?
我正在获取NullReferenceExceptions的LOADS.例如,这种方法:
private void SetUserFriends(List<Friend> list)
{
int x = 40;
int y = 3;
if (list != null)
{
foreach (Friend friend in list)
{
FriendControl control = new FriendControl();
control.ID = friend.ID;
control.URL = friend.URL;
control.SetID(friend.ID);
control.SetName(friend.Name);
control.SetImage(friend.Photo);
control.Location = new Point(x, y);
panel2.Controls.Add(control);
y = y + control.Height + 4;
}
}
}
Run Code Online (Sandbox Code Playgroud)
我不得不把一个丑陋的包裹作为罪恶如果在实际的foreach循环周围,以防止异常.
我觉得我只是把绷带放在轮胎上,而不是实际解决问题.有什么办法可以解决这个问题吗?也许我应该读一本关于编程模式的书或不是什么?
真的,我迷路了.我可能会问错误的问题.
foreach (string s in myField.getChilds()) {
if (s == null)
//handle null
else
//handle normal value
}
Run Code Online (Sandbox Code Playgroud)
当我运行我的程序时,我得到一个NullReferenceException,因为getChilds可能返回null.如何使程序继续运行并处理异常?我不能在foreach之外处理它,无法解释为什么因为它需要花费太多时间(而且我相信你们很忙:P).有任何想法吗?
我已经尝试过这种方式:
foreach (string s in myField.getChilds() ?? new ArrayList(1)) {
if (s == null)
//handle null
else
//handle normal value
}
Run Code Online (Sandbox Code Playgroud)
但它不起作用,程序只是跳到foreach的末尾,但我想让它进入foreach而不是!
我想List用我的自定义类包装类.至于现在我有这样的事情;
public class PriorityListOfNodes
{
private List<Node> list_;
private IComparer<Node> sorter_;
public List<Node> List_ {
get {return list_;}
set {list_ = value;}
}
public PriorityListOfNodes ()
{
sorter_ = new NodeSorter_fValueDescending ();
}
public Node PopFromEnd ()
{
Node temp = new Node (list_ [list_.Count - 1]);
list_.RemoveAt (list_.Count - 1);
return temp;
}
public Node PeekFromEnd ()
{
return list_ [list_.Count - 1];
}
public void Add (ref Node toAdd)
{
Debug.Log (toAdd);
list_.Add (toAdd);
list_.Sort (sorter_); …Run Code Online (Sandbox Code Playgroud) 我有一节课:
public class Cell
{
public enum cellState
{
WATER,
SCAN,
SHIPUNIT,
SHOT,
HIT
}
public Cell()
{
currentCell = cellState.WATER;
MessageBox.Show(currentCell.ToString());
}
public cellState currentCell { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
然后我尝试在以下类中使用它:
public class NietzscheBattleshipsGameModel
{
private byte MAXCOL = 10;
private byte MAXROW = 10;
public Cell[,] HomeArray;
private Cell[,] AwayArray;
public NietzscheBattleshipsGameModel()
{
HomeArray = new Cell [MAXCOL, MAXROW];
AwayArray = new Cell [MAXCOL, MAXROW];
}
public string alphaCoords(Int32 x)
{
if (x < 0 || x > …Run Code Online (Sandbox Code Playgroud) 我有以下代码片段,产生编译错误:
public List<string> batchaddresses;
public MapFiles(string [] addresses)
{
for (int i = 0; i < addresses.Count(); i++)
{
batchaddresses.AddRange(Directory.GetFiles(addresses[i], "*.esy"));
}
}
Run Code Online (Sandbox Code Playgroud)
我尝试使用该List<T>.AddRange()方法时出错:
Object reference not set to an instance of an object
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
我们在ASP.NET Web应用程序中得到随机的System.NullReferenceException.我们正在使用inproc会话状态.堆栈跟踪:
System.NullReferenceException: Object reference not set to an instance of an object.
at System.Collections.Specialized.NameObjectCollectionBase.BaseRemove(String name)
at System.Web.SessionState.SessionStateItemCollection.Remove(String name)
at System.Web.SessionState.HttpSessionStateContainer.Remove(String name)
at System.Web.UI.SessionPageStatePersister.Save()
at System.Web.UI.Page.SaveAllState()
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
Run Code Online (Sandbox Code Playgroud)
问题似乎来自Page.PageStatePersister属性,我们已经覆盖了这样的属性.
protected override PageStatePersister PageStatePersister {
get {
return new SessionPageStatePersister(Page);
}
}
Run Code Online (Sandbox Code Playgroud)
Reflector显示SessionPageStatePersister在内部调用Session.Remove.
我们在Page上直接调用Session.Remove时会得到类似的随机NullReferenceException.相关的堆栈跟踪:
---> System.NullReferenceException: Object reference not set to an instance of an object.
at System.Collections.Specialized.NameObjectCollectionBase.BaseRemove(String name)
at System.Web.SessionState.SessionStateItemCollection.Remove(String name)
at System.Web.SessionState.HttpSessionStateContainer.Remove(String name)
at System.Web.SessionState.HttpSessionState.Remove(String name)
at PERH.WebFramework.Bases.PageBase.OnPreInit(EventArgs e) in
Run Code Online (Sandbox Code Playgroud)
有什么想法,可能会导致那些异常吗?
环境:
IIS6,Windows Server 2003 64位,.net …
我有以下C#类:
public class Locales
{
public Region region { get; set; }
public Buttons buttons { get; set; }
public Fields fields { get; set; }
}
public class Region
{
public Center center { get; set; }
public East east { get; set; }
}
public class Center
{
public string title { get; set; }
}
public class East
{
public string title { get; set; }
}
public class Buttons
{
public string save { get; set; …Run Code Online (Sandbox Code Playgroud) 为什么下面NullReferenceException的声明崩溃a.b.c = LazyInitBAndReturnValue(a);?
class A {
public B b;
}
class B {
public int c;
public int other, various, fields;
}
class Program {
private static int LazyInitBAndReturnValue(A a)
{
if (a.b == null)
a.b = new B();
return 42;
}
static void Main(string[] args)
{
A a = new A();
a.b.c = LazyInitBAndReturnValue(a);
a.b.other = LazyInitBAndReturnValue(a);
a.b.various = LazyInitBAndReturnValue(a);
a.b.fields = LazyInitBAndReturnValue(a);
}
}
Run Code Online (Sandbox Code Playgroud)
赋值表达式从右到左进行计算,因此在我们分配时a.b.c,a.b不应为null.奇怪的是,当调试器中断异常时,它也显示a.b为初始化为非空值.

我是一名初级Android开发人员,每天都会遇到很多错误及其解决方案.我注意到我很多次java.lang.NullPointerException: Attempt to invoke virtual method 'xxx' on a null object reference.Upto NullPointerException,听起来很熟悉,某些东西的价值为null,但问题从下一个短语开始.[请关注代码问题]
在我看来,我发现这个非常不清楚,因为Android工作室不像Eclipse只能说出Attempt to invoke virtual method 'xxx' on a null object reference任何问题,我很难知道我在哪里缺少点.无论是游标,数据库,arraylist还是你能想到的任何对象,像疯狂机器人这样的Android工作室总会给出这个错误.
任何人都可以解释一下这究竟是什么意思,在一般情况下我的方法应该是什么?我得到这个错误?
这是我在我的应用程序中遇到的一种奇怪的行为,它让我发疯.
这个功能很好
ArrayList<HashMap<String, String>> getAllRows() {
Cursor c = db.query(TABLE_NAME, ALL_KEYS, null, null, null, null, null); // THIS LINE IS MY POINT
//
...
//
c.close();
return profilesArray;
}
Run Code Online (Sandbox Code Playgroud)
但另一个功能给我错误
java.lang.NullPointerException: Attempt to invoke virtual method 'android.database.Cursor android.database.sqlite.SQLiteDatabase.query(java.lang.String, java.lang.String[], java.lang.String, java.lang.String[], java.lang.String, java.lang.String, java.lang.String)' on a null object reference …
我创建了一个扩展函数,它将字符串作为输入并检查值并基于Generic Type,将其转换为目标类型并返回它,它运行良好.
现在的问题是,如果我将输入值作为空传递它应该返回null,对于可空类型,但它只是抛出异常.
例如:如果我想将它转换为日期时间,它会抛出异常:
String was not recognized as a valid DateTime
Run Code Online (Sandbox Code Playgroud)
以下是我的代码:
public static class Extension
{
public static T ToNull<T>(this string value)
{
var stringType = "System.Nullable`1[[System.{0}, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]";
if (typeof(T) == typeof(String))
stringType = string.Format(stringType, "String");
if (typeof(T) == typeof(Int32?) || typeof(T) == typeof(Int32))
stringType = string.Format(stringType, "Int32");
if (typeof(T) == typeof(DateTime?) || typeof(T) == typeof(DateTime))
stringType = string.Format(stringType, "DateTime");
if (typeof(T) == typeof(Int64?) || typeof(T) == typeof(Int64))
stringType = string.Format(stringType, "Int64");
Type originalType = …Run Code Online (Sandbox Code Playgroud) c# asp.net generics extension-methods nullreferenceexception