Setup Menus in Admin Panel

ITeLearn.com

java.lang.NullPointerException | Selenium Automation | Discussions | ITeLearn.com

Forum usage guidelines

  1. Search First.: If you don’t find a similar post, start a new topic. If you find a similar thread and still have a question, reply to that topic. Duplicate threads will be ignored/deleted without warning.
  2. Proper Subject: Ensure you choose a very appropriate topic name. Else it will be tough to get support from other members.
  3. Patience, please: Our Tech team or other members will usually try to respond within 24 hours on weekdays and max within 48 hours on weekends. Sometimes sooner.
  4. Share opinions: Please feel free to reply, add or comment on any ongoing topics. Collective learning will be of great benefit to all.

 
ITeLearn reviews from participants.

Please consider registering
guest

Log In

Lost password?
Advanced Search

— Forum Scope —

  

— Match —

   

— Forum Options —

   

Minimum search word length is 4 characters - maximum search word length is 84 characters

Topic RSS
java.lang.NullPointerException
February 23, 2017
9:25 pm
Sumathi Vooradi
New Member
Forum Posts: 1
Member Since:
April 25, 2016
Offline

Hi ,
Hi i am getting NullPointerException error.

package proj2DDF;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.concurrent.TimeUnit;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class DDF_WebDriver9 {
//This is our first webdriver code

// Define the selenium webDriver
WebDriver driver;
String vUrl;
String xlPath, xlSheet, xlPathRes;
String[][] xlTestData; // Define a 2D string
int xRows, xCols;
String vActualValue;

@Before
public void myBefore() throws Exception{

System.out.println(“Hi before”);

// Initialize the selenium webdriver
System.setProperty(“webdriver.chrome.driver”,”C:\\Users\\venkataramana\\Documents\\SBT-dec16\\Drivers\\chromedriver.exe”);

driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);

xlPath = “C:\\Users\\venkataramana\\Desktop\\BR DDF\\DDF1-BR.xls”;
xlPathRes= “C:\\Users\\venkataramana\\Desktop\\BR DDF\\DDF1-BR Result1.xls”;
xlSheet = “TestData”;
xlTestData = readXL(xlPath, xlSheet);

// Store the basic URL
vUrl = “http://www.bankrate.com/calculators/auto/auto-loan-calculator.aspx”;

}

@Test
public void myTest() throws Exception{
String vTDSet, vLoanAmt, vTermYears, vRate, vExpectedValue;
for (int i=1; i<xRows; i++ ){
vTDSet = xlTestData[i][0];
vLoanAmt = xlTestData[i][1];
vTermYears = xlTestData[i][2];
vRate = xlTestData[i][3];
vExpectedValue = xlTestData[i][4];

xlTestData[i][6] = bankRateTest1(vUrl, vLoanAmt, vTermYears, vRate, vExpectedValue);
xlTestData[i][7] = bankRateTest2(vTermYears);
xlTestData[i][5] = vActualValue;
}

//close the website
driver.quit();

}

@After
public void myAfter() throws Exception{
System.out.println("hi ");
writeXL(xlPathRes, "TestDataRes", xlTestData);
}

public String bankRateTest1(String fUrl, String fLA, String fTerm, String fRate, String fEV) throws Exception{
// Input : LA, Term, Rate, ExpectedValue
// Output : Pass or Fail

//Driver go to the main url or base url
//1.open the website
driver.get(fUrl);

//2.Enter Auto loan amount 23000
driver.findElement(By.xpath("//input[@id=’loanAmount’]")).clear();
driver.findElement(By.xpath("//input[@id=’loanAmount’]")).sendKeys(fLA);

//3.Enter Term in years 5
driver.findElement(By.xpath("//input[@id=’years’]")).clear();
driver.findElement(By.xpath("//input[@id=’years’]")).sendKeys(fTerm);

//4.Rate per year 2.99
driver.findElement(By.xpath("//input[@id=’interestRate’]")).clear();
driver.findElement(By.xpath("//input[@id=’interestRate’]")).sendKeys(fRate);

//5.click on calculate
driver.findElement(By.xpath("//button[@id=’calcButton’]")).click();
Thread.sleep(2000);

//6.verify the payment 413.18
//where is the payment?
String vActualValue = driver.findElement(By.xpath("//span[@id=’mpay’]")).getText();
//What is the value?
System.out.println("The value on the website is " + vActualValue);
//compare with what is expected

System.out.println("The Expected value is " + fEV);
if (vActualValue.equals(fEV)){
System.out.println("Test is a pass");
return "pass";
} else {
System.out.println("Test is a fail");
return "fail";
}
}

