为什么空引用异常?

时间:2012-06-26 05:38:00

标签: c# vb.net

任何人都可以帮助我,visual basic应用程序提示我一条消息“对象引用未设置为对象的实例”。我在一个解决方案中有2个项目。一个用c#开发,另一个用vb开发。我在vb项目中调用c#项目的方法。 vb代码是:

    Dim objUserProfileSystem As New IndexCatalogSystem() // c# project class
    Dim Ds_Themes As New DataSet()                       //dataset
    Ds_Themes = objUserProfileSystem.FillThemes(msg, 1)  //c# class method returning dataset
    ThemeID = Ds_Themes.Tables(0).Rows(0)("ThemeID")     //getting themeid from dataset

我从c#应用程序调用vb应用程序。像:

    System.Diagnostics.Process.Start(Application.StartupPath + "\\DocumentViewer.exe ", " (" + val + ") ");

当我单独调试vb项目时它工作正常,当我从c#应用程序调用vb应用程序时,它会提示错误。我错过了任何.net框架参考?

这是Fill_Theme方法。

   public DataSet FillThemes(ref String msg, int UserID)
        {
            try
            {
                //inilialize the Connection 
                Connection objCon = new Connection();
                if (objCon.Ini_Connection(ref msg) == true)
                {
                    //declare data set
                    DataSet DsGroup = new DataSet();
                    //declare data adapter
                    SqlDataAdapter DaRole = new SqlDataAdapter();

                    //initialize Sql Select Command and fill dataset
                    DaRole.SelectCommand = new SqlCommand("SELECT ThemeID FROM DMUsers where UserID = " + PARA_User_ID, objCon.con);
                    DaRole.SelectCommand.Parameters.Add(PARA_User_ID, SqlDbType.Int).Value = UserID;
                    DaRole.SelectCommand.CommandType = CommandType.Text;
                    DaRole.Fill(DsGroup, "DMUsers");

                    //Dispose object
                    objCon.Dispose_Con(ref msg);
                    DaRole.SelectCommand.Dispose();
                    DaRole.Dispose();

                    //Return Folder Dataset
                    objCon.Dispose_Con(ref msg);
                    return DsGroup;
                }
                else
                {
                    msg = "Connect ion fail contact Administrator";
                }
                return null;
            }

我发展为调用主题的整体方法。

  Private Sub Themes()
    Dim objUserProfileSystem As New IndexCatalogSystem()
    Dim Ds_Themes As New DataSet()
    Ds_Themes = objUserProfileSystem.FillThemes(msg,modCommon.UserID)
    ThemeID = Ds_Themes.Tables(0).Rows(0)("ThemeID")

    If Ds_Themes IsNot Nothing Then
        If ThemeID = 1 Then
            MessageBox.Show("6")
            THEMES3Blue()
        ElseIf ThemeID = 2 Then
            THEMES2Olive()
        ElseIf ThemeID = 3 Then
            THEMES1Silver()
        Else
            THEMES1Silver()
        End If
    End If
End Sub

1 个答案:

答案 0 :(得分:0)

我在这一行中看到问题可能出现了:

ThemeID = Ds_Themes.Tables(0).Rows(0)("ThemeID") 

试试这个:

if(Ds_Themes != null) {
 if( Ds_Themes.Tables != null) {
    if( Ds_Themes.Tables.Size() > 0) {
       if(Ds_Themes.Tables(0).Rows != null) {
         if(Ds_Themes.Tables(0).Rows.Size() > 0) {
          ThemeID = Ds_Themes.Tables(0).Rows(0)("ThemeID")
         }
       }
    }
  }
}

我使用上面的5 if语句'因为VB.NET在计算“&&&”时没有优化运营商。