-1

[but I am getting null pointer exception error][1]

My error in line protected class GetDataCountry extends AsyncTask<Void, Void, String> and my error in line List list=context.getElementsByTag("li"), I am using below code to get List Element from API: "https://www.fxexchangerate.com/currency-converter-rss-feed.html" by using jsoup I am fetching details but its throwing some exception.

protected class GetDataCountry extends AsyncTask<Void, Void, String> { // error in line

@Override
protected void onPreExecute() {
  super.onPreExecute();
  progressDialog = new ProgressDialog(MainActivity.this);
  progressDialog.setMessage("Fetching conntry data");
  progressDialog.setCancelable(false);
  progressDialog.show();
 }

        @Override
        protected String doInBackground(Void... voids) {
            StringBuffer response = null;
            try {
                String path = "https://www.fxexchangerate.com/currency-converter-rss-feed.html";
                URL url = new URL(path);
                HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                conn.setReadTimeout(15000);
                conn.setConnectTimeout(15000);
                conn.setRequestMethod("GET");

                int responseCode = conn.getResponseCode();
                if (responseCode == HttpsURLConnection.HTTP_OK) {
                    // Reading response from input Stream
                    BufferedReader in = new BufferedReader(
                            new InputStreamReader(conn.getInputStream()));
                    String output;
                    response = new StringBuffer();

                    while ((output = in.readLine()) != null) {
                        response.append(output);
                    }
                    in.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

            return response.toString();
        }

@Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            if (progressDialog.isShowing())
                progressDialog.dismiss();

            Document document =  Jsoup.parse(s);
            Element context =  document.getElementsByClass("rss").first();
            List<Element> list=context.getElementsByTag("li"); //error in line
            for (int i=0; i<list.size();i++)
            {
                String url=list.get(i).getElementsByTag("a").first().attr("href");
                String currencyName=list.get(i).text();
                listCountry.add(currencyName);
                listCountryCode.put(currencyName,url);
            }
        }
    }


  [1]: https://i.stack.imgur.com/1uQou.png
edward
  • 1
  • sorry I tried but it didn't fix the error, thank you. Do you have any other way with my error? – edward May 23 '22 at 06:44
  • The URL is no longer valid for the sample code, you should learn the idea instead of use the code directly. – AIMIN PAN May 23 '22 at 06:56
  • `sorry I tried but it didn't fix the error` it most definitely will solve your problem, all null pointers are the same, something is null when it shouldn't be, find out in your case why and what is null and avoid it – a_local_nobody May 23 '22 at 06:57
  • `document.getElementsByClass("rss").first()` is returning null here . Check the response first and [Debug](https://developer.android.com/studio/debug) your code line by line and find out why. i m sorry u need to debug it by yourself no one can help you with this . – ADM May 23 '22 at 06:57

0 Answers0