Avatar of newbie java
newbie java
 asked on

How to handle null point exception while using switch strings

Was able to launch and close browser 1 only.
            
Error:


java.lang.NullPointerException
      at java.util.Hashtable.put(Unknown Source)
      at java.util.Properties.setProperty(Unknown Source)
      at java.lang.System.setProperty(Unknown Source)
      at newpackage.Launchbrowsers.Browser(Launchbrowsers.java:32)
      at newpackage.Launchbrowsers.main(Launchbrowsers.java:13)

Thanks in advance for any help.
* EclipseSeleniumJava

Avatar of undefined
Last Comment
dpearson

8/22/2022 - Mon
dpearson

You'll need to post some code if you want more help on this.
SOLUTION
CEHJ

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
newbie java

ASKER
package newpackage;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;


public class Launchbrowsers {

      public static void main(String[] args)throws Exception {
            
            Browser("Chrome");
            Browser("IE");
            Browser("Firefox");
            
            }
            
      public static void Browser(String Launcher) {
            
            switch(Launcher){
            case "Chrome":
                  String file1 = System.getProperty("user.dir")+"/lib/chromedriver.exe";
                  System.setProperty("webdriver.chrome.driver",file1);
                  WebDriver driver1 = new ChromeDriver();
                  driver1.get("https://apple.com/");
                  driver1.close();
                  System.out.println("Chrome is successfully launched and closed");
                  break;
                  
            case "IE":
                  
                  String file2 = System.getProperty("C:/Users/newproject/lib/IEDriverServer.exe");
                  System.setProperty("webdriver.ie.driver", file2);
                  WebDriver driver2 = new InternetExplorerDriver();
                  driver2.get("https://apple.com/");
                  driver2.close();
                  System.out.println("InternetExplorer is successfully launched and closed");
                  break;
                  
            case "Firefox":
                  String file3 = System.getProperty("user.dir")+"/lib/geckodriver.exe";
                  System.setProperty("webdriver.gecko.driver",file3);
                  WebDriver driver3 = new FirefoxDriver();
                  driver3.get("https://apple.com/");
                  driver3.close();
                  System.out.println("Firefox is successfully launched and closed");
                  break;
                  
            default:
                  System.out.println("Errors in browser + driver configuration");
                  break;
            }
            }
}
            
I thought I had attached the .java file here. Sorry, here's the program I'm trying to execute.
ASKER CERTIFIED SOLUTION
dpearson

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
dpearson

I think we covered this pretty well.
Your help has saved me hundreds of hours of internet surfing.
fblack61