I made a parser application and wrote a cleaner function that should remove the first words if they are on the blacklist, but when I try to pass the value returned by this function, the application crashes. What to do? I attach the code and error logs below:
Code:
public class MainActivity extends AppCompatActivity implements MyRecyclerViewAdapter.ItemClickListener { MyRecyclerViewAdapter adapter; public Elements content; public ArrayList<String> titleList = new ArrayList<>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); new NewThread().execute(); new NewThread().execute(); RecyclerView recyclerView = findViewById(R.id.rv_list); LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this); recyclerView.setLayoutManager(linearLayoutManager); adapter = new MyRecyclerViewAdapter(this, titleList); adapter.setClickListener(this); recyclerView.setAdapter(adapter); } @Override public void onItemClick(View view, int position) { Toast.makeText(this, "You clicked " + adapter.getItem(position) + " on row number " + position, Toast.LENGTH_SHORT).show(); } public class NewThread extends AsyncTask<String, Void, String> { String cleaner(String a){ String source = a; String result = " "; ArrayList<String> arr = new ArrayList<>(); arr.add(source); for(String retrival: source.split(" ")){ arr.add(retrival); } arr.remove(0); for(int i = 0; i<arr.size(); i++){ if(arr.get(i).equals("Россия")||arr.get(i).equals("Екатеринбург")){ arr.remove(i); } result+=arr.get(i); } return result; } @Override protected String doInBackground(String... arg) { Document doc; try{ doc= Jsoup.connect("https://www.znak.com/?&%D0%B5%D0%BA%D0%B0%D1%82%D0%B5%D1%80%D0%B8%D0%BD%D0%B1%D1%83%D1%80%D0%B3%20%D0%BC%D1%83%D0%B7%D0%B5").get(); content = doc.select(".pub"); for (Element headline : doc.getElementsByTag("h5")) { Log.d("parse", headline.text()); } titleList.clear(); for(Element contents: content){ titleList.add(cleaner(contents.text())); } }catch (IOException e){ e.printStackTrace(); } return null; } @Override protected void onPostExecute(String result) { adapter.notifyDataSetChanged(); } } } Logs:
04-25 20:10:35.187 20033-20051/com.example.max.recyclerviewtesting E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1 Process: com.example.max.recyclerviewtesting, PID: 20033 java.lang.RuntimeException: An error occurred while executing doInBackground() at android.os.AsyncTask$3.done(AsyncTask.java:353) at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:383) at java.util.concurrent.FutureTask.setException(FutureTask.java:252) at java.util.concurrent.FutureTask.run(FutureTask.java:271) at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636) at java.lang.Thread.run(Thread.java:764) Caused by: java.lang.IndexOutOfBoundsException: Index: 6, Size: 6 at java.util.ArrayList.get(ArrayList.java:437) at com.example.max.recyclerviewtesting.MainActivity$NewThread.cleaner(MainActivity.java:73) at com.example.max.recyclerviewtesting.MainActivity$NewThread.doInBackground(MainActivity.java:98) at com.example.max.recyclerviewtesting.MainActivity$NewThread.doInBackground(MainActivity.java:53) at android.os.AsyncTask$2.call(AsyncTask.java:333) at java.util.concurrent.FutureTask.run(FutureTask.java:266) at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636) at java.lang.Thread.run(Thread.java:764) 04-25 20:10:35.412 20033-20054/com.example.max.recyclerviewtesting D/EGL_emulation: eglMakeCurrent: 0xa3c88340: ver 3 0 (tinfo 0xabb1ba30) 04-25 20:10:35.446 20033-20038/com.example.max.recyclerviewtesting I/zygote: Do partial code cache collection, code=60KB, data=45KB 04-25 20:10:35.449 20033-20038/com.example.max.recyclerviewtesting I/zygote: After code cache collection, code=60KB, data=45KB Increasing code cache capacity to 256KB