btnImport()
{
GetVideoLists();
}
public static void GetVideoLists(string listFile)
{
videoList = new List<videoInfo>();
string[] videoArr = File.ReadAllLines(listFile);
foreach (string videoInfo in videoArr)
{
string[] info = videoInfo.Split(new char[] { ',' });
// here i get error
videoInfo tempInfo = new videoInfo(info[0].Trim(), info[1].Trim());
if (CheckVideoUrl(tempInfo.videoUrl))
{
videoList.Add(tempInfo);
}
}
}
-4
A
回答
4
的问题是,你认为每线在你的文件中有至少一个逗号:
string[] info = videoInfo.Split(new char[] { ',' });
videoInfo tempInfo = new videoInfo(info[0].Trim(), info[1].Trim());
如果在videoInfo
没有逗号这将失败。为了解决这个问题,使用方法:
string[] info = videoInfo.Split(new char[] { ',' });
if (info.Length >= 1)
{
videoInfo tempInfo = new videoInfo(info[0].Trim(), info[1].Trim());
...
}
或者
string[] info = videoInfo.Split(new char[] { ',' });
if (info.Length < 1)
continue;
videoInfo tempInfo = new videoInfo(info[0].Trim(), info[1].Trim());
...
+0
thx ..求助。我没有清楚地检查它。 –
3
您正在用逗号分割,并且该字符串可能不包含逗号,因此info [1]不在此处。
向我们展示videoinfo的价值。如果它包含说"test, test2"
那么它会没事的。当你用逗号分割时,你会得到位置[0]和[1]。
相关问题
- 1. 索引超出了数组的范围
- 2. 索引超出了数组的范围
- 3. 索引超出了数组的范围
- 4. 索引超出了数组的范围
- 5. 索引超出了数组的范围。但索引是在范围
- 6. 多维数组:“索引超出了数组的范围。”
- 7. 索引超出数组的范围
- 8. 索引超出数组的范围
- 9. 数组的索引超出范围
- 10. C#System.IndexOutOfRangeException:索引超出数组范围
- 11. Swift数组索引超出范围
- 12. 索引超出数组范围VB
- 13. 索引超出数组范围
- 14. Unity MeshRenderer.materials数组索引超出范围
- 15. Java数组索引超出范围?
- 16. numpy数组索引说超出范围
- 17. System.IndexOutOfRangeException:索引超出数组范围
- 18. 数组索引超出范围
- 19. UICollectionView数组索引超出范围
- 20. 数组索引超出范围?
- 21. 数组索引超出范围刷新
- 22. 索引数组超出范围异常
- 23. java数组索引超出范围
- 24. 数组索引超出范围
- 25. java.lang.ArrayIndexOutOfBoundsException:数组索引超出范围:0
- 26. Java数组索引超出范围
- 27. 数组索引超出范围:30(Java)
- 28. 数组列表索引超出范围
- 29. 数组索引超出范围异常
- 30. 数组查询,索引超出范围
确定videoArr的每一行都有一个值? –
[欢迎使用stackoverflow,请考虑阅读** SO Tour **。它会引导你如何最好地使用这个网站](http://stackoverflow.com/about) – Prix
可能只是一个videoArr的项目,它不包含','所以'info [1]'不存在。 –