且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何解决这个异常

更新时间:2022-10-29 14:32:54

您应该使用列表而不是数组:



  public   static   string  [] words( string  file)
{
string [] fileContents = null ;
List< string> words = new List< string>();

尝试
{
// 打开并读取整个数据日志文件内容。
fileContents = File.ReadAllLines(file);
}
catch (例外情况)
{
throw new 异常( 输入日志文件不是发现或格式不正确\ n详细信息: + ex.Message);
}

if (fileContents == null
throw new 例外( 未找到输入日志文件或格式不正确);

// 处理数据日志文件的每一行并过滤掉CAN Msg Id的相应内容每个CAN总线。
for int 索引= 0 ;索引< fileContents.Length; Index ++)
{
string CANMsgId = string .Empty;
string [] spaceSeperator = new string [] { };
string [] lineWords =(fileContents [Index] .Trim())。Split(spaceSeperator,StringSplitOptions.RemoveEmptyEntries);
if (lineWords.Length < 2 + 1 ))

继续;

// 如果CAN消息标识有效,则应以x结尾。如果它不以'x'结尾,
// 然后跳过该条目并转到下一行日志文件
if (lineWords [ 2 ]。EndsWith ( x))
{
CANMsgId = lineWords [ 2 ]。TrimEnd(' x');
words.Add(lineWords [ 0 ]);
}

}



return words.ToArray() ;


}





这是因为words数组为空,你永远不会创建它。由于您不知道数组中需要多少条目,因此***使用列表。在您发布的代码中,您总是将第二个元素(索引1)设置为该值,然后递增i,并在下一个循环中重置它,即使您初始化了数组,这实际上也没有。


好吧,让我们从你得到的例外开始?我的猜测是一个空的例外,因为查看你的代码你永远不会实例化单词

就你正在做的任务而言,我不会将单词声明为字符串[]。



我会将单词声明为:

 List< string> words =  new 列表< string>(); < /  字符串 >  < /   string  >  



然后代替单词[i] =我会这样做:

 words.Add(lineWords [ 0 ]); 



最后如果你必须返回一个数组,请执行以下操作:

  return  words.ToArray(); 



哦,我会改变:

 if(lineWords.Length<(2 + 1))

 if(lineWords.Length< 3)


当你不确定有多少元素时如果有可能有删除的情况,那么你应该总是去找一个列表。


Hi,
I am using following code to read the data from log files and when lineWords[2].EndsWith("x"),I want to save lineWords[0] to string[] words and I want return both lineWords[0] and lineWords[2] when the method is called


public static string[] words(string file)
{
                 string[] fileContents = null;
                 string[] words = null;
                
                 try
                 {
                     // Open and read the entire Data Log File contents.
                     fileContents = File.ReadAllLines(file);
                 }
                 catch (Exception ex)
                 {
                     throw new Exception("The Input log file was not found or is not in correct format\nDetails: " + ex.Message);
                 }

                 if (fileContents == null)
                     throw new Exception("The Input log file was not found or is not in correct format");

                 // Process each line of the Data Log File and filter out the CAN Msg Id's corresponding to each CAN Bus.
                 for (int Index = 0; Index < fileContents.Length; Index++)
                 {
                     string CANMsgId = string.Empty;
                     string[] spaceSeperator = new string[] { " " };
                     string[] lineWords = (fileContents[Index].Trim()).Split(spaceSeperator, StringSplitOptions.RemoveEmptyEntries);
                     if (lineWords.Length < (2 + 1))

                         continue;

                     // If a CAN Msg Id is valid, it should end with 'x'. If it doesnot end with 'x',
                     // then skip the entry and go to the next line of log file
                     if (lineWords[2].EndsWith("x"))
                     {
                         int i = 1;
                         CANMsgId = lineWords[2].TrimEnd('x');
                        words[i] = lineWords[0];
                        i++;
                     }
                     
                 }


           
           return words;
                
     
} 



I am trying some thing like above, but I am getting some exception at this line

words[i] = lineWords[0];



Can anyone help me in solving this


Thanks
John

You should use a list instead of an array:

public static string[] words(string file)
{
                 string[] fileContents = null;
                 List<string> words = new List<string>();
                
                 try
                 {
                     // Open and read the entire Data Log File contents.
                     fileContents = File.ReadAllLines(file);
                 }
                 catch (Exception ex)
                 {
                     throw new Exception("The Input log file was not found or is not in correct format\nDetails: " + ex.Message);
                 }
 
                 if (fileContents == null)
                     throw new Exception("The Input log file was not found or is not in correct format");
 
                 // Process each line of the Data Log File and filter out the CAN Msg Id's corresponding to each CAN Bus.
                 for (int Index = 0; Index < fileContents.Length; Index++)
                 {
                     string CANMsgId = string.Empty;
                     string[] spaceSeperator = new string[] { " " };
                     string[] lineWords = (fileContents[Index].Trim()).Split(spaceSeperator, StringSplitOptions.RemoveEmptyEntries);
                     if (lineWords.Length < (2 + 1))
 
                         continue;
 
                     // If a CAN Msg Id is valid, it should end with 'x'. If it doesnot end with 'x',
                     // then skip the entry and go to the next line of log file
                     if (lineWords[2].EndsWith("x"))
                     {
                         CANMsgId = lineWords[2].TrimEnd('x');
                         words.Add(lineWords[0]);
                     }
                     
                 }
 

           
           return words.ToArray();
                
     
}



This is because words array is null and you never create it. Since you don't know how many entries you will need in the array, its better to use a list. In the code you posted, you were always setting the second element (index 1) to the value, then incrementing i, and resetting it on the next loop, this does, effectively, nothing even if you had initialized the array.


Well lets start with what exception are you getting? My guess would be a null exception because looking at your code you never instantiate "words"
Personally for the task you are doing, I wouldn't declare words as a string[].

I would declare words as :
List<string> words = new List<string>();</string></string>


Then instead of words[i] = I would do:

words.Add(lineWords[0]);


Finally if you have to return an array do:

return words.ToArray();


Oh, and I would change:

if (lineWords.Length < (2 + 1))

to

if (lineWords.Length < 3)


When you are not sure how many elements are there and if there are cases that there can be deletions, then you should always go for a LIST.