Error using EPPlus, only in Debug Mode

Support for all customers who have purchased a SAPIEN PowerShell Studio product license. This forum does not offer a response time guarantee.
Forum rules
DO NOT POST SUBSCRIPTION NUMBERS, LICENSE KEYS OR ANY OTHER LICENSING INFORMATION IN THIS FORUM.
Only the original author and our tech personnel can reply to a topic that is created in this forum. If you find a topic that relates to an issue you are having, please create a new topic and reference the other in your post.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
User avatar
SuncareApeldoorn
Posts: 6
Joined: Fri Apr 29, 2016 3:08 pm

Error using EPPlus, only in Debug Mode

Post by SuncareApeldoorn » Thu Jul 19, 2018 1:32 am

Product, version and build: Powershell Studio 2018 v5.5.153
32 or 64 bit version of product: x64
Operating system: Windows 10.0.17134
32 or 64 bit OS: x64

Hello,

In my project I'm trying to have the user select one of the sheets of an Excelfile. I'm using EPPlus rather than the COM-object approach for several reasons (version: 4.5.2.1, by using Add-Type -Path 'C:\Temp\EPPlus.4.5.2.1\lib\net40\EPPlus.dll').
But that only works fine in Debug Mode until I assign one of the worksheets to a variable, e.g. with a line of code like this:
  1. $Sheet = $Package.Workbook.Worksheets['Sheet1']


What happens then, is that all code will be executed normally but only until the point where a breakpoint is set, wherever that might be. And then this error is displayed (which translates to object reference not set to an instance of an object):
error.png
error.png (16.9 KiB) Viewed 745 times
If I run the project in Debug Mode but without a breakpoint or just run it in normal mode without any debugging, there's no error at all.

Attached Project.ps1 is the project reduced to the minimum but still producing the error.

I hope you can help me with this, as it is kind of cumbersome to change some code, export the whole project to clipboard, paste it into the Powershell ISE, set a breakpoint and try to find what's going wrong and so on. So thanks in advance for any advice and/or help!
Attachments
Project.ps1
(4.07 KiB) Downloaded 10 times

User avatar
Alexander Riedel
Posts: 6604
Joined: Tue May 29, 2007 4:43 pm

Re: Error using EPPlus, only in Debug Mode

Post by Alexander Riedel » Thu Jul 19, 2018 6:37 am

The error message is pretty clear. You are using a variable which is not initialized.
If you want to set the text of a selected tab you need to always include a test if there actual is a selected tab, i.e. if($tbSelectedTab -ne $null)
You should not ever install assemblies in a Windows temporary folder, as it might lead to permission problems.
The line you say causes the problem "$Sheet = $Package.Workbook.Worksheets['Sheet1']" is not the line shown the error message.
This also suggests you have an initialization error somewhere.

In the ISE you might get different results because you have likely runspace contamination, meaning variables are set and retain values from whatever you have done before.
The sample you attached does not contain any line of code as you show in the error message.
Please understand that we cannot install third party code to help you debug a script. This EPPlus project would need to pass quite a number of tests before we could
install and test with it.
Alexander Riedel
SAPIEN Technologies, Inc.

User avatar
SuncareApeldoorn
Posts: 6
Joined: Fri Apr 29, 2016 3:08 pm

Re: Error using EPPlus, only in Debug Mode

Post by SuncareApeldoorn » Mon Jul 23, 2018 3:00 am

Could you then tell me what could be some possible cause(s) in general of an error's only showing up while Debugging and with at least one breakpoint set? Because if I Run (Ctrl-F5) the project there's no error at all, nor is there when I Debug without any breakpoints.
And at whatever line I set the breakpoint, the same error (object reference not set to an instance of an object) will appear as a result of that line, whatever that is.

E.g. to get below error, I put a temporary button on the form and set the code for the Click-event to: "1 + 1". Even then, the same error appears:
error1+1.png
error1+1.png (17.28 KiB) Viewed 625 times
I understand you cannot go around and install all sorts of third party code so that's why I'm asking if you could give me a direction in which to look for, as it seems kind of strange to me that the error only appears if and when a breakpoint is hit.

User avatar
SuncareApeldoorn
Posts: 6
Joined: Fri Apr 29, 2016 3:08 pm

Re: Error using EPPlus, only in Debug Mode

Post by SuncareApeldoorn » Wed Aug 01, 2018 4:26 am

I would still very much like to solve this problem so could you please give me some possible areas to look in or to try some stuff? Or even a workaround would still be nice...

User avatar
Alexander Riedel
Posts: 6604
Joined: Tue May 29, 2007 4:43 pm

Re: Error using EPPlus, only in Debug Mode

Post by Alexander Riedel » Wed Aug 01, 2018 10:50 am

Remove all references to this EPPlus dll. Does it work then?
Alexander Riedel
SAPIEN Technologies, Inc.

User avatar
SuncareApeldoorn
Posts: 6
Joined: Fri Apr 29, 2016 3:08 pm

Re: Error using EPPlus, only in Debug Mode

Post by SuncareApeldoorn » Thu Aug 02, 2018 4:36 am

Yes, that works. But it also works if I run the project (instead of debugging it).

User avatar
SuncareApeldoorn
Posts: 6
Joined: Fri Apr 29, 2016 3:08 pm

Re: Error using EPPlus, only in Debug Mode

Post by SuncareApeldoorn » Thu Aug 09, 2018 3:11 pm

Well, what do you know! You're not going to believe this but lo and behold:

If I delete all of the code, there's no error anymore!

Thank you really much for your help Alexander!

User avatar
Alexander Riedel
Posts: 6604
Joined: Tue May 29, 2007 4:43 pm

Re: Error using EPPlus, only in Debug Mode

Post by Alexander Riedel » Fri Aug 10, 2018 7:22 am

I am sensing some sarcasm here. Be as it may, if there was any work around or solution I could provide, I would do so.
It is most likely a threading issue with that specific module, but that is not something that is easy to track down.
Alexander Riedel
SAPIEN Technologies, Inc.

User avatar
SuncareApeldoorn
Posts: 6
Joined: Fri Apr 29, 2016 3:08 pm

Re: Error using EPPlus, only in Debug Mode

Post by SuncareApeldoorn » Tue Aug 14, 2018 1:29 am

You sensed that completely right ;) With my last post not being answered and no mentioning of any direction for me to look for a solution or work around despite my asking twice, I felt like the absence of a reaction had to do with unwillingness because I thought you guys would only have to think for a minute to know the answer to these kind of questions.

But now I understand and would yet like to thank you for still responding to my somewhat cheeky message :oops:

User avatar
Alexander Riedel
Posts: 6604
Joined: Tue May 29, 2007 4:43 pm

Re: Error using EPPlus, only in Debug Mode

Post by Alexander Riedel » Tue Aug 14, 2018 6:07 am

Paste some code surrounding the area where it fails. Say 10 lines up and 10 lines down.
We try different locations for breakpoints to narrow down what the problem could be.
Alexander Riedel
SAPIEN Technologies, Inc.