2014-10-17 76 views
0

命名空間與這兩個類都是相同的。我需要在WhatsAppSend類中調用xyz()方法。無法調用類的方法

這是我在Form1.cs方法

public void SendWhatsAppMsg() 
{ 
try 
{ 
WhatsAppSend ws; 
ws = new WhatsAppSend(this); //Error Coming on this 
ws.xyz(txtNickName.Text.ToString(), 
     gridSender.Rows[iCurrentChannelNo].Cells[0].Value.ToString(), 
     gridSender.Rows[iCurrentChannelNo].Cells[1].Value.ToString(), 
     CreateArrayFrom(i + 1, (i + 1) + iMsgLimitPerNo), 
     dropWhatsappType.SelectedItem.ToString(), 
     SendingData(dropWhatsappType.SelectedItem.ToString()), 
     ext); 
} 
catch(Exception e) 
{ 
txt.log += e.ToString(); 
} 
} 

方法,我想在其他類調用WhatsAppSend.cs

namespace WhatsApp_Bulk 
{ 
    public class WhatsAppSend 
    { 
     public static WhatsAppBulk form1; 
     public WhatsAppSend(WhatsAppBulk _form1) 
      { 
       form1 = _form1; 
      } 
    public static String line = ""; 
    public static String command = ""; 
    public static string[] dst; 
    public static int count_sleep = Convert.ToInt32(form1.txtMsgGap.Text); 
    public static string WhatsAppType = ""; 
    public static string DataToSend = ""; 
    public static string ext = ""; 
public void xyz(string nickname, string sender, string password, string[] Datadst, string DataWhatsAppType, string DataDataToSend, string Dataext) 
     { 
      dst = Datadst; 
      WhatsAppType = DataWhatsAppType; 
      DataToSend = DataDataToSend; 
      ext = Dataext; 
      WhatsApp wa = new WhatsApp(sender, password, nickname, true); 
     } 
    } 
} 

錯誤:

System.TypeInitializationException: The type initializer for 'WhatsApp_Bulk.WhatsAppSend' threw an exception. ---> System.NullReferenceException: Object reference not set to an instance of an object. 
    at WhatsApp_Bulk.WhatsAppSend..cctor() in d:\WhatsApp Bulk\WhatsApp Bulk\WhatsAppSend.cs:line 32 
    --- End of inner exception stack trace --- 
    at WhatsApp_Bulk.WhatsAppSend..ctor(WhatsAppBulk _form1) 
    at WhatsApp_Bulk.WhatsAppBulk.SendWhatsAppMsg() in d:\WhatsApp Bulk\WhatsApp Bulk\Form1.cs:line 367 

請告訴我在哪裏出錯

+0

在哪個行中引發異常 – vallabha 2014-10-17 06:10:42

+1

您的設計是非常有缺陷的。您將來自實例構造函數的'WhatsAppBulk'表單實例設置爲靜態字段('form1')。想想看。 – 2014-10-17 06:16:54

回答

3
public static int count_sleep = Convert.ToInt32(form1.txtMsgGap.Text); 

form1null時,此行執行類型初始化。 您在稍後執行的實例構造函數中設置了form1

這是TypeInitializationException的常見原因。

UPD:

我可以建議以下

public class WhatsAppSend 
{ 
    public WhatsAppBulk form1; 
    public WhatsAppSend(WhatsAppBulk _form1) 
    { 
     form1 = _form1; 
     count_sleep = Convert.ToInt32(form1.txtMsgGap.Text); 
    } 
    public String line = ""; 
    public String command = ""; 
    public string[] dst; 
    public int count_sleep; 
    public string WhatsAppType = ""; 
    public string DataToSend = ""; 
    public string ext = ""; 
    public void xyz(string nickname, string sender, string password, string[] Datadst, string DataWhatsAppType, string DataDataToSend, string Dataext) 
    { 
     dst = Datadst; 
     WhatsAppType = DataWhatsAppType; 
     DataToSend = DataDataToSend; 
     ext = Dataext; 
     WhatsApp wa = new WhatsApp(sender, password, nickname, true); 
    } 
} 

請注意,公共實例字段還是不好的設計,但我不知道在哪個環境下,你會使用這個類,所以我讓他們公開。

+0

你的速度更快! – mybirthname 2014-10-17 06:13:41

+1

此外,請注意,堆棧跟蹤告訴你到底是哪裏出現問題:「在WhatsApp_Bulk.WhatsAppSend..cctor()在D:\ WhatsApp批量\ WhatsApp批量\ WhatsAppSend.cs:32行」即。 .cs文件的第32行,這應該變成那行代碼。 – 2014-10-17 06:14:32

+0

SO可以做些什麼來使用'公共靜態int count_sleep = Convert.ToInt32(form1.txtMsgGap.Text);'我應該初始化它 – Kirtesh 2014-10-17 06:16:28

0

使WhatsAppSend類靜態然後調用,根據我的假設該類中聲明的變量是靜態的,所以類應該有相同的訪問修飾符。