• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

值一一设置为字符串数组时,出现“空引用异常"(2)

用户头像
it1352
帮助1

问题说明

亲爱的朋友,

首先,我要感谢大家花时间阅读并友好回答我的问题.建议我在定义时设置数组大小".

我理解它,但事实是,在定义时,我不知道它可能需要的大小.如前所述,我必须定义文件名"
这是button1_Click事件外部的字符串数组(由于某些原因).但是在这种情况下,它的大小将变得清晰.所以我不知道.

换句话说,当我需要在定义时定义一个大小未知的数组以免引发此类错误时该怎么办?

在下面,为方便起见,我重复了我的问题

*************

你好,

在程序中,用户可以选择一些文件进行某些操作.在"OpenFileDialog"中,可以返回文件的完整路径,目录路径和文件名.我需要的是将文件路径与其后面的文件名分开.为此,我需要将值一个一个地设置为字符串数组.我使用了下面的代码,但有如下错误:

空引用异常未处理
对象引用未设置为对象的实例.

你能帮我吗?
非常感谢


Dear Friends,

I should First thank you all for taking time reading and kindly answering my question. It was suggested that I should set the Array Size at the time of definition.

I understand it but the fact is, at the time of definition, I am not aware of its probable size needed. As it was pointed I have to define the ‘FileNames’
which is an Array of string outside of button1_Click event (due to some reasons). But within this event the size of it will become clear. So I don’t know it in advance.

In other words, what should I do when I need to define an array with unknown size at the time of definition so that it won’t raise such error?

In bellow my question is repeated for convenience

*************

Hello,

In a program the user may choose some files to have some operation on. In ‘OpenFileDialog’, Full path of files, both path of directory and File names could be returned. What I require is to separate File path from its following File name. To do so, I needed to set value to Array of string one by one. I used the code below but there is a error as follows:

Null Reference Exception was unhandled
Object reference not set to an instance of an object.

Could you please help me?
Thanks a lot


public Form1()
        {
            InitializeComponent();
        }
 
 string[] FilePathNames;
        string[] FileNames; // I need to define here, instead of defining inside ''button1_Click'' method 

        private void button1_Click(object sender, EventArgs e)
        {
 
           ... 
 

            if (myOpenFileDialog.ShowDialog() == DialogResult.OK)
            {
                 FilePathNames = myOpenFileDialog.FileNames;
                //FilePathNames, includes Full path of files, both path of                                directory and File names
                                            
                int counter = 0;
 
                // in loop below, i want to detach File path from its following File name
                foreach (string fname in FilePathNames)  
                {
                    int idx = fname.LastIndexOf(''\\'');
                    FileNames[counter] = fname.Substring(idx   1); // Here, Error Ocuurs!!!
                    counter  = 1;
                }
            }
 
        }

正确答案

#1
一旦您知道从OpenFileDialog中获取了多少文件,就重新初始化数组是个好主意...除此之外,您可能想要查看使用列表.您无需知道要添加多少个对象即可使用它们.

(如果在此末尾有几个标签...我发布后不知道它们为什么在那里,请忽略它们.)

Re-initializing the array once you know how many files you are getting from the OpenFileDialog is a good idea... beyond that, you may want to look into using a List. You do not need to know how many objects you are going to add to use them.

(if there is a couple tags at the end of this... I don''t know why they''re there after I post, ignore them.)

List<string> fileList = new List<string>();

// in loop below, i want to detach File path from its following File name
                foreach (string fname in FilePathNames)
                {
                    int idx = fname.LastIndexOf(''\\'');
                    fileList.Add(fname.Substring(idx 1));
                }
</string></string>
将其添加到foreach之前
Add this just before the foreach
string[] tempFileNames = new string[myOpenFileDialog.FileNames.Length];


并复制到ur变量(可以进行浅拷贝,深拷贝)


and copy to ur variable ( you can shallowcopy,deep copy)

FileNames = tempFileNames; // just template
如果openFileDialog返回null值,则此错误occrod

尝试在您的代码中进行错误处理

和第二个错误(对象引用未设置为对象的实例):
定义此类型的变量
if openFileDialog return a null value this error occrod

try error handling in your code

and second error (Object reference not set to an instance of an object):
define variable this type
string[] FileNames=new string[300];



希望有帮助



hope this help

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /reply/detail/tanhcfgaac
系列文章
更多 icon
同类精品
更多 icon
继续加载