Error using EPPlus, only in Debug Mode

This forum can be browsed by the general public. Posting is limited to current SAPIEN license holders with active maintenance and does not offer a response time guarantee.
Forum rules
DO NOT POST LICENSE NUMBERS, ACTIVATION 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.
This topic is 5 years and 8 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
User avatar
JKNederland
Posts: 7
Last visit: Thu Mar 07, 2024 5:28 am

Error using EPPlus, only in Debug Mode

Post by JKNederland »

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 4106 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 120 times
User avatar
Alexander Riedel
Posts: 8488
Last visit: Tue Apr 16, 2024 8:42 am
Answers: 20
Been upvoted: 37 times

Re: Error using EPPlus, only in Debug Mode

Post by Alexander Riedel »

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
JKNederland
Posts: 7
Last visit: Thu Mar 07, 2024 5:28 am

Re: Error using EPPlus, only in Debug Mode

Post by JKNederland »

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 3986 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
JKNederland
Posts: 7
Last visit: Thu Mar 07, 2024 5:28 am

Re: Error using EPPlus, only in Debug Mode

Post by JKNederland »

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: 8488
Last visit: Tue Apr 16, 2024 8:42 am
Answers: 20
Been upvoted: 37 times

Re: Error using EPPlus, only in Debug Mode

Post by Alexander Riedel »

Remove all references to this EPPlus dll. Does it work then?
Alexander Riedel
SAPIEN Technologies, Inc.
User avatar
JKNederland
Posts: 7
Last visit: Thu Mar 07, 2024 5:28 am

Re: Error using EPPlus, only in Debug Mode

Post by JKNederland »

Yes, that works. But it also works if I run the project (instead of debugging it).
User avatar
JKNederland
Posts: 7
Last visit: Thu Mar 07, 2024 5:28 am

Re: Error using EPPlus, only in Debug Mode

Post by JKNederland »

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: 8488
Last visit: Tue Apr 16, 2024 8:42 am
Answers: 20
Been upvoted: 37 times

Re: Error using EPPlus, only in Debug Mode

Post by Alexander Riedel »

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
JKNederland
Posts: 7
Last visit: Thu Mar 07, 2024 5:28 am

Re: Error using EPPlus, only in Debug Mode

Post by JKNederland »

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: 8488
Last visit: Tue Apr 16, 2024 8:42 am
Answers: 20
Been upvoted: 37 times

Re: Error using EPPlus, only in Debug Mode

Post by Alexander Riedel »

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.
This topic is 5 years and 8 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.