public String bankRateTest2(String fTerm){
// Calculate the term in months
int fTermMonths;
String fTermActual;

fTermMonths = Integer.parseInt(fTerm) * 12;
System.out.println("Expected Term in months is " + fTermMonths);

// Get the actual value from the website
// Where is the Element
fTermActual = driver.findElement(By.xpath("//input[@id=’terms’]")).getAttribute("value");
// Which Attribute has that value

// Compare if they are the same
if (Integer.parseInt(fTermActual) == fTermMonths){
System.out.println("Test2 Pass");
return "Pass";
} else {
System.out.println("Test2 Fail");
return "Fail";
}

}

// Teach Java to R/W from MS Excel
// Method to read XL
public String[][] readXL(String fPath, String fSheet) throws Exception{
// Inputs : XL Path and XL Sheet name
// Output :

String[][] xData;

File myxl = new File(fPath);
FileInputStream myStream = new FileInputStream(myxl);
HSSFWorkbook myWB = new HSSFWorkbook(myStream);
HSSFSheet mySheet = myWB.getSheet(fSheet);
xRows = mySheet.getLastRowNum()+1;
xCols = mySheet.getRow(0).getLastCellNum();
System.out.println("Total Rows in Excel are " + xRows);
System.out.println("Total Cols in Excel are " + xCols);
xData = new String[xRows][xCols];
for (int i = 0; i < xRows; i++) {
HSSFRow row = mySheet.getRow(i);
for (int j = 0; j < xCols; j++) {
HSSFCell cell = row.getCell(j);
String value = "-";
if (cell!=null){
value = cellToString(cell);
}
xData[i][j] = value;
System.out.print(value);
System.out.print("—-");
}
System.out.println("");
}
myxl = null; // Memory gets released
return xData;
}

//Change cell type
public static String cellToString(HSSFCell cell) {
// This function will convert an object of type excel cell to a string value
int type = cell.getCellType();
Object result;
switch (type) {
case HSSFCell.CELL_TYPE_NUMERIC: //0
result = cell.getNumericCellValue();
break;
case HSSFCell.CELL_TYPE_STRING: //1
result = cell.getStringCellValue();
break;
case HSSFCell.CELL_TYPE_FORMULA: //2
throw new RuntimeException("We can’t evaluate formulas in Java");
case HSSFCell.CELL_TYPE_BLANK: //3
result = "%";
break;
case HSSFCell.CELL_TYPE_BOOLEAN: //4
result = cell.getBooleanCellValue();
break;
case HSSFCell.CELL_TYPE_ERROR: //5
throw new RuntimeException ("This cell has an error");
default:
throw new RuntimeException("We don’t support this cell type: " + type);
}
return result.toString();
}

// Method to write into an XL
public void writeXL(String fPath, String fSheet, String[][] xData) throws Exception{

File outFile = new File(fPath);
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet osheet = wb.createSheet(fSheet);
int xR_TS = xData.length;
int xC_TS = xData[0].length;
for (int myrow = 0; myrow < xR_TS; myrow++) {
HSSFRow row = osheet.createRow(myrow);
for (int mycol = 0; mycol < xC_TS; mycol++) {
HSSFCell cell = row.createCell(mycol);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellValue(xData[myrow][mycol]);
}
FileOutputStream fOut = new FileOutputStream(outFile);
wb.write(fOut);
fOut.flush();
fOut.close();
}
}

}

In my Failure trace i am getting this.
java.lang.NullPointerException
at proj2DDF.DDF_WebDriver9.readXL(DDF_WebDriver9.java:162)
at proj2DDF.DDF_WebDriver9.myBefore(DDF_WebDriver9.java:48)

java.lang.NullPointerException
at proj2DDF.DDF_WebDriver9.writeXL(DDF_WebDriver9.java:219)
at proj2DDF.DDF_WebDriver9.myAfter(DDF_WebDriver9.java:80)

Please reply for this error.
Thanks

April 15, 2017
5:03 pm
Soujanya Avunuri
New Member
Forum Posts: 2
Member Since:
January 28, 2017
Offline

Hi Team,

I am also getting g NullPointerException error.

Below is my code:

package selenium_feb2;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.concurrent.TimeUnit;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;

public class DDF3 {
static WebDriver myd;
static int xRows, xCols;
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub

System.out.println(“Test case execution started”);
//Test Data Varaibles declaration
String vTDID, vURL, vHvalue, vLvalue,vIRate,vLmonth,vLyear,vExpayment,vActpayment,vResult,vExecute;
String xlPath, xlSheet;

// Info about the excel sheet where TD is
xlPath = “C:\\\\Users\\\\vipfi\\\\soujanya\\\\DDF\\\\DDF1.xls”;
xlSheet = “TD”;
String xlPath_Res = “C:\\\\Users\\\\vipfi\\\\soujanya\\\\DDF\\\\DDF1_res.xls”;
//Read XL data into a 2D array
String xTD[][] = readXL(xlPath, xlSheet);

System.setProperty(“webdriver.gecko.driver”,”C:\\\\Users\\\\vipfi\\\\soujanya\\\\geckodriver-v0.14.0-win64\\\\geckodriver.exe”);

myd = new FirefoxDriver();

myd.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

//myd.navigate().to(“http://www.mortgagecalculator.org/”);

for (int tdrow=1; tdrow<xRows; tdrow++){
//Read TD Values for each row

vTDID = xTD[tdrow][0];
vURL = xTD[tdrow][2];
vHvalue = xTD[tdrow][3];
vLvalue=xTD[tdrow][4];
vIRate= xTD[tdrow][5];
vLmonth = xTD[tdrow][6];
vLyear = xTD[tdrow][7];
vExpayment =xTD[tdrow][8];
vActpayment = "";
vResult = "";
vExecute = xTD[tdrow][11];

if (vExecute.equalsIgnoreCase("Y")){

System.out.println("TD set running——" + vTDID);
myd.navigate().to(vURL);

enterValues(vHvalue, vLvalue,vIRate,vLmonth,vLyear);
vResult = submitcompare(vExpayment);
System.out.println("Test is a : " + vResult);

System.out.println("TD set finished——" + vTDID);

}else
{
System.out.println("TD set skipped——" + vTDID);
//close the browser
}
}
myd.close();

writeXL(xlPath_Res, "TDRESULT", xTD);
System.out.println("Test Case Execution Ended");

}

public static String submitcompare(String fExpayment) throws InterruptedException{
myd.findElement(By.name("cal")).click();

//verify mothly payment
Thread.sleep(3000);

String fActpayment = myd.findElement(By.xpath("(//h3)[2]")).getText();
System.out.println("Expected payment is : " + fActpayment);

System.out.println("Acutal payment is : " + fExpayment);

if (fActpayment.equals(fExpayment))
{

return "pass";
}
else
{

return "fail";
}
}

public static void enterValues(String fHvalue, String fLvalue, String fIRate, String fLmonth, String fLyear){
//enter home value
myd.findElement(By.name("param[homevalue]")).clear();
myd.findElement(By.name("param[homevalue]")).sendKeys(fHvalue);

//enter loan amount

myd.findElement(By.id("loanamt")).clear();
myd.findElement(By.id("loanamt")).sendKeys(fLvalue);

//enter interest rate

myd.findElement(By.id("intrstsrate")).clear();
myd.findElement(By.id("intrstsrate")).sendKeys(fIRate);

new Select(myd.findElement(By.name("param[start_month]"))).selectByVisibleText(fLmonth);
new Select(myd.findElement(By.name("param[start_year]"))).selectByVisibleText(fLyear);

}

public static String[][] readXL (String sPath, String iSheet) throws Exception{
String [][] xData;

File myxl = new File(sPath);
FileInputStream myStream = new FileInputStream(myxl);
HSSFWorkbook myWB = new HSSFWorkbook(myStream);
HSSFSheet mySheet = myWB.getSheet(iSheet); // Referring to 1st sheet
xRows = mySheet.getLastRowNum()+1;
xCols = mySheet.getRow(0).getLastCellNum();

xData = new String[xRows][xCols]; // Giving the size of the array
for (int i = 0; i < xRows; i++) {
HSSFRow row = mySheet.getRow(i);
for (int j = 0; j < xCols; j++) {
HSSFCell cell = row.getCell(j); // To read value from each col in each row
String value = cellToString(cell);
xData[i][j] = value;
System.out.print(value);
System.out.print("####");
}
System.out.println("");
}
return xData;
}

public static void writeXL(String sPath, String iSheet, String[][] xData) throws Exception {
System.out.println("Inside XL Write for Test Case");
File outFile = new File(sPath);
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet osheet = wb.createSheet("iSheet");
int xR_TS = xData.length;
int xC_TS = xData[0].length;
for (int myrow = 0; myrow < xR_TS; myrow++) {
HSSFRow row = osheet.createRow(myrow);
for (int mycol = 0; mycol < xC_TS; mycol++) {
HSSFCell cell = row.createCell(mycol);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellValue(xData[myrow][mycol]);
}
FileOutputStream fOut = new FileOutputStream(outFile);
wb.write(fOut);
fOut.flush();
fOut.close();
}
}

public static String cellToString(HSSFCell cell) {
// This function will convert an object of type excel cell to a string value
int type = cell.getCellType();
Object result;
switch (type) {
case HSSFCell.CELL_TYPE_NUMERIC: //0
result = cell.getNumericCellValue();
break;
case HSSFCell.CELL_TYPE_STRING: //1
result = cell.getStringCellValue();
break;
case HSSFCell.CELL_TYPE_FORMULA: //2
throw new RuntimeException("We can’t evaluate formulas in Java");
case HSSFCell.CELL_TYPE_BLANK: //3
result = "-";
break;
case HSSFCell.CELL_TYPE_BOOLEAN: //4
result = cell.getBooleanCellValue();
break;
case HSSFCell.CELL_TYPE_ERROR: //5
throw new RuntimeException ("This cell has an error");
default:
throw new RuntimeException("We don’t support this cell type: " + type);
}
return result.toString();
}
}

Below is my error:

Exception in thread "main" java.lang.NullPointerException
at selenium_feb2.DDF3.cellToString(DDF3.java:194)
at selenium_feb2.DDF3.readXL(DDF3.java:159)
at selenium_feb2.DDF3.main(DDF3.java:36)

Can you please reply why it is not working.

Thank you.

Forum Timezone: UTC -8

Most Users Ever Online: 70

Currently Online:
12 Guest(s)

Currently Browsing this Page:
1 Guest(s)

Top Posters:

Techhelp: 1940

Manu: 114

Shan Nathan: 80

Palanki sai charan: 67

Userone: 59

Vishal Anand: 53

Archana Sapre: 51

Geetha Reddy: 44

useL: 38

Kavya Vangala: 37

Member Stats:

Guest Posters: 19

Members: 8069

Moderators: 0

Admins: 1

Forum Stats:

Groups: 1

Forums: 22

Topics: 2124

Posts: 5829

Newest Members: Drashti Patel, Anirudh Kumar Vandhanapu, Jyothika Vuppunuthula, Enterprise HoldingsInc, Devasish kar, Anupama Sood, Thrilok Thrilok, HARISH PUTTA, Immanuel Michealraj, Sarvani Uppati

Administrators: Karthik eLearn (Admin): 49

0 Responses on Discussions"

Main Menu