The error "java.lang.IllegalArgumentException: Keys to send should be a not null CharSequence as the stored value returns null" is occurring because the value being retrieved by the method checkcreatedSampleNoentered()
is returning null. This means that the element from which you are trying to get the value is not present or the value is not being stored properly.
Here are a few things you can try:
1 Check if the element locator is correct - Make sure that the locator used to identify the element is correct and is able to identify the element properly.
2 Check if the element is visible and enabled - Make sure that the element is visible and enabled before trying to get the value. You can use explicit waits to ensure that the element is visible and enabled before proceeding.
3 Check if the value is being stored properly - Make sure that the value is being stored properly in the samplenos
variable. You can print the value of samplenos
before returning it to ensure that it is not null.
4 Check if the value is being set properly - Make sure that the value of samplenos
is being set properly in the CommonUseUtil
class. You can print the value of samplenos
after setting it to ensure that it is not null.
Here's an updated version of your code with the modifications:
public String checkcreatedSampleNoentered() throws InterruptedException {
Thread.sleep(3500);
String numberfromsample = "";
samplenos= "//input[@formcontrolname='sample']";
WebElement samplenoteno = new WebDriverWait(driver, 10).until(ExpectedConditions.visibilityOfElementLocated(By.xpath(samplenos)));
if(samplenoteno.isEnabled()){
numberfromsample = samplenoteno.getAttribute("value");
System.out.println("Print the sampenumber:"+numberfromsample);
}
return numberfromsample;
}
public String getsampleNo() throws InterruptedException {
return checkcreatedSampleNoentered();
}
public void storeSampleNo() throws InterruptedException {
CommonUseUtil.setSamplenos(checkcreatedSampleNoentered());
}
In the checkcreatedSampleNoentered()
method, I've added explicit wait for the element to be visible and enabled before getting the value. In the storeSampleNo()
method, I'm setting the value of samplenos in the CommonUseUtil
class using the setSamplenos()
method. You can now call storeSampleNo()
method to store the value of the first field and then use getSamplenos()
method to retrieve the stored value for the third and fourth fields.
I hope this helps you in resolving the issue.
Edit:
If the value is being printed correctly in the checkcreatedSampleNoentered() method, but not being retrieved properly from the CommonUseUtil class, it's possible that the issue is with the scope of the samplenos variable.
In the CommonUseUtil class, the samplenos variable is declared as a static variable. This means that the value of the variable will be shared across all instances of the class. However, in the checkcreatedSampleNoentered() method, the value of samplenos is set for the current instance of the class, and not for the static variable.
To fix this issue, you can change the samplenos variable in the CommonUseUtil class from static to instance variable. Then, you can create an instance of the CommonUseUtil class in your main class and use it to set and get the value of samplenos.
Here's an updated version of your code with the modifications:
public class CommonUseUtil {
public String samplenos;
public String getSamplenos() {
return samplenos;
}
public void setSamplenos(String samplenos) {
this.samplenos = samplenos;
}
}
public String checkcreatedSampleNoentered() throws InterruptedException {
Thread.sleep(3500);
String numberfromsample = "";
samplenos= "//input[@formcontrolname='sample']";
WebElement samplenoteno = new WebDriverWait(driver, 10).until(ExpectedConditions.visibilityOfElementLocated(By.xpath(samplenos)));
if(samplenoteno.isEnabled()){
numberfromsample = samplenoteno.getAttribute("value");
System.out.println("Print the sampenumber:"+numberfromsample);
}
return numberfromsample;
}
public String getsampleNo() throws InterruptedException {
return checkcreatedSampleNoentered();
}
public void storeSampleNo() throws InterruptedException {
CommonUseUtil commonUseUtil = new CommonUseUtil();
commonUseUtil.setSamplenos(checkcreatedSampleNoentered());
}
Now, you can create an instance of the CommonUseUtil class in your main class and use it to set and get the value of samplenos. For example:
CommonUseUtil commonUseUtil = new CommonUseUtil();
commonUseUtil.storeSampleNo();
System.out.println("Print the sampleno:" + commonUseUtil.getSamplenos());
I hope this helps you in resolving the issue.