数组,循环和碰撞问题! C# [英] Arrays, Loops and Collision problem! C#

查看:116
本文介绍了数组,循环和碰撞问题! C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题.我是这个网站的新手,也是初学者,所以非常简单的帮助将非常感谢!

未将对象引用设置为对象的实例,并且未处理NullReferenceException.当我的飞船与随机产生的小行星相撞时,会发生什么?为什么这不起作用?

Hi there, I''ve got a problem. I''m new to this site and a beginner so simple help would be great thanks!

Object reference not set to an instance of an object and NullReferenceException was unhandled. that what comes up when my spaceship collides with a randomly generated asteroid, anyone? Ideas why this is not working?

If you require the code, just say!

推荐答案

是的,我们可能需要一些代码,但不要全部粘贴!只是相关的位-发生错误的方法,例如.

话虽如此,大多数时候找出问题所在很简单:

错误显示对象引用未设置为对象的实例"
这意味着您正在某个地方使用对象并尝试使用字段,方法或属性,但是该对象本身未设置.

问题可能是myVariable.myMethodmyArray[index].myMethod
的问题
错误消息(和异常)将告诉您发生在哪一行.在调试器中查看它,并跟踪变量,直到找到问题.

您可能会发现,将所有内容分解成单独的行,直到每行只有一小段代码,这是有帮助的.

Yes we will probably need some code, but don''t paste it all! Just the relevant bits - the method the error occurs in, say.

Having said that, most of the time it is pretty simple to work out where the problem:

The error says "Object reference not set to an instance of an object"
That means that somewhere you are using an object and trying to work with a feild, method, or property, but the object itself is not set.

The problem will be something along the lines of myVariable.myMethod or myArray[index].myMethod

The error message (and the exception) will tell you which line it is occurring on. Look at it in the debugger and follow the variables until you find the problem.

You may find it helps to break everything out into separate variables on separate lines until it is just one tiny bit of code per line.

MyArray[myIndex].myMethod().myProperty = 0;


变为:


becomes:

MyObject my1 = MyArray[myIndex];
MyOtherObject my2 = my1.myMethod();
MyThirdObject my3 = my2.myProperty;
my3 = 0;



尝试一下-这是一个值得自我发展的技能!



Try it - it is a skill well worth developing yourself!


如果您看到这样的异常,则该问题已90%得以解决.
我曾经看到大量的代码,其中通过编写异常处理程序而不将任何异常重新抛出到堆栈中来抑制了许多异常. (可以在极少数情况下执行此操作;在几乎所有情况下,只有当UI报告了异常时,才可以在每个线程的顶部或在UI周期中抑制异常.)

你还没有做坏事;太好了,保持这种方式!

使用Visual Studio,您只需要在调试器下运行.在99.99%(?)的情况下,Studio会简单地将违反代码的代码直接打到您的脸上.

有一件微妙的事情要知道:调试器异常处理应正确设置:主菜单->调试->异常(Ctrl+D, E).通常,默认值为最佳.

没有Studio,或者更糟的是,问题不在Studio下再现吗?
不用担心:编写异常处理程序可将完整的异常转储写入某个地方(文本文件,系统事件日志).递归地编写完整的异常堆栈和所有内部异常非常重要.
If you see the exception like this, the problem is already 90% solved.
I used to see good amount of code where many exceptions were suppressed by writing exception handlers not re-throwing any exception up to the stack. (This can be done in some quite rare cases; in almost all cases an exception can be only suppressed on the very top of each thread or in the UI cycle when and only when it is reported by the UI.)

You have not done bad thing; great, keep this way!

With Visual Studio you simply need to run under debugger. In 99.99% (?) cases the Studio will simply slap the offending line of the code right into your face.

There is one delicate thing to know: Debugger exception handling should be set up properly: main menu -> Debug -> Exceptions (Ctrl+D, E). Usually, the default is optimal.

No Studio, or, worse, the problem is not reproduces under Studio?
Not to worry: write exception handler to write a full dump of exception somewhere (text file, system event log). It''s important to write full exception stack and all inner exceptions, recursively.


这篇关于数组,循环和碰撞问题! C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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