2017-09-01 60 views
-1

我試圖從文本框中捕獲值輸入(是的,我已經驗證它有一個niput並且不爲空)。我有一個類變量聲明,以確保我與正確的形式相關聯。然而,我的語法似乎從來沒有賦值,它擊中分配行,然後跳起來PrintPreviewDlg.ShowDialog();並引發從Windows窗體中捕獲文本框值

類型「System.ArgumentNullException」的未處理的異常出現在System.Windows.Forms.dll中的錯誤
附加信息:值不能爲空。

這是我的語法

public static LiveBait MainInstance { get; set; } 

private void btnPreview_Click(object sender, EventArgs e) 
{ 
    PrintPreviewDialog PrintPreviewDlg = new PrintPreviewDialog(); 
    PrintPreviewDlg.ClientSize = new System.Drawing.Size(400, 300); 
    PrintPreviewDlg.Location = new System.Drawing.Point(29, 29); 
    PrintPreviewDlg.Name = "PrintPreviewDlg"; 
    PrintPreviewDlg.MinimumSize = new System.Drawing.Size(375, 250); 

    PrintPreviewDlg.WindowState = FormWindowState.Maximized; 
    PrintPreviewDlg.UseAntiAlias = true; 

    dynamic PD = CreatePrintDocument(); 
    PD.DefaultPageSettings.Landscape = true; 
    PrintPreviewDlg.Document = PD; 

    PrintPreviewDlg.ShowDialog(); 
} 

PD CreatePrintDocument() 
{ 
    PD document = new PD(); 
    document.SetParentCtrl(this); 
    document.PrintData.MagRead = MainInstance.txtMagazineRead.Text; 

    //More captures here 

    return document; 
} 

class PraDa 
{ 
    public string MagRead; 
} 

爲什麼我從來不變量被設置什麼,我必須爲了做,以確保它被設置使得BTN點擊事件不會出錯?

EDIT
複製到剪貼板產生該堆棧跟蹤

System.ArgumentNullException了未處理的HResult = -2147467261 消息=值不能爲空。 參數名稱:ARGS PARAMNAME = ARGS 源= mscorlib程序 堆棧跟蹤: 在System.String.Format(字符串格式,對象[]參數) 在ProdData.PD.OnPrintPage(PrintPageEventArgs e)如C:\ Users \用戶擁有者\ Dropbox \ ProdData \ PD.cs:line 234 at System.Drawing.Printing.PrintDocument._OnPrintPage(PrintPageEventArgs e) at System.Drawing.Printing.PrintController.PrintLoop(PrintDocument document) at System.Drawing.Printing.PrintController 。打印(PrintDocument文檔) at System.Drawing.Printing.PrintDocument.Print() at System.Windows.Forms.PrintPreviewControl.ComputePreview() at System.Windows.Forms.PrintPreviewControl。 CalculatePageInfo() 在System.Windows.Forms.Control.InvokeMarshaledCallbackDo(ThreadMethodEntry TME) 在System.Windows.Forms.Control.InvokeMarshaledCallbackHelper(obj對象) 在System.Threading.ExecutionContext.RunInternal(ExecutionContext中的ExecutionContext,ContextCallback回調,對象狀態下,布爾preserveSyncCtx) 在System.Threading.ExecutionContext.Run(的ExecutionContext的ExecutionContext,ContextCallback回調,對象的狀態,布爾preserveSyncCtx) 在System.Threading.ExecutionContext.Run(的ExecutionContext的ExecutionContext,ContextCallback回調,在系統對象狀態) 。 Windows.Forms.Control.InvokeMarshaledCallback(ThreadMethodEntry tme) at System.Windows.Forms.Control.InvokeMarshaledCallbacks() at System .Windows.Forms.Control.WndProc(消息&米) 在System.Windows.Forms.PrintPreviewControl.WndProc(消息&米) 在System.Windows.Forms.Control.ControlNativeWindow.OnMessage(消息&米) 在系統.Windows.Forms.Control.ControlNativeWindow.WndProc(消息&米) 在System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr的的HWND,MSG的Int32,IntPtr的WPARAM,IntPtr的LPARAM) 在System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW (MSG & msg) at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager。FPushMessageLoop(IntPtr 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 System.Windows.Forms.Form.ShowDialog() at ProdData.Controls。在C:\ Users \ Owner \ Dropbox \ Drl.cs中的DrawingStageCtrl.btnPreview_Click(Object sender,EventArgs e):System.Windows.Forms.Control.OnClick上的889行 System.Windows.Forms.Button上的EventErgs e) .OnClick(EventArgs e) at System.Windo ws.Forms.Button.OnMouseUp(MouseEventArgs mevent) 在System.Windows.Forms.Control.WmMouseUp(消息&米,MouseButtons按鈕,點擊的Int32) 在System.Windows.Forms.Control.WndProc(消息&米) 在System.Windows.Forms.ButtonBase.WndProc(消息&米) 在System.Windows.Forms.Button.WndProc(消息&米) 在System.Windows.Forms.Control.ControlNativeWindow.OnMessage(消息&米) System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message & m) at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd,Int32 msg,IntPtr wparam,IntPtr lparam) 在System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG & MSG) 在System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr的dwComponentID,的Int32原因,的Int32 pvLoopData) 在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 ProdData.Program.Main()in C:\ Users \ Owner \ Dropbox \ Program.cs:line 18 InnerException:

