我有一些代码,当它执行时,它抛出一个NullReferenceException,说:
你调用的对象是空的.
这是什么意思,我该怎么做才能解决这个错误?
我有一些代码,当它执行时,它抛出一个IOException,说
该进程无法访问文件'filename',因为它正由另一个进程使用
这是什么意思,我能做些什么呢?
我知道这个问题是什么,但我对我的程序如何输出一个超出数组的值感到困惑.
我有一个0到8的整数,这意味着它可以保持9个整数,对吗?我有一个int,检查以确保用户输入值是1-9.我从整数中删除一个(像这样)
if (posStatus[intUsersInput-1] == 0) //if pos is empty
{
posStatus[intUsersInput-1] += 1;
}//set it to 1
Run Code Online (Sandbox Code Playgroud)
然后我自己输入9并得到错误.它应该访问数组中的最后一个int,所以我不明白为什么我会收到错误.相关代码:
public int[] posStatus;
public UsersInput()
{
this.posStatus = new int[8];
}
int intUsersInput = 0; //this gets try parsed + validated that it's 1-9
if (posStatus[intUsersInput-1] == 0) //if i input 9 it should go to 8?
{
posStatus[intUsersInput-1] += 1; //set it to 1
}
Run Code Online (Sandbox Code Playgroud)
错误:
"Index was outside the bounds of the array." "Index was outside the bounds of …Run Code Online (Sandbox Code Playgroud) 我收到以下错误之一:
它是什么意思,我该如何解决?
我的程序中有两个列表,列表的长度不相等。我想逐个元素减去这两个列表的成员并将它们保存在一个列表中。
List<double> arrivals = new List<double>();
List<double> departure = new List<double>();
List<double> delays = new List<double>();
double departure_p = 0.8;
double arrival_p = 0.45;
int slot = 0;
while (slot < 1000)
{
Random a = new Random();
double arrival_t = a.NextDouble();
Random d = new Random();
double departure_t = d.NextDouble();
if (departure_t <= departure_p)
{
departure.Add(departure_t);
}
if (arrival_t <= arrival_p)
{
arrivals.Add(arrival_t);
}
slot++;
}
Run Code Online (Sandbox Code Playgroud)
当我使用这个方法时,我遇到了异常 system.IndexOutOfRangeException。
for (int i = 0; i <= arrivals.Count; i++)
{
delays.Add(departure[i] …Run Code Online (Sandbox Code Playgroud) 我正在开发一个ATM软件作为家庭作业,在其中我想知道今天要处理的交易总额,为此,我编写了以下代码
public decimal getDayTransaction(int accountid, string date, string transactiontype)
{
decimal totalamount = 0;
int i = 0;
string connectionString =
"Persist Security Info=False;User ID=sa; Password=123;Initial Catalog=ATMSoftware;Server=Bilal-PC";
try
{
using (SqlConnection connection =
new SqlConnection(connectionString))
{
SqlCommand command = new SqlCommand(
"Select Amount From [Transaction] where AccountID = "
+ accountid + " AND CurrDate ='" + date
+ "' AND TransactionType = '"
+ transactiontype + "';", connection);
connection.Open();
SqlDataReader dr = command.ExecuteReader();
while (dr.Read())
{
totalamount += Convert.ToDecimal(dr.GetString(i)); …Run Code Online (Sandbox Code Playgroud) 我正在运行一个Windows表单程序,它在另一个线程上完成所有工作.一段时间后,UI冻结并停止响应.后台线程仍然正常工作(我可以从完成的工作中看到).
我有这个例外:
exe中类型为"System.IndexOutOfRangeException"的第一次机会异常
当我追踪异常线并再次运行时,我得到了这个:
线程
<No Name>(0x19b4)已退出,代码为0(0x0).
代码行运行正常,但它给出了一个System.IndexOutofRangeException.
MatchCollection tempcollection = Regex.Matches(document,
"(?<data>More information from(.|\\r|\\n)*?</div>)");
if (tempcollection.Count == 0)
{
return Result;
}
string ThisDiv = tempcollection[0].Groups["data"].Value;
// The above line shows exception in Output Window,
// otherwise it works fine and moves to next line.
Run Code Online (Sandbox Code Playgroud)
更新:我已经看到输出提供有关每个异常的信息,无论它是否被捕获,我认为这是冻结UI的原因,但事实并非如此.
我有一个 Excel 模板,它有一个表格(范围:A4:AM5),我需要通过 EPPlus 插入行来扩展它。我的代码在文件中插入单元格值没有问题,但我需要根据第一列中插入的每个新值扩展表的行。
我尝试使用该InsertRow(5,1)方法,但它显示异常
“System.ArgumentOutOfRangeException:'行不能小于 1。参数名称:值'”
价值呈现:
{System.ArgumentOutOfRangeException: Row 不能小于 1。参数名称:值 > 在 OfficeOpenXml.ExcelCellAddress.set_Row(Int32 value) > 在 OfficeOpenXml.ExcelNamedRangeCollection.InsertRows(Int32 rowFrom, Int32 rows, ExcelNamedRange namedOpenRange.> at OfficeOpenXml.ExcelNamedRangeCollection.InsertRows) > at OfficeOpenXml.ExcelNamedRangeCollection.InsertRows Insert(Int32 rowFrom, Int32 colFrom, Int32 rows, Int32 cols, Func`2 filter) > at OfficeOpenXml.ExcelWorksheet.InsertRow(Int32 rowFrom, Int32 rows, Int32 copyStylesFromRow) > at TestInsertRow.Program.Main(String[] args)}
我将代码的功能最小化到这个也显示相同的异常:
class Dnp3
{
private static ExcelPackage _dnp3Package;
private static ExcelWorksheet _worksheet1;
private static FileInfo _templateInfo;
private static FileInfo _newDnp3FileInfo;
public static bool TempFile
{
get …Run Code Online (Sandbox Code Playgroud) 我正在尝试创建一个C#for循环,它将循环遍历项目数组并打印用户的第一个日期.下面是数组的代码(我认为是正确的).
string[,] arrHolidays = new string[2, 3];
arrHolidays[0, 0] = "10/05/2015";
arrHolidays[0, 1] = "15/05/2015";
arrHolidays[0, 2] = "Danny";
arrHolidays[1, 0] = "20/05/2015";
arrHolidays[1, 1] = "22/05/2015";
arrHolidays[1, 2] = "Kieran";
Run Code Online (Sandbox Code Playgroud)
下面是For循环的代码(抛出错误)
for(int i = 0; i < arrHolidays.Length; i++)
{
Console.WriteLine(arrHolidays[i, 0]);
}
Run Code Online (Sandbox Code Playgroud)
索引超出范围异常未得到处理.这是我收到的错误.当它抛出错误并且我检查值1时它是2,如果这是解决它的任何帮助.我希望在控制台应用程序中看到的是:
10/05/2015
20/05/2015
INB4我几乎没有任何c#经验这是我的第一个项目,所以请解释任何帮助:)
我用来DataGrid显示自定义集合(只是通过书中PersonCollection : List<Person>, INotifyCollectionChanged痛苦的示例来学习 WPF )。如果我将项目添加到最初为空的集合中,以及当我从使用多个项目创建的集合中删除项目时,该程序将起作用。在这些情况下DataGrid会正确更新。
但是,有一种情况会导致 ArgumentOutOfRangeException 异常:
DataGrid到非空集合。在这种情况下我得到这个例外:
System.ArgumentOutOfRangeException
HResult=0x80131502
Message=Specified argument was out of the range of valid values.
Parameter name: index
Source=PresentationFramework
StackTrace:
at System.Windows.Controls.ItemCollection.GetItemAt(Int32 index)
at System.Windows.Controls.VirtualizedCellInfoCollection.Contains(DataGridCell cell)
at System.Windows.Controls.DataGridCell.PrepareCell(Object item, DataGridRow ownerRow, Int32 index)
at System.Windows.Controls.Primitives.DataGridCellsPresenter.PrepareContainerForItemOverride(DependencyObject element, Object item)
at System.Windows.Controls.ItemsControl.MS.Internal.Controls.IGeneratorHost.PrepareItemContainer(DependencyObject container, Object item)
at System.Windows.Controls.ItemContainerGenerator.System.Windows.Controls.Primitives.IItemContainerGenerator.PrepareItemContainer(DependencyObject container)
at System.Windows.Controls.DataGridCellsPanel.InsertContainer(Int32 childIndex, UIElement container, Boolean isRecycled)
at System.Windows.Controls.DataGridCellsPanel.AddContainerFromGenerator(Int32 childIndex, UIElement child, Boolean newlyRealized)
at …Run Code Online (Sandbox Code Playgroud)