3 Replies Latest reply on Jul 19, 2017 4:13 PM by Sean Donnelly

    'Object reference not set to an instance of an object.' GetVar C#

    Sean Donnelly

      Basically, I have my own search using WinForms (standalone application outside of PDM) so that I can search for "base" part numbers in my search and pull in variables when I select the filename in the list. The first part works nicely and I'm trying to now pull variables from the file. I can get file information just fine, but it's breaking when I try to set my textboxes to the object string.

       

      Edit: I should mention the checkVault() is a method for getting the vault and logging in. this is not an issue as I am able to pull information using this method such as filename, path, id, etc.

       

              public void checkVault()
              {
                  vault2 = null;
                  if (vault1 == null)
                  {
                      vault1 = new EdmVault5();
                  }
                  vault2 = (IEdmVault7)vault1;
      
      
                  if (!vault1.IsLoggedIn)
                  {
                      //vault1.LoginAuto("FocalPoint Vault", this.Handle.ToInt32());
                      vault1.LoginAuto("FocalPoint Vault", this.Handle.ToInt32());
                  }
              }
      

       

              private void listResults_MouseClick(object sender, MouseEventArgs e)
              {
                  string configName = "Layout1";
                  object Desc1 = null;
                  object Desc2 = null;
                  object Rev = null;
                  //object StockingType = null;
      
      
                  int numSel = listResults.SelectedIndex;
                  if (numSel >= 0)
                  {
                      checkVault();
      
      
                      IEdmFolder5 retFolder = default(IEdmFolder5);
                      aFile = vault1.GetFileFromPath(listResults.SelectedValue.ToString(), out retFolder);
      
      
                      IEdmEnumeratorVariable10 myvars = default(IEdmEnumeratorVariable10); //Set the enumeratorvariable object  
                      myvars = (IEdmEnumeratorVariable10)aFile.GetEnumeratorVariable(); //Cast the enumeratorvariable object to the file  
      
      
                      //myvars.GetVar("CreatedBy", configName, out CreatedBy); //Use the GetVar method  
                      myvars.GetVar("CAD_Description", configName, out Desc1);
                      myvars.GetVar("CAD_Description1", configName, out Desc2);
                      myvars.GetVar("CAD_Revision", configName, out Rev);
                      myvars.CloseFile(false); //Always close the file when done accessing card variables to avoid runtime errors  
                                               //Mymessage = Mymessage + "File ID: " + myfile.ID + System.Environment.NewLine; //Create a message with the File ID
                      //breaks here
                      textRev.Text = Rev.ToString();
                      textDesc1.Text = Desc1.ToString();
                      textDesc2.Text = Desc2.ToString();
                  }
      
      
      
      
              }
      
        • Re: 'Object reference not set to an instance of an object.' GetVar C#
          Lee CS Young
          • The error is telling you exactly what the issue is, 'Rev' is a null. You could use the null-conditional operator if you're using C# 6. Other wise use a normal conditional. (i.e. textRev.Text = (Rev == null) ? string.Empty : Rev.ToString() )
          • It may not be pulling values from the file because your configName may not be correct. Try Model.
          • IIRC you don't need to call CloseFile() when you're only using GetVar().
            • Re: 'Object reference not set to an instance of an object.' GetVar C#
              Sean Donnelly

              I am still having issues even after trying your methods. I am now trying a SolidWorks template which is also not cooperating and giving me the same issue with their code. I modified it below to have my configuration name ("@") and variable name ("CAD_Revision"). As seen in the image below, the value A is present for the variable. When I run the code without the complex third MessageBox (instead simply asking for the value in RevisionObj.ToString() ) it tells me the object is not found. When I do add the third MessageBox to be similar to your recommendation, it shows blank. There is clearly a value here though. I have the latest version of the file as well.

               

              Template code: 2014 SOLIDWORKS API Help - Set Initial Revision Example (C#)

               

              Reminder, this is a standalone application reading from EPDM and is not an add-in inside of SolidWorks. Thanks for the help.

               

              Image of drawing in question, with variable clearly present:

               

              Modified section of template code:

               

              (image in case its cutoff. the preview does not show MessageBox )

               

              private void SetInitialRevisionButton_Click(System.Object sender, System.EventArgs e)
                      {
                          try
                          {
                              //Declare and create an instance of IEdmVault5
                              IEdmVault5 vault = new EdmVault5();
              
              
                              //Log into selected vault as the current user
                              vault.LoginAuto(VaultsComboBox.Text, this.Handle.ToInt32());
              
              
                              //Display a File Open dialog for user to
                              //choose the files on which set the initial revision
                              EdmStrLst5 PathList = default(EdmStrLst5);
                              PathList = vault.BrowseForFile(this.Handle.ToInt32(), (int)EdmBrowseFlag.EdmBws_ForOpen + (int)EdmBrowseFlag.EdmBws_PermitMultipleSel + (int)EdmBrowseFlag.EdmBws_PermitVaultFiles, "SolidWorks files " + "(*.sldprt; *.sldasm; *.slddrw)|" + "*.sldprt;*.sldasm;*.slddrw|" + "All Files (*.*)|*.*||", "", "", vault.RootFolderPath, "Select Files to Set Initial Revision on");
              
              
                              //Exit if the user clicks Cancel
                              if (PathList == null)
                                  return;
              
              
                              //Traverse the selected files
                              IEdmPos5 pos = default(IEdmPos5);
                              pos = PathList.GetHeadPosition();
                              while (!pos.IsNull)
                              {
                                  //Get each file path from the selected files list
                                  string FilePath = null;
                                  FilePath = PathList.GetNext(pos);
                                  //Get the IEdmFile5 object corresponding
                                  //to the path
                                  IEdmFile5 FileObj = default(IEdmFile5);
                                  IEdmFolder5 retfold = default(IEdmFolder5);
                                  FileObj = vault.GetFileFromPath(FilePath, out retfold);
              
              
                                  //Skip any files that are checked out
                                  if (FileObj.IsLocked)
                                  {
                                      MessageBox.Show(FilePath + " is checked out." + Constants.vbCrLf + "Check it in and try again." + Constants.vbCrLf + "Skipping this file.", "File is checked out.", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                                      continue;
                                  }
              
              
                                  //Get the value of the file data
                                  //card variable named Revision
                                  IEdmEnumeratorVariable8 EnumVarObj = default(IEdmEnumeratorVariable8);
                                  //Keeps the file open
                                  EnumVarObj = (IEdmEnumeratorVariable8)FileObj.GetEnumeratorVariable();
              
              
                                  bool GetVarSuccess = false;
                                  //GetVar returns a Variant
                                  object RevisionObj = null;
                                  GetVarSuccess = EnumVarObj.GetVar("CAD_Revision", "@", out RevisionObj);
                                  EnumVarObj.CloseFile(false);
                                  MessageBox.Show("Get Var");
                                  MessageBox.Show(GetVarSuccess.ToString());
                                  MessageBox.Show((RevisionObj == null) ? string.Empty : RevisionObj.ToString());