編輯2
這是我的方法被請求的語法,希望這將闡明問題是什麼。

protected override void OnPrintPage(PrintPageEventArgs e) 
{ 
    base.OnPrintPage(e); 
    int printHeight = 0; 
    int printWidth = 0; 
    int rightMargin = 0; 

    PaperSize ps = default(PaperSize); 
    for (int ix = 0; ix <= PrinterSettings.PaperSizes.Count - 1; ix++) 
    { 
     if (PrinterSettings.PaperSizes[ix].Kind == PaperKind.A4) 
     { 
      ps = PrinterSettings.PaperSizes[ix]; 
      DefaultPageSettings.PaperSize = ps; 
      break; 
     } 
    } 

    DefaultPageSettings.Margins.Top = PAGE_TOP_MARGIN; 
    DefaultPageSettings.Margins.Left = PAGE_LEFT_MARGIN; 
    DefaultPageSettings.Landscape = true; 

    var CurrentPageSettings = base.DefaultPageSettings; 
    printWidth = CurrentPageSettings.PaperSize.Height - CurrentPageSettings.Margins.Top - CurrentPageSettings.Margins.Bottom; 
    printHeight = CurrentPageSettings.PaperSize.Width - CurrentPageSettings.Margins.Left - CurrentPageSettings.Margins.Right; 
    m_leftMargin = CurrentPageSettings.Margins.Left; //X 
    rightMargin = CurrentPageSettings.Margins.Top; //Y 

    //Create a rectangle printing are for our document 
    m_PrintArea = new RectangleF(m_leftMargin, rightMargin, printWidth, printHeight); 

    // Get Normal Row Height 
    int charactersFitted = 0; 
    int linesFilled = 0; 
    SizeF TextSize = new SizeF(); 
    StringFormat textFormat = new StringFormat(); 
    //Tell it to Alignment Text in its rectangle 
    textFormat.Alignment = StringAlignment.Near; 
    textFormat.FormatFlags = StringFormatFlags.NoClip; 
    TextSize = e.Graphics.MeasureString("NORMALROW", SUB_HEADING_FONT, m_PrintArea.Size, textFormat, out charactersFitted, out linesFilled); 
    m_NormalRowHeight = (int)TextSize.Height + 3; //Row is bigger than text 

    //Draw First Heading 
    Rectangle MainHeaderRect = new Rectangle(); 
    DrawMainHeading(e, LogoRect, ref MainHeaderRect); 
    m_iCurrentLocationY = MainHeaderRect.Bottom + (m_NormalRowHeight); 

    int LeftSubHeadingWidth = 200; 
    m_SubHeaderTextFieldSize = new Size(LeftSubHeadingWidth, m_NormalRowHeight); 
    m_TextValuePairSize = new Size(m_SubHeaderTextFieldSize.Width/30, m_SubHeaderTextFieldSize.Height); 

    bool IPN = !string.IsNullOrEmpty(MainInstance.txtPN.Text); 
    bool IPJN = !string.IsNullOrEmpty(MainInstance.txtPJN.Text); 
    bool IDt = !string.IsNullOrEmpty(MainInstance.dateIDt.Text); 
    bool IPB = !string.IsNullOrEmpty(MainInstance.txtIPB.Text); 
    bool ISS = !string.IsNullOrEmpty(MainInstance.txtISS.Text); 

    string ST = !IPN ? String.Format("IPN:: ", MainInstance.txtPN.Text) : String.Format("IPN:: ", null); 
    string pnt = !IPJN ? String.Format(MainInstance.txtPJN.Text) : String.Format(null); 
    string dt = !IDt ? String.Format(MainInstance.dateIDt.Text) : String.Format(null); 
    string pbt = !IPB ? String.Format(MainInstance.txtIPB.Text) : String.Format(null); 
    string ST = !ISS ? String.Format(MainInstance.txtISS.Text) : String.Format(null); 

    m_iCurrentLocationY += (int)(1.5 * m_NormalRowHeight); 
    int iAlphaStart_Y = m_iCurrentLocationY; 

    DrawSubHeading(e, ST, new Rectangle(new Point(m_leftMargin, m_iCurrentLocationY), m_SubHeaderTextFieldSize), StringAlignment.Near); 
    DrawPairsToPage(e, "IPJN: ", pnt, true, m_leftMargin); 
    DrawPairsToPage(e, "Date: ", dt, true, m_leftMargin); 
    DrawPairsToPage(e, "IPB: ", pbt, true, m_leftMargin); 
    DrawPairsToPage(e, "ISS: ", ST, true, m_leftMargin); 

    Panel DrawingArea = m_ParentCtrl.GetDrawingArea(); 
    Bitmap pota = new Bitmap(DrawingArea.Width, DrawingArea.Height); 
    m_ParentCtrl.ShowDetails(true); 
    foreach (Control c in DrawingArea.Controls) 
    { 
     c.BringToFront(); 
    } 
    DrawingArea.DrawToBitmap(pota, new Rectangle(0, 0, DrawingArea.Width, DrawingArea.Height)); 
    foreach (Control c in DrawingArea.Controls) 
    { 
     c.BringToFront(); 
    } 
    m_ParentCtrl.ShowDetails(false); 
    int iDrawingY = iAlphaStart_Y + (5 * m_NormalRowHeight); 
    e.Graphics.DrawImage(pota, new Rectangle(m_leftMargin, iDrawingY, printWidth, printHeight - iDrawingY)); 
} 
} 
+0

什麼是堆棧跟蹤? – SLaks

+0

你在哪裏設置MainInstance –

+0

你可能不應該使用'dynamic'。 – SLaks

回答

1

堆棧跟蹤告訴你確切位置ArgumentNull例外是:

C:\用戶\用戶\ Dropbox的\ ProdData \ PD.cs:行234

而且它也告訴你它正在打電話給String.Format

我的orbuculum讓我相信它是在這一行(格式化爲可讀性):

string ST = !IPN ? 
      String.Format("IPN:: ", MainInstance.txtPN.Text) : 
      String.Format("IPN:: ", null); 

documenation也列出了這些例外情況。

該行有幾個錯誤。

首先String.Format("IPN:: ", null);是你的問題的原因,它也沒有多大意義。爲什麼你要格式化無論如何都是空的東西。只需返回純字符串。

其次String.Format("IPN:: ", MainInstance.txtPN.Text)不會拋出,但會一直輸出IPN::,因爲您忘記在格式字符串中包含{0}以指示參數應該到達的位置。

最後String.Format(null)也會拋出異常。

我建議你介紹一種輔助方法,這樣你就可以替換整個布爾玩花樣

// takes any Control and return an emtpy string if Text is null or empty 
string GetTextOrEmpty(Control ctl) 
{ 
    var txt = ctl.Text; // base implementation of Text doesn't return null 
    if (String.IsNullOrEmpty(txt)) { 
    return String.Empty; 
    } else { 
    return txt; 
    } 
} 

的東西更乾淨是這樣的:

string ST = String.Format("IPN:: {0}", GetTextOrEmpty(MainInstance.txtPN)); 
string pnt = GetTextOrEmpty(MainInstance.txtPJN.Text); 
string dt = GetTextOrEmpty(MainInstance.dateIDt.Text); 
string pbt = GetTextOrEmpty(MainInstance.txtIPB.Text); 
string ST = GetTextOrEmpty((MainInstance.txtISS.Text); 

,將解決你的ArgumentNull異常的參數指定參數和以及參數格式,但你仍然必須遇到的那個。