gpt4 book ai didi

java - 无法实例化因 InvocableTargetException 和 NullPointerException 导致的类

转载 作者:行者123 更新时间:2023-12-01 23:26:13 25 4
gpt4 key购买 nike

enter image description here//基类 包com.IVAPP.qa.Base;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;



public class BaseClass {
public static Properties prop;
public static WebDriver driver;



public BaseClass() throws IOException{


try{
prop = new Properties();

//prop.load(this.getClass().getResourceAsStream("C:\\Users\\Jomonli\\workspace\\IVAPP_Automation\\src\\main\\java\\com\\IVAPP\\qa\\Config\\Config.properties"));
FileInputStream ip = new FileInputStream(System.getProperty("C:\\Users\\Jomonli\\workspace\\IVAPP_Automation\\src\\main\\java\\com\\IVAPP\\qa\\Config\\Config.properties"));
prop.load(ip);
}catch(FileNotFoundException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}

}

public void intialization(){
// String browserName = prop.getProperty("browser");
//
// if(browserName.equals("IE")){
// System.setProperty("webdriver.ie.driver","C:\\Users\\Jomonli\\Desktop\\IEDriverServer_x64_3.14.0\\IEDriverServer.exe");
// //driver = new InternetExplorerDriver(); }

File file = new File("C:\\Users\\Jomonli\\Desktop\\IEDriverServer_x64_3.14.0\\IEDriverServer.exe");
System.setProperty("webdriver.ie.driver", file.getAbsolutePath());
driver = new InternetExplorerDriver();


driver.manage().window().maximize();
driver.get(prop.getProperty("url"));
}
}

这是扩展为我要运行的类的基类。

LoginPageTest
package com.IVAPP.qa.OffShoreTestCases;

import java.io.IOException;

import org.openqa.selenium.WebDriver;

import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import com.IVAPP.qa.Base.BaseClass;
import com.IVAPP.qa.OffShorePage.HomePage;
import com.IVAPP.qa.OffShorePage.LoginPage;



public class LoginPageTest extends BaseClass {
LoginPage loginpage;
HomePage homePage;
WebDriver driver;

public LoginPageTest() throws IOException{
super();
}

@BeforeMethod
public void setUp() throws IOException{
intialization();
// String exePath = "C:\\Users\\Jomonli\\Desktop\\IEDriverServer_x64_3.14.0\\IEDriverServer.exe";
// //For use of IE only; Please enable for IE Browser
// System.setProperty("webdriver.ie.driver", exePath);
// driver = new InternetExplorerDriver();
this.loginpage =new LoginPage();
homePage = new HomePage();
}
@Test(description="Logging in with valid credentials")
public void LoginTest() throws IOException{
homePage = loginpage.login(prop.getProperty("UserID"), prop.getProperty("Password"));
}
@AfterMethod
public void teardown(){
driver.quit();
}
}

================================================== =================================================== Testng.xml 文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite">
<test name="Test">
<classes>
<class name="com.IVAPP.qa.OffShoreTestCases.LoginPageTest"/>
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->

这是 testng .xml,它指向我正在尝试运行的正确类,但它说它无法实例化该类。这是否与驱动程序路径有关。

我尝试了 Project>Clean>Update maven,更改了路径,编写了不同的代码,但没有任何效果

Exception errors
org.testng.TestNGException:
Cannot instantiate class com.IVAPP.qa.OffShoreTestCases.LoginPageTest
at org.testng.internal.ObjectFactoryImpl.newInstance(ObjectFactoryImpl.java:40)
at org.testng.internal.ClassHelper.createInstance1(ClassHelper.java:373)
at org.testng.internal.ClassHelper.createInstance(ClassHelper.java:285)
at org.testng.internal.ClassImpl.getDefaultInstance(ClassImpl.java:126)
at org.testng.internal.ClassImpl.getInstances(ClassImpl.java:191)
at org.testng.TestClass.getInstances(TestClass.java:104)
at org.testng.TestClass.initTestClassesAndInstances(TestClass.java:90)
at org.testng.TestClass.init(TestClass.java:82)
at org.testng.TestClass.<init>(TestClass.java:45)
at org.testng.TestRunner.initMethods(TestRunner.java:422)
at org.testng.TestRunner.init(TestRunner.java:252)
at org.testng.TestRunner.init(TestRunner.java:222)
at org.testng.TestRunner.<init>(TestRunner.java:171)
at org.testng.remote.support.RemoteTestNG6_9_10$1.newTestRunner(RemoteTestNG6_9_10.java:28)
at org.testng.remote.support.RemoteTestNG6_9_10$DelegatingTestRunnerFactory.newTestRunner(RemoteTestNG6_9_10.java:61)
at org.testng.SuiteRunner$ProxyTestRunnerFactory.newTestRunner(SuiteRunner.java:604)
at org.testng.SuiteRunner.init(SuiteRunner.java:170)
at org.testng.SuiteRunner.<init>(SuiteRunner.java:117)
at org.testng.TestNG.createSuiteRunner(TestNG.java:1359)
at org.testng.TestNG.createSuiteRunners(TestNG.java:1346)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1200)
at org.testng.TestNG.runSuites(TestNG.java:1124)
at org.testng.TestNG.run(TestNG.java:1096)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:132)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:236)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:81)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at org.testng.internal.ObjectFactoryImpl.newInstance(ObjectFactoryImpl.java:29)
... 25 more
Caused by: java.lang.NullPointerException
at java.util.Properties$LineReader.readLine(Unknown Source)
at java.util.Properties.load0(Unknown Source)
at java.util.Properties.load(Unknown Source)
at com.IVAPP.qa.Base.BaseClass.<init>(BaseClass.java:28)
at com.IVAPP.qa.OffShoreTestCases.LoginPageTest.<init>(LoginPageTest.java:23)
... 30 more

这是我遇到的两个异常(exception)

最佳答案

如您所见,尝试读取配置文件时出现异常。

Caused by: java.lang.NullPointerException
at java.util.Properties$LineReader.readLine(Unknown Source)
at java.util.Properties.load0(Unknown Source)
at java.util.Properties.load(Unknown Source)

您可以更改配置路径吗?

FileInputStream ip = new FileInputStream("Config/Config.properties");
prop.load(ip);

关于java - 无法实例化因 InvocableTargetException 和 NullPointerException 导致的类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58306782/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com