NullPointerException
HELLO
I'm running 2024.2 (5172)
some of Jython scripts started crashing.
I tracked it down to A NullPointerException being generated by the code
curr = currencies.getCurrencyByTickerSymbol(tickerSym)
Yes The ticker needs to be added to the account.
I normally print a nice message saying please add ticker xyz to your account when it returned None. and carry on
but now the entire script crashes.
My fix is to put a try/accept trap around the function.
It did take me some time to figure out what was wrong.
Please fix
Thanks
Wayne
Comments are currently closed for this discussion. You can start a new one.
Keyboard shortcuts
Generic
? | Show this help |
---|---|
ESC | Blurs the current field |
Comment Form
r | Focus the comment reply box |
---|---|
^ + ↩ | Submit the comment |
You can use Command ⌘
instead of Control ^
on Mac
1 Posted by Stuart Beesley ... on 16 Feb, 2025 09:36 PM
Repeat. Then grab help/console and upload that here.
2 Posted by Stuart Beesley ... on 16 Feb, 2025 09:37 PM
Also. Can you post the code that’s failing? Or at least more than the above?
3 Posted by Stuart Beesley ... on 16 Feb, 2025 10:32 PM
Are you sure that one of:
or
isn’t None at that point?
4 Posted by waynelloydsmith on 17 Feb, 2025 02:50 PM
Hello Stuart
I'm busy doing some updates to my scripts.
I will create a version without the try/accept fix
and post it for you when I have finished this.
Also Put some debug prints in it etc.
Thanks
Wayne
5 Posted by Stuart Beesley ... on 17 Feb, 2025 06:16 PM
It still might be quicker and easier to send me the help/show console output after you get the error?
6 Posted by waynelloydsmith on 17 Feb, 2025 07:48 PM
ok
here is some stuff
I modified my script .. removed the try/accept and added some debug message
I did 3 tests
1 - put a blank field in the csv file for the ticker (a frequent thing)
2- put a ticker in the csv file that was never seen before
3. put a real ticker in the csv file ENB but removed it from the Account
My code deals all the above by changing the ticker to FAKE-T and caring on.
been doing this for around ten years.
of course the Null Pointer except stopped me in my tracks.
Ill attach the results of my tests. test1 failed , test2 and test3 succeeded
I'll send you the scripts in another message
7 Posted by waynelloydsmith on 17 Feb, 2025 07:51 PM
seems to be some problem attaching stuff
8 Posted by waynelloydsmith on 17 Feb, 2025 07:54 PM
again
9 Posted by waynelloydsmith on 17 Feb, 2025 07:57 PM
This is the script that I used to do the testing .. without the try/accept fix
10 Posted by Stuart Beesley ... on 17 Feb, 2025 08:02 PM
Without looking at anything, the error message is very clear.
You are passing a ticker parameter value of None to getCurrencyByTickerSymbol(). Have you checked why that is None?
11 Posted by waynelloydsmith on 17 Feb, 2025 08:06 PM
these are scripts that are used to support Scota-Inv-new.py
12 Posted by waynelloydsmith on 17 Feb, 2025 08:08 PM
Yes I know I am passing a None.
Just like I've been doing for a long time.
I didn't used to blow up.
13 Posted by Stuart Beesley ... on 17 Feb, 2025 08:20 PM
To ask a dumb question. Whether it used to work or not, passing None will / would find None…
Would it not just be better to say something like:
14 Posted by Stuart Beesley ... on 17 Feb, 2025 08:22 PM
or just create a wrapper function:
15 Posted by waynelloydsmith on 17 Feb, 2025 09:26 PM
sure
but the try/accept works fine until they get it fixed
16 Posted by waynelloydsmith on 17 Feb, 2025 09:31 PM
the banks often pass transactions with no ticker.
so None is a real thing.
anyway it used to work fine.
17 Posted by Stuart Beesley ... on 17 Feb, 2025 09:37 PM
Please confirm what version of MD you were using when this worked for you before? I presume you recently upgraded and that’s when the scripts stopped working?
18 Posted by Stuart Beesley ... on 17 Feb, 2025 10:23 PM
Ok. I just checked and passing None was ‘allowed’ in 2022 and does not work from 2023 onwards. I suspect this is when the code base was changed to Kotlin and null safety was enforced by default.
I personally don’t see this as a bug. If anything perhaps None shouldn’t have been allowed in the first place. Your code already knows when the ticker is None so there is nothing to find.
Anyway, just my own comments. It’s not my system. I’m stepping out of this thread as there’s nothing more I can add, and you have a solution.
(Not support, just a fellow user)
Support Staff 19 Posted by Sean Reilly on 18 Feb, 2025 11:16 AM
Hi Wayne,
Stuart's exactly right that this method was updated when we converted the core code to kotlin which allows us to specify the optionality of parameters. This is one that was made non-nullable intentionally and I'd like to keep it that way.
The change on your side is a very simple one though, and no try/accept is necessary. Please just use the
or
operator when calling getCurrencyByTickerSymbol and you'll have clean code and no exceptions:getCurrencyByTickerSymbol(ticker or "")
Thanks!
Sean
--
Sean Reilly
Developer, The Infinite Kind
https://infinitekind.com
20 Posted by waynelloydsmith on 18 Feb, 2025 02:01 PM
ok I checked my Scotia script more carefully
the call to currencies.getCurrencyByTickerSymbol(tickerSym)
is already in a wrapper function called
def getSecurityAcct(rootAcct, invAcct, tickerSym):
so I just added
if tickerSym == None:
return None
to the beginning of the getSecurityAcct(rootAcct() function
........... its good to here that throwing a Null Pointer exception is intended behavior..
I was starting to worry about my system
None tickers are mostly on Bonds and Mutual Funds which is Normal.
The Scotia script is about 5 years old .
I moved my accounts to BMO and created a new script to import the BMO csv files.
the new script subs in FAKE-T as the ticker if it is None in the csv file.
That's why I didn't hit this Null Pointer till I tried running the old Scotia script.
All my accounts now carry FAKE-T as a security so I can easily find them after an import and fix them manually.
Since I now don't use Mutual Funds its only the Bonds that need to be fixed.
FAKE-T will also show up after an import if I forget to add the new security to the Account .
So I guess we can call this Closed.
Thanks
Wayne
oh I'm posting all my scripts on hithub waynelloydsmith/Moneydance-Scripts
waynelloydsmith closed this discussion on 18 Feb, 2025 09:19 PM.