I have a java method that passes some arguments to a python file. I am passing the the arguments this way :
String name=null; //can be null or some value in some cases.
....
String[] cmd = {
"python",
"C:/Python34/myscript.py",
name,
username,
pwd};
Process p=Runtime.getRuntime().exec(cmd);
the variable name would be null in some execution and it might not but when it becomes null i am getting java.lang.NullPointerException.
My python file myscript.py will run with null values if i am running it from command prompt but i am not able to from java.
How can i pass null value without the exception. Can you help me with this?
Since python and java are different processes you are communicating between using system calls, you cannot convert Java null to the command line.
(What you call null in Python is actually None)
What you can do is: pass empty string ("" not null) to the python program, and the python program can interpret empty strings and convert them to None.
Or decide a convention: if "<NULL>" string is passed from java then assume None from python side when parsing the args.
Other solutions like Jython would maybe allow this because they do no involve system calls.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With