Runtime error in string builder which was declared static
My code to insert data is as follows it works for the first time if i tried to insert data for the second time i am getting the error
My complete code
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.Subst开发者_如何学运维ring(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;
}
}
Stack Trace
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()
Since it is clear that "sb" is null, and you do not know why, the easiest thing you can do is change it to a property and set a breakpoint in the setter waiting for a null assignment:
public static class Append
{
....
private static StringBuilder _sb = new StringBuilder();
public static StringBuilder sb {
get { return _sb; }
set {
_sb = value;
}
}
....
}
Put a breakpoint on the line, _sb = value;
and see when/if it ever gets fired. When it does, see if it's getting set to null. If it is, examine the callstack to identify the offender. Also put a breakpoint on "_sb = new StringBuilder();" and see when that gets fired. It's conceivable that it is getting fired after your method saveEntry
.
I don't see it in this code, so it's probably happening elsewhere; but since you never actually clear sb
, you might as well precede the line in question with sb = new StringBuilder();
anyway.
Yes, I know it's a hack.
Edit
It's clear from the OP's followup comments that this problem is external to the code shown. @Kirk Woll's suggestion for debugging the problem is a good start, but given the OP's response I suspect a structural problem of some kind.
精彩评论