2010-09-24 70 views
1

我的代碼中插入數據如下它的工作原理是第一次,如果我試圖插入第二次數據我收到錯誤運行時錯誤被宣佈靜態

我的完整代碼

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

發生異常的線是什麼? – 2010-09-24 14:29:02

+0

錯誤發生在哪一行? – egrunin 2010-09-24 14:29:52

+0

它在代碼中的評論「在這一行我得到錯誤」 – 2010-09-24 14:30:37

回答

0

我沒有看到它在這個代碼,所以它可能在其他地方發生;但是因爲你從來沒有真正清除過sb,所以你最好還是先在sb = new StringBuilder();之前。

是的,我知道這是一個黑客。

編輯

這是從OP的後續評論明確表示,這個問題是外部顯示的代碼。 @Kirk Woll提出的調試問題的建議是一個好的開始,但考慮到OP的反應,我懷疑某種結構性問題。

+0

是的,這是一個黑客。這是非常溫和的。 – 2010-09-24 14:36:30

+1

問題是,在第二次調用之前Append.sb被設置爲null;因爲它是一個公共成員,所以可以在你的代碼中的任何地方。 – 2010-09-24 14:40:48

2

因爲很顯然,「SB」是空的,你不知道爲什麼,你可以做最簡單的事情是改變一個屬性,並設置在二傳手斷點等待空賦值:

public static class Append 
{ 
    .... 
    private static StringBuilder _sb = new StringBuilder(); 
    public static StringBuilder sb { 
     get { return _sb; } 
     set { 
      _sb = value; 
     } 
    } 
    .... 
} 

放就行了斷點,_sb = value;,看看當/如果它曾經被炒魷魚。當它發生時,看看它是否設置爲空。如果是,請檢查該堆棧以識別違規者。還要在「_sb = new StringBuilder();」上放置一個斷點看看什麼時候會被解僱。可以想象的是,在之後,您的方法saveEntry被燒掉了

+0

即使它顯示相同的錯誤 – Dotnet 2010-09-24 14:47:03

+0

@Dorababu,你是什麼意思?你試過我的建議嗎?它是否停止在任何一個斷點? – 2010-09-24 15:07:53

+0

在保存條目本身,它正在停止並引發錯誤 – Dotnet 2010-09-24 15:13:48