我插入数据的代码如下所示,如果我第二次尝试插入数据时,它第一次起作用我收到错误
我的完整代码
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
namespace ACHDAL
{
public class EntryDetail
{
int[] debits ={ 25, 26, 27, 28, 29, 35, 36, 37, 38, 39, 46, 47, 48, 48, 49, 55, 56, 81, 84, 86, 88 };
int[] credits ={ 20, 21, 22, 23, 24, 30, 31, 32, 33, 34, 40, 41, 42, 43, 44, 45, 50, 51, 52, 53, 54, 82, 83, 85, 87 };
private string m_strRecordTypeCode;
private string m_strTransactionCode;
private string m_strRecievingDFIIdentification;
private string m_strCheckDigit;
private string m_strDFIAccountNumber;
private string m_strAmount;
private string m_strIdentificationNumber;
private string m_strRecievingcompanyName;
private string m_strDiscretionaryData;
private string m_strAddendaRecordIndicator;
private string m_strTraceNumber;
private static string m_path = string.Empty;
private bool m_flag = false;
public string RecordTypeCode
{
get { return m_strRecordTypeCode; }
set { m_strRecordTypeCode = value; }
}
public string TransactioCode
{
get { return m_strTransactionCode; }
set { m_strTransactionCode = value; }
}
public string RecievingDFIIdentification
{
get { return m_strRecievingDFIIdentification; }
set { m_strRecievingDFIIdentification = value; }
}
public string CheckDigit
{
get { return m_strCheckDigit; }
set { m_strCheckDigit = value; }
}
public string DFIAccountNumber
{
get { return m_strDFIAccountNumber; }
set { m_strDFIAccountNumber = value; }
}
public string Amount
{
get { return m_strAmount; }
set { m_strAmount = value; }
}
public string IdentificationNumber
{
get { return m_strIdentificationNumber; }
set { m_strIdentificationNumber = value; }
}
public string RecievingCompanyName
{
get { return m_strRecievingcompanyName; }
set { m_strRecievingcompanyName = value; }
}
public string DiscretionaryData
{
get { return m_strDiscretionaryData; }
set { m_strDiscretionaryData = value; }
}
public string AddendaRecordIndicator
{
get { return m_strAddendaRecordIndicator; }
set { m_strAddendaRecordIndicator = value; }
}
public string TraceNumber
{
get { return m_strTraceNumber; }
set { m_strTraceNumber = value; }
}
public bool addEntrydetails()
{
this.m_flag = false;
if (m_strRecordTypeCode != string.Empty && m_strTransactionCode != string.Empty && m_strRecievingDFIIdentification != string.Empty &&
m_strCheckDigit != string.Empty && m_strDFIAccountNumber != string.Empty && m_strAmount != string.Empty && m_strRecievingcompanyName != string.Empty
&& m_strAddendaRecordIndicator != string.Empty)
{
saveEntry(m_path);
m_flag = true;
}
else
{
m_flag = false;
}
return m_flag;
}
public bool BankRoutingNumberValidation(string sInput)
{
m_flag = false;
bool bRule1 = false;
bool bRule2 = false;
int iResult = 0;
if (sInput.Length == 9)
{
iResult = 3 * Convert.ToInt32(sInput.Substring(0, 1)) +
7 * Convert.ToInt32(sInput.Substring(1, 1)) +
Convert.ToInt32(sInput.Substring(2, 1)) +
3 * Convert.ToInt32(sInput.Substring(3, 1)) +
7 * Convert.ToInt32(sInput.Substring(4, 1)) +
Convert.ToInt32(sInput.Substring(5, 1)) +
3 * Convert.ToInt32(sInput.Substring(6, 1)) +
7 * Convert.ToInt32(sInput.Substring(7, 1)) +
Convert.ToInt32(sInput.Substring(8, 1));
if ((iResult % 10) == 0)
{
bRule1 = true;
iResult = 7 * Convert.ToInt32(sInput.Substring(0, 1)) +
3 * Convert.ToInt32(sInput.Substring(1, 1)) +
9 * Convert.ToInt32(sInput.Substring(2, 1)) +
7 * Convert.ToInt32(sInput.Substring(3, 1)) +
3 * Convert.ToInt32(sInput.Substring(4, 1)) +
9 * Convert.ToInt32(sInput.Substring(5, 1)) +
7 * Convert.ToInt32(sInput.Substring(6, 1)) +
3 * Convert.ToInt32(sInput.Substring(7, 1));
if ((iResult % 10) == Convert.ToInt32(sInput.Substring(8, 1)))
{
bRule2 = true;
}
m_flag = bRule1 & bRule2;
}
}
return m_flag;
}
public bool saveEntry(string strPath)
{
long Amnt = 0;
double AmtforFileControl_debit=0;
double AmtforFileControl_credit=0;
m_flag = true;
Append.sb.AppendLine();
//When i put a break point i am getting the error here Object reference not set to an instance of an object.
Append.sb.Append(m_strRecordTypeCode.PadLeft(1, '0'));
Append.sb.Append(m_strTransactionCode.PadLeft(2, '0'));
Append.sb.Append(m_strRecievingDFIIdentification.PadLeft(9, '0'));
Append.sb.Append(m_strDFIAccountNumber.PadRight(17, ' '));
Append.sb.Append(m_strAmount.PadLeft(10, '0'));
int length = debits.Length;
for (int j = 0; j < length; j++)
{
if (m_strTransactionCode == debits[j].ToString())
{
Amnt = Convert.ToInt64(m_strAmount);
if(Append.oldbatchcontrol!=string.Empty)
{
AmtforFileControl_debit=Convert.ToDouble(m_strAmount);
}
Append.debitAmnt += Amnt;
break;
}
}
int CreditLength = credits.Length;
for (int k = 0; k < CreditLength; k++)
{
if (m_strTransactionCode == credits[k].ToString())
{
Amnt = Convert.ToInt64(m_strAmount);
if(Append.oldbatchcontrol!=string.Empty)
{
AmtforFileControl_credit=Convert.ToDouble(m_strAmount);
}
Append.creditAmnt += Amnt;
break;
}
}
Append.sb.Append(m_strIdentificationNumber.PadRight(15, ' '));
Append.EntryHash += Convert.ToDouble(m_strRecievingDFIIdentification);
Append.sb.Append(m_strRecievingcompanyName.PadRight(22, ' '));
Append.sb.Append(m_strDiscretionaryData.PadRight(2, ' '));
Append.sb.Append(m_strAddendaRecordIndicator.PadLeft(1, '0'));
Append.sb.Append(m_strTraceNumber.PadLeft(15,'0'));
//Entry Count for EntryDetail
Append.Entrycnt++;
if (Append.oldbatchcontrol != string.Empty)
{
//Append.sb.AppendLine();
string strRecordtype = "8";
string strServcclasscode = Append.StandEntryCode.PadLeft(3, '0');
//m_strServiceClassCode.PadLeft(3, '0').ToString();
string Entrycnt = Append.Entrycnt.ToString().PadLeft(6, '0');
filecontrolvariables.entrycount++;
string EntryHash = Append.EntryHash.ToString().PadLeft(10, '0');
filecontrolvariables.Entryhash += Convert.ToDouble(m_strRecievingDFIIdentification);
string debitAmnt = Append.debitAmnt.ToString().PadLeft(12, '0');
filecontrolvariables.debitamt += AmtforFileControl_debit;
string creditAmnt = Append.creditAmnt.ToString().PadLeft(12, '0');
filecontrolvariables.creditamt = AmtforFileControl_credit;
string CompIdentification = Append.Companyidentification.PadRight(10, ' ');
//m_strCompanyIdentification.PadRight(10, ' ');
string MessageAuthenticationCode = "".PadRight(19, (char)32).ToString();
string Reserved = "".PadRight(6, (char)32).ToString();
string Odfi = Append.OriginalOdfi.PadLeft(8, '0');
//m_strOriginationDFIIdentification.PadLeft(8, '0');
string batchno = Append.Batchnum_edit.ToString().PadLeft(7, '0');
//m_strBatchNumber.PadLeft(7, '0');
Append.sb.AppendLine();
Append.sb.Append(strRecordtype);
Append.sb.Append(strServcclasscode);
Append.sb.Append(Entrycnt);
Append.sb.Append(EntryHash);
Append.sb.Append(debitAmnt);
Append.sb.Append(creditAmnt);
Append.sb.Append(CompIdentification);
Append.sb.Append(MessageAuthenticationCode);
Append.sb.Append(Reserved);
Append.sb.Append(Odfi);
Append.sb.Append(batchno);
StreamReader forAddEntry = new StreamReader(strPath);
string Filedata = string.Empty;
while (Filedata == forAddEntry.ReadToEnd())
{
Filedata.Replace(Append.oldbatchcontrol, Append.sb.ToString());
}
forAddEntry.Close();
StreamWriter sw = new StreamWriter(strPath);
sw.Write(Filedata);
sw.Close();
}
//Append.Batchcnt++;
return m_flag;
}
}
public static class Append
{
public static string OriginalOdfi=string.Empty;
public static string oldbatchcontrol=string.Empty;
public static StringBuilder sb = new StringBuilder();
public static StringBuilder _sb
{
get { return sb; }
set
{
sb = value;
}
}
public static int Batchcnt = 0;
public static int Batchnum_edit=0;
public static int Traceno = 0;
public static int Entrycnt = 0;
public static double EntryHash = 0;
public static long debitAmnt = 0;
public static long creditAmnt = 0;
public static int Seccode = 0;
public static string Companyidentification = string.Empty;
public static string StandEntryCode = string.Empty;
}
}
堆栈跟踪
at ACHDAL.EntryDetail.saveEntry(String strPath) in D:\ACHWINAPPLICATION\ACHDAL\EntryDetail.cs:line 164
at ACHWINAPPLICATION.frmEntryDetails.btnSave_Click(Object sender, EventArgs e) in D:\ACHWINAPPLICATION\ACHWINAPPLICATION\frmEntryDetails.cs:line 114
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods. IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.RunDialog(Form form)
at System.Windows.Forms.Form.ShowDialog(IWin32Window owner)
at ACHWINAPPLICATION.frmMain.addEntry_Click(Object sender, EventArgs e) in D:\ACHWINAPPLICATION\ACHWINAPPLICATION\frmMain.cs:line 553
at System.Windows.Forms.ToolStripItem.RaiseEvent(Object key, EventArgs e)
at System.Windows.Forms.ToolStripMenuItem.OnClick(EventArgs e)
at System.Windows.Forms.ToolStripItem.HandleClick(EventArgs e)
at System.Windows.Forms.ToolStripItem.HandleMouseUp(MouseEventArgs e)
at System.Windows.Forms.ToolStripItem.FireEventInteractive(EventArgs e, ToolStripItemEventType met)
at System.Windows.Forms.ToolStripItem.FireEvent(EventArgs e, ToolStripItemEventType met)
at System.Windows.Forms.ToolStrip.OnMouseUp(MouseEventArgs mea)
at System.Windows.Forms.ToolStripDropDown.OnMouseUp(MouseEventArgs mea)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
at System.Windows.Forms.ToolStrip.WndProc(Message& m)
at System.Windows.Forms.ToolStripDropDown.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at ACHWINAPPLICATION.Program.Main() in D:\ACHWINAPPLICATION\ACHWINAPPLICATION\Program.cs:line 20
at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
答案 0 :(得分:2)
由于很明显“sb”为空,并且您不知道原因,您可以做的最简单的事情是将其更改为属性并在setter中设置断点,等待空赋值:
public static class Append
{
....
private static StringBuilder _sb = new StringBuilder();
public static StringBuilder sb {
get { return _sb; }
set {
_sb = value;
}
}
....
}
在行_sb = value;
上放置一个断点,然后查看它是否被触发。如果是,请查看它是否设置为null。如果是,请检查callstack以识别违规者。还在“_sb = new StringBuilder();”上加了一个断点。并看看何时被解雇。可以想象,在你的方法saveEntry
之后,被解雇了。
答案 1 :(得分:0)
我在这段代码中没有看到它,所以它可能正在其他地方发生;但是,由于您实际上从未真正清除sb
,因此无论如何您也可以在sb = new StringBuilder();
之前的相关行之前。
是的,我知道这是一个黑客攻击。
修改强>
从OP的后续评论中可以清楚地看出,此问题在所示代码的外部。 @Kirk Woll关于调试问题的建议是一个良好的开端,但考虑到OP的反应,我怀疑存在某种结构性问题。