Search code examples
javaandroidjsoup

Why do I get a Fatal Exception when I click on the button in my app that tells me if I should bring an umbrella today?


I am making an Android app using Java and I get a Fatal Exception when I click on the button. The idea of the code is (using the jsoup library) to look up the chance to rain today and if the percentage is greater than 50% then it says yes and if it less than that it says no.
Here is the code

enterpackage com.example.doineedanumbrellatoday;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import android.view.View;
import android.widget.*;
import java.io.IOException;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
TextView txtview;
Button button;

    public MainActivity() throws IOException {
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        txtview = (TextView)findViewById(R.id.text);
        button = (Button)findViewById(R.id.button);
        button.setOnClickListener(this);
    }


    @Override
    public void onClick(View view) {
        if(view.getId()==R.id.button){
            Document doc= null;
            try {
                doc = Jsoup.connect("https://dalivali.bg/?location=222").timeout(6000).get();

            } catch (IOException e) {
                e.printStackTrace();
            }
        org.jsoup.select.Elements rain = doc.select("span.rain-num");
        //System.out.println(rain);
        char[] chars2 = rain.toString().toCharArray();
        StringBuilder chance = new StringBuilder();

        for(char c : chars2){
            if(Character.isDigit(c)){
                chance.append(c);
            }
        }
        if(Integer.parseInt(chance.toString())>=50) {
            txtview.setText("Yes");
        } else {
            txtview.setText("No");
        }
    }
}



} 

Could someone tell me why I get an exception?


Solution

  • As Gabe Sechan suggested, you have to run the connection of JSoup code in a background thread so your UI doesn't freeze and then you have to start a UI thread inside a background thread because you can't access views like TextView inside a background thread. So, you can start a background thread for the connection and UI thread for setting of texts like this:

            Thread thread = new Thread() {
                @Override
                public void run() {
    
                    Document document = null;
    
                    try {
                        document = Jsoup.connect("https://dalivali.bg/?location=222").timeout(6000).get();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
    
                    assert document != null;
                    org.jsoup.select.Elements rain = document.select("span.rain-num");
    
                    char[] chars2 = rain.toString().toCharArray();
                    StringBuilder chance = new StringBuilder();
    
                    for (char c : chars2) {
                        if (Character.isDigit(c)) {
                            chance.append(c);
                        }
                    }
    
                    runOnUiThread(() -> {
                        if (Integer.parseInt(chance.toString()) >= 50)
                            txtview.setText("Yes");
                        else
                            txtview.setText("No");
                    });
                }
            };
    
            thread.start();