0

I wanted to start getting into web scraping so i made this very simple code for a currency converter. Here I have these lines of code but I keep getting a null pointer exception even though an output exists. I am basically going on to the site after inputting the currencies to and from and then saying the amount of currency then I am getting the text that is the output and saving it into a variable called Num, but it always says Num is null. What am I doing wrong and how do I fix it. Here is my website I am using. https://www.x-rates.com/calculator/?from=USD&to=EUR&amount=5

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URLEncoder;
import java.util.List;

import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlElement;
import com.gargoylesoftware.htmlunit.html.HtmlForm;
import com.gargoylesoftware.htmlunit.html.HtmlItalic;
import com.gargoylesoftware.htmlunit.html.HtmlOption;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.html.HtmlRadioButtonInput;
import com.gargoylesoftware.htmlunit.html.HtmlSelect;
import com.gargoylesoftware.htmlunit.html.HtmlSubmitInput;
import com.gargoylesoftware.htmlunit.html.HtmlTextArea;
import com.gargoylesoftware.htmlunit.html.HtmlTextInput;
import com.gargoylesoftware.htmlunit.javascript.host.dom.Document;

import java.util.Scanner;

import org.jsoup.Jsoup;

public class SwissArmyKnife {
   public static void main(String[] Args) {
       System.out.println(Converter());

   }
   public static String Converter() {
       String From = " ";
       String To = " ";
       boolean again = true;
       while(again) {
       Scanner scf = new Scanner(System.in);
       System.out.println("Which currency should it be converted from? A. Euro B. USD C. Pound D. Rupee E. Canadian Dollar F. Austalian Dollar");
       String Answer = scf.next();
       if(Answer.equals("A")|| Answer.contentEquals("a")) {
           From = "EUR";
           again = false;
       }else if(Answer.equals("B")|| Answer.contentEquals("b")){
           From = "USD";
           again = false;
       }else if(Answer.equals("C")|| Answer.contentEquals("c")){
           From = "GBP";
           again = false;
       }else if(Answer.equals("D")|| Answer.contentEquals("d")){
           From = "INR";
           again = false;
       }else if(Answer.equals("E")|| Answer.contentEquals("e")){
           From = "CAD";
           again = false;
       }else if(Answer.equals("B")|| Answer.contentEquals("b")){
           From = "AUD";
           again = false;
       }else {
           System.out.println("Sorry, Incorrect input");
       }
       }
       again = true;
       while(again) {
           Scanner scf = new Scanner(System.in);
           System.out.println("Which currency should it be converted to? A. Euro B. USD C. Pound D. Rupee E. Canadian Dollar F. Austalian Dollar");
           String Answer = scf.next();
           if(Answer.equals("A")|| Answer.contentEquals("a")) {
               To = "EUR";
               again = false;
           }else if(Answer.equals("B")|| Answer.contentEquals("b")){
               To = "USD";
               again = false;
           }else if(Answer.equals("C")|| Answer.contentEquals("c")){
               To = "GBP";
               again = false;
           }else if(Answer.equals("D")|| Answer.contentEquals("d")){
               To = "INR";
               again = false;
           }else if(Answer.equals("E")|| Answer.contentEquals("e")){
               To = "CAD";
               again = false;
           }else if(Answer.equals("B")|| Answer.contentEquals("b")){
               To = "AUD";
               again = false;
           }else {
               System.out.println("Sorry, Incorrect input");
           }
           }
       again = true;
       float Amount= 0f;
       while(again) {
           Scanner scf = new Scanner(System.in);
           System.out.println("How much of the currency should be converted?");
           String Answer = scf.next();

           try {
               Amount = Float.parseFloat(Answer); 
               again = false;
           }catch(Exception e) {
               System.out.println("Error: "+ e);
               again = true;

       }
       }
       WebClient client = new WebClient();
       client.getOptions().setCssEnabled(false);
       client.getOptions().setJavaScriptEnabled(false);
       try {
         String Amounts = Float.toString(Amount);
         String searchUrl = "https://www.x-rates.com/calculator/?from="+URLEncoder.encode(From, "UTF-8")+ "&to="+URLEncoder.encode(To, "UTF-8")+"amount="+URLEncoder.encode(Amounts, "UTF-8");
         HtmlPage page = client.getPage(searchUrl);
         HtmlElement Num = ((HtmlElement) page.getFirstByXPath(".//p/span[@class='ccOutputRslt']")) ;
         //System.out.println(page.asXml());

         return Num.asText();
       }catch(Exception e){
         e.printStackTrace();
         return "Whoops!";
       }

   }   


}
  • Please attach log with number of the string of your code where NPE was thrown – Egor May 28 '20 at 14:41
  • java.lang.NullPointerException at SwissArmyKnife.Converter(SwissArmyKnife.java:113) at SwissArmyKnife.main(SwissArmyKnife.java:27) – Pixelgost May 28 '20 at 15:18
  • Your XPath expression is wrong. It should be ".//span[@class='ccOutputRslt']". There is no

    tag at all on the page. In Google Chrome you can easily test your XPath expression before you use it in your application. Open the website of interest. Press F12 to open the developer tools, Select Console at the bottom and enter: $x(".//p/span[@class='ccOutputRslt']"). You will see all matches within the active web page. With $x you can execute any XPath expression on the active page. ".//p/span[@class='ccOutputRslt']"

    – rmunge May 28 '20 at 16:51
  • Thanks, that worked! Only problem is the TextContent of the element is set to 0 no matter what value i put in. – Pixelgost May 28 '20 at 17:22

0 Answers0