Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I pass null arguments to my Java main method using run configuration arguments in Eclipse?

I'm trying to run a main method that requires four arguments, but the last one can be null (If #4 is null, use the string value in #2 as input. If #4 is NOT null, use #4 as a file path and use the string in that file as input instead. Bad coding, I didn't write it). However, I'm having trouble getting Eclipse to accept that fourth argument as null.

If I put anything on the fourth line, it takes it as a string. If I put nothing, then it only processes three arguments.

Does anyone know how to get it to accept a null value through the run configuration arguments?

Thanks in advance,

Brandon

like image 976
BrandonToms Avatar asked Nov 21 '25 07:11

BrandonToms


1 Answers

The arguments can never be null. They just won't exist, so what you need to do is to check the length of your arguments.

public static void main(String[] args)
{
    // Check how many arguments were passed in
    if(args.length == 3)
    {     
        //the last arguement is null
    }
}
like image 121
mohsen kamrani Avatar answered Nov 22 '25 19:11

mohsen kamrani



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!