我得到一个'对象实例'异常 [英] I get an 'object instance' exception

查看:99
本文介绍了我得到一个'对象实例'异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的代码中,我在第'int fileLength1 = PhotoUpload.PostedFile.ContentLength'行获得了一个例外,比如'使用new关键字创建一个对象实例'。如何解决

呢?



In the following code I get an exception like 'Use the new keyword to create an object instance' at line 'int fileLength1 = PhotoUpload.PostedFile.ContentLength'. How to solve
it?

string fileName = ImagePreview.ImageUrl;
      int fileLength1 = PhotoUpload.PostedFile.ContentLength;
      byte[] imageBytes = new byte[fileLength1];
      PhotoUpload.PostedFile.InputStream.Read(imageBytes, 0, fileLength1);

推荐答案

从那个片段开始,我们无法真正说出来。

所以你需要先看看调试器发生了什么。

在方法的开头放置一个断点,然后查看各种变量。可能是PhotoUpload为null,或者是PostedFile属性。它们为什么会依赖于您在其他地方的代码,我们无法访问。但是当你知道哪个,你可以开始查看它应该设置的位置,这应该告诉你它为什么没有。



对不起,但没有让你的代码在你的系统中运行并完全按照你所做的去做那里我真的没有别的办法。
From just that fragment, we can't really tell.
So you need to start by looking at what's going on with the debugger.
Put a breakpoint at the start of the method and step through looking at the various variables. The chances are that either PhotoUpload is null, or it's PostedFile property is. Why they are will depend on your code elsewhere, which we don;t have access to. But when you know which, you can start looking at where it should be set, and that should tell you about why it hasn't been.

Sorry, but without having your code running in your system and doing exactly what you did to get that far there really isn't anything else we can do.


我的猜测是没有文件。您需要检查文件是否已上载,然后才能访问PostedFile属性。修改你的代码如下:

My guess is there is no file. You need to check if a file was uploaded, only then access the PostedFile property. Modify your code like this:
if(PhotoUpload.HasFile)     
{
      int fileLength1 = PhotoUpload.PostedFile.ContentLength;
      byte[] imageBytes = new byte[fileLength1];
      PhotoUpload.PostedFile.InputStream.Read(imageBytes, 0, fileLength1);
}


在引用之前检查对象是否引用了PostedFile和ContentLength。
Check if the object references for PostedFile and ContentLength both exist before you reference them.


这篇关于我得到一个'对象实例'异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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