0

Scenario: There are 3 fields in the Screen where i enter a number in the 1st field text box and i need to enter the same number in the 3rd and 4th text box as well

///So in this case i have created methods to store the entered text as seen below:

  public  String checkcreatedSampleNoentered() throws InterruptedException {
          Thread.sleep(3500);
          String numberfromsample = "";
          {
          samplenos= "//input[@formcontrolname='sample']";
          Thread.sleep(500);
          WebElement samplenoteno = driver.findElement(By.xpath(samplenos));
              numberfromsample = samplenoteno.getAttribute("value");
              System.out.println("Print the sampenumber:"+numberfromsample);

      }
      return numberfromsample; - this should return the number that was created in the first text box 

}

public String getsampleNo() throws InterruptedException {
    return checkcreatedSampleNoentered();
}
public void storeSampleNo() throws InterruptedException {
    samplenos = checkcreatedSampleNoentered();

}

In CommonUseUtils i have created the getters and setters for this:

 public class CommonUseUtil {
public static String samplenos;
public static String getSamplenos() {
    return samplenos;
}

public  void setSamplenos(String samplenos) {
    CommonUseUtil.samplenos = samplenos;
}

}

And in the method where iam using to call this samplenumber text

     apputils.doSendKeys(palletNo,CommonUseUtil.getSamplenos());

Basically the samplenumber returns as null so that is why iam getting this error

Any leads would be very helpful

1 Answer 1

-1

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.

3
  • tried as per what you said but still getting null value ...element locator is getting highlighted .. System.out.println("Print the sampleno:" + CommonUseUtil.getSamplenos()); this part is returning null ...@IAmMilinPatel any other inputs?
    – user56056
    Apr 27 at 5:40
  • Did a sysout in the checkcreatedSampleNoentered method and it is printing the number the issue is in commonuseutil..bz when i try to Commonuseutil.getsampleno it is not printing the number - @IAmMilinPatel
    – user56056
    Apr 27 at 6:41
  • Have tried to add more information based on your comments. Apr 28 at 5:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.