Feeling trapped by the NullPointer Exception bug

I am currently automating the add to cart process on the website "http://www.oyehappy.com" using TestNG POM Structure. I have encountered a NullPointer Exception while handling autosuggestion. The code for my POM Class is as follows:

public class productPage_POM {
public WebDriver driver;

@FindBy(id="tagsf2")
private WebElement city_txtbox;
public productPage_POM(WebDriver driver)
{
    PageFactory.initElements(driver, this);
}

public void sel_adr()
{
    city_txtbox.sendKeys("gan");     // facing nullpointer exception here

    String xp = "//a[starts-with(text(),'gan')]";
    List<WebElement> allList = driver.findElements(By.xpath(xp));
    int count = allList.size();
    System.out.println(count);
    for (int i = 0; i < count ; i++) 
    {
        String name = allList.get(i).getText();
        System.out.println(name);
        if(name.contains("Gandhinagar"))
        {
            allList.get(i).click();
            break;
        }
    }

Below is the HTML structure:

<div class="pro_detail">
<div id="changeImage" class="pro_detail_image">
<div class="pro_detail_title">
<div class="pro_detail_size">
<div class="pro_detail_form">
<input id="cityIdHidden" type="hidden" value="CITY-1219">
<input id="hasShippingOption" type="hidden" value="">
<input id="cityIdHidden1" type="hidden" value="">
<input id="liCount" type="hidden" value="1">
<div id="hiddenField">
<div class="pro_detail_form_inn">
<label>1. Where do you want to deliver this?</label>
<input id="tagsf2" class="inp_pro_detail2 election ui-autocomplete-input" type="text" value="Enter City" onchange="showAddtocart();" onblur="WatermarkOnblur('Enter City','tagsf2');shippingOption();" onfocus="WatermarkOnfocus('Enter City','tagsf2')" autocomplete="off">
<span class="ui-helper-hidden-accessible" role="status" aria-live="polite">CITY-1219</span>
<div class="clear"></div>
<div id="cityNotAvailable" class="error_message hide" style="display: none;">
</div>
<div class="textbox">
</div>
<div class="pro_detail_form">
<div id="divDeliveryText" class="pro_detail_form" style="display:none;">

Answer №1

Perhaps you are viewing the Angular website, where the id changes dynamically. This could be why your code is unable to locate the defined id. Consider using a different selector that does not change dynamically. (I'm not very familiar with JavaScript).

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What steps can be taken to execute a selenium webdriver on a client machine?

I have integrated Selenium WebDriver with Internet Explorer in a Java web application. On my server, I am using Apache Tomcat 6 to host the application. While all tests are functioning correctly on my local machine, when attempting to access the applicatio ...

Newbie React Developer Struggling to Implement Material-UI Example in React Project, State Functioning Differently from Hooks

I am currently in the early stages of learning React and trying to create a form for a project management web application. For this, I am using material-ui library. I referred to one of the select box component examples from the material-ui documentation ...

Issue encountered: Cannot locate module: Error message - Unable to find 'stream' in 'C:devjszip-test ode_modulesjsziplib' folder

I am encountering an issue in my angular 7 application while using jszip v3.2.1. During the project build process (e.g., running npm start), I receive the following error message: ERROR in ./node_modules/jszip/lib/readable-stream-browser.js Module not fo ...

Looking for an easy solution in RegExp - how to locate the keys?

Similar Inquiries: Retrieving query string values using JavaScript Utilizing URL parameters in Javascript I am tasked with extracting specific keys from a series of URLs where the key is denoted by 'KEY=123'. My goal is to identify and e ...

React components do not re-render when the context value changes

The issue with React not re-rendering when the context value changes persists I am using tailwindcss, React(v18.2.0), and vite(3.2.4). I have already attempted i want that clicking on TodoItem should change the completed value in the todo using React con ...

Conceal a different div unless it includes

Hi everyone, I need help with a filter code snippet: $(".title").not(":contains('" + $("[name=filter]").val() + "')").hide() The issue I'm facing is that the .title class is nested within the .sortAll class along with many other divs. I w ...

The ontrack listener for WebRTC peerConnection fails to fire

Primarily, the challenge I'm facing is unique from what I have come across in my research on "google". My setup involves using Spring Boot as a signaling server to establish connections between two different tabs of the browser or utilizing two peerCo ...

What is the best way to access an excel file using JavaScript and Protractor?

Is it possible to read an Excel file dynamically in JavaScript and iterate through cell values using row and column counts, without converting it into a JSON object? I have searched through several posts on Stack Overflow but have not found a solution yet. ...

Implementing JavaScript functionality upon page load with ASP.NET

I have come across a situation where I am trying to integrate working code (jQuery/Javascript) that makes an API call and sends data to it. The API service then responds with a success or failure message based on the data insertion into the API db. Everyth ...

Guide to building a multi-dimensional array from a flat object

My endpoint is outputting data in a specific format: const result = [ {id: 4, parentId: null, name: 'Fruits & Veggies'}, {id: 12, parentId: 133, name: 'Sanguinello'}, {id: 3, parentId: 4, name: 'Fruits'}, {id: 67, ...

Is locking Node and npm versions necessary for frontend framework projects?

Currently working on frontend projects in React and Vue, I am using specific versions of node and npm. However, with other developers contributing to the repository, how can we ensure that they also use the same versions to create consistent js bundles? ...

Why does it appear that Angular is undefined in this straightforward Angular demonstration?

I'm completely new to AngularJS and I've encountered an issue. Yesterday, I ran a very simple AngularJS application that I downloaded from a tutorial and it worked perfectly. The application consists of 2 files: 1) index.htm: <!DOCTYPE htm ...

Retrieve Javascript files from the local static directory

Currently, I am developing a small project with Nuxt JS and I am facing a challenge in calling some Javascript files from my static directory. When it comes to CSS files, I have been able to do it successfully using the following code: css: [ './stat ...

Storing a Promise in a Variable in React

As a newcomer to JavaScript/React, I am finding it challenging to grasp the concept of using Promise and async. To illustrate, consider the API call getSimById in a JS file that returns a Promise: export function getSimById(simId) { return fetch(simsUrl ...

Encountering a WriteableDraft error in Redux when using Type Definitions in TypeScript

I'm facing a type Error that's confusing me This is the state type: export type Foo = { animals: { dogs?: Dogs[], cats?: Cats[], fishs?: Fishs[] }, animalQueue: (Dogs | Cats | Fishs)[] } Now, in a reducer I&a ...

Could you assist me in retrieving information from an API request?

I can't seem to pinpoint my mistake, but I know it's there. After the user provides their state and city information and submits it, a fetch request is supposed to retrieve latitude and longitude values. These values are then used in another API ...

What could be causing my "onChange" function to not work as expected?

export function UpdateData(props){ return( <div> <div className='updateForm'> <form> <div className="form-group"> <label>Your ID:& ...

Could there be an issue with my website's bootstrap where badges are not being applied properly?

Currently, I am in the process of learning ReactJS and running into an issue where my bootstrap is not working within my .jsx file. Despite following a tutorial (MOSH) diligently and extensively researching on stack overflow, I have not been able to find a ...

Issue with JQuery addClass functionality in Firefox

I've come across numerous posts on this topic, but none of them have provided a solution. I recently added drag and drop functionality to my website. When I drag an item over a valid container, I add a specific class to it. Here is the HTML for the ...

What is the method for assigning a value to a Material UI text field?

Trying to create an autocomplete search feature using EsriGeocode and Material UI. The form includes Street name, City, State, and Zip code fields. Currently facing an issue where the Street name text field displays the entire address instead of just the s ...