如何解决这个异常 [英] How to solve this exception

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

问题描述



我使用以下代码从日志文件中读取数据,当使用lineWords [2] .EndsWith(x)时,我想将lineWords [0]保存到字符串[ ]当我调用这个方法时,我想要返回lineWords [0]和lineWords [2]





  public   static   string  []单词( string 文件)
{
string [] fileContents = ;
string [] words = null ;

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

if (fileContents == null
throw new 例外( < span class =code-string>未找到输入日志文件或格式不正确
);

// 处理数据日志文件的每一行并过滤掉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))
{
int i = 1 ;
CANMsgId = lineWords [ 2 ]。TrimEnd(' X');
words [i] = lineWords [ 0 ];
i ++;
}

}



返回字;


}





我正在尝试上面的一些事情,但我得到一些例外在这一行

 words [i] = lineWords [ 0 ]; 





任何人都可以帮我解决这个问题





谢谢

John

解决方案

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



  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 例外( < span class =code-string>未找到输入日志文件或格式不正确);

// 处理数据日志文件的每一行并过滤掉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.


这篇关于如何解决这个异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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