Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference , while using evaluateJavascript

119 views Asked by At

im trying to get some string return from a webview evalueteJavascript value

idk what is wron here it was perfectly working till yesterday , i had made no changes to this code , but dont know why its giving me this error

Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference

im not even using any bool here , im a little confused what is actually wrong here , please can anyone helps me out

the Main Nav methhod has to evaluateJavascript where the first one checks if the threads page is valid or not or broken

means the var brokenPage = document.querySelector('html[id=instagram]'); checks of the page contains a elemnt that mostly appears for a broken page or a private profile

this two

 "var splashScreenDark = document.querySelector('html[class=" + selectorsdark + "]');" +
                            "var splashScreenLite = document.querySelector('html[class=" + selectorslite + "]');" +

checks if the page is valid and is not a private account or an unexpected page thats not a threads webpage

the seocnd evalute javascript has one main elemnt check that diffrentiate between a comment page and post page

if document.querySelector('[data-pressable-container=true]'); is true it means its a comment page and if false its a post page

public void linkValidationAndUrl(String url) throws IOException {
        textStatus.setTextColor(ContextCompat.getColor(MainActivity.this, R.color.white));
        textStatus.setText(getString(R.string.validating_link));
        if (url.startsWith("https://www.threads.net") || url.startsWith("https://threads.net")) {
            MainNav(url, new AccountStatusCallback() {
                @Override
                public void onAccountStatusReceived(String[] accountStatus) {
                    // Regex pattern for profile link
                    Pattern profileLinkPattern = Pattern.compile("^https://(?:www\\.)?threads\\.net/@[^/]+$");
                    Pattern post1Pattern = Pattern.compile("(?<=.net\\/t\\/).*(?=\\/\\?)|(?<=post\\/).*");
                    Matcher profileMatcher = profileLinkPattern.matcher(url);
                    Matcher post1Matcher = post1Pattern.matcher(url);
                    if (accountStatus[0].equals("true")) {
                        textStatus.setText(null);
                        Toast.makeText(getApplicationContext(), "Broken Url or Private Profile", Toast.LENGTH_SHORT).show();
                        textStatus.setTextColor(ContextCompat.getColor(MainActivity.this, R.color.orange));
                        textStatus.setText(getString(R.string.brokenorprivateStr));
                    } else if (accountStatus[0].equals("unexpected")) {
                        textStatus.setText(null);
                        Toast.makeText(getApplicationContext(), "Unexpected Error", Toast.LENGTH_SHORT).show();
                        textStatus.setTextColor(ContextCompat.getColor(MainActivity.this, R.color.orange));
                        textStatus.setText(getString(R.string.unexpected_error));
                    } else if (accountStatus[0].equals("false")) {
                        if (profileMatcher.matches()) {
                            textStatus.setText(null);
                            textStatus.setText(getString(R.string.profile_link));
                            Intent iPr = new Intent(getApplicationContext(), ProfileActivity.class);
                            startActivity(iPr);

                        } else if (post1Matcher.find()) {
                            textStatus.setText(null);
                            if (accountStatus[1].equals("true")) {
                                //Navigate to comment page
                                textStatus.setText("Comment Link Provided");
                                Intent iC = new Intent(getApplicationContext(), CommentActivity.class);
                                startActivity(iC);
                            } else if (accountStatus[1].equals("false")) {
                                // Navigate to post page
                                textStatus.setText("Post Link Provided");
                                Intent iP = new Intent(getApplicationContext(), PostActivity.class);
                                startActivity(iP);
                            } else if (accountStatus[1].equals("unexpected")) {
                                textStatus.setText(null);
                                Toast.makeText(getApplicationContext(), "Unexpected Error", Toast.LENGTH_SHORT).show();
                                textStatus.setTextColor(ContextCompat.getColor(MainActivity.this, R.color.orange));
                                textStatus.setText(getString(R.string.unexpected_error));

                            }
                        } else {
                            textStatus.setText(null);
                            textStatus.setTextColor(ContextCompat.getColor(MainActivity.this, R.color.orange));
                            textStatus.setText(getString(R.string.threadlink_invaliddataurl));
                        }
                    }else {
                        textStatus.setText(null);
                        textStatus.setTextColor(ContextCompat.getColor(MainActivity.this, R.color.orange));
                        textStatus.setText("Unable To Get Data");
                    }
                }

            });


        } else if ((!url.startsWith("https://")) && (url.contains("www.threads.net") || url.contains("threads.net"))) {

            textStatus.setTextColor(ContextCompat.getColor(MainActivity.this, R.color.orange));
            textStatus.setText(getString(R.string.threadLink_NoHttps));


        } else {
            Toast.makeText(MainActivity.this, "Invailed Url", Toast.LENGTH_SHORT).show();
            textStatus.setText(null);
            textStatus.setTextColor(ContextCompat.getColor(MainActivity.this, R.color.orange));
            textStatus.setText(getString(R.string.threadLink_False));
        }

    }

    public void MainNav(String profileUrl, AccountStatusCallback callback) {
        final String[] AccountAndCommentStatus = {null, null};
        String selectorsdark = "\"_9dls __fb-dark-mode\"";
        String selectorslite = "\"_9dls __fb-light-mode\"";
        AtomicInteger evaluationCounter = new AtomicInteger(); // Define the evaluationCounter here
        webView.loadUrl(profileUrl);
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageFinished(WebView view, String url) {
                // Delay the JavaScript execution by 2000 milliseconds (2 seconds)
                // Execute JavaScript to get the account status
                new Handler().postDelayed(() -> {
                    webView.evaluateJavascript("javascript: var brokenPage = document.querySelector('html[id=instagram]');\n" +
                            "var splashScreenDark = document.querySelector('html[class=" + selectorsdark + "]');" +
                            "var splashScreenLite = document.querySelector('html[class=" + selectorslite + "]');" +
                            "var pageAvailability = 'null';\n" +
                            "if (brokenPage) {\n" +
                            "    pageAvailability = 'true';\n" +
                            "}else if (splashScreenDark || splashScreenLite){\n" +
                            "    pageAvailability = 'false'\n" +
                            "}else{pageAvailability = 'null'}\n" +
                            "pageAvailability;", value -> {
                        if (value != null) {
                            // Remove the double quotes around the value
                            value = value.replace("\"", "");

                            switch (value) {
                                case "true":
                                    AccountAndCommentStatus[0] = "true";
                                    break;
                                case "false":
                                    AccountAndCommentStatus[0] = "false";
                                    break;
                                case "null":
                                    AccountAndCommentStatus[0] = "unexpected";
                                    break;
                            }

                            // Increase the evaluation counter
                            evaluationCounter.getAndIncrement();

                            // Check if both evaluations are done
                            if (evaluationCounter.get() == 2 && callback != null) {
                                callback.onAccountStatusReceived(AccountAndCommentStatus);
                            }
                        }else {
                            Toast.makeText(getApplicationContext(),"Unexpected Error Main Page",Toast.LENGTH_LONG).show();
                        }
                    });
                }, 2000);
                new Handler().postDelayed(() -> {
                    // Execute JavaScript to get the comment status
                    webView.evaluateJavascript("javascript: var pageAvail = document.querySelector('html[id=instagram]');\n" +
                            "var commentExist = 'null';\n" +
                            "if (!pageAvail) {\n" +
                            "\n" +
                            "    var firstData = document.querySelector('[data-pressable-container=true]');\n" +
                            "    if (firstData) {\n" +
                            "        var repliesElement = firstData.querySelector('svg[aria-hidden=true][fill=none]');\n" +
                            "        if (repliesElement) {\n" +
                            "            commentExist = 'true';\n" +
                            "        } else { commentExist = 'false';}\n" +
                            "    }\n" +
                            "}\n" +
                            "commentExist;\n", value -> {
                        if (value != null) {
                            // Remove the double quotes around the value
                            value = value.replace("\"", "");

                            switch (value) {
                                case "true":
                                    AccountAndCommentStatus[1] = "true";
                                    break;
                                case "false":
                                    AccountAndCommentStatus[1] = "false";
                                    break;
                                case "null":
                                    AccountAndCommentStatus[1] = "unexpected";
                                    break;
                            }

                            // Increase the evaluation counter
                            evaluationCounter.getAndIncrement();

                            // Check if both evaluations are done
                            if (evaluationCounter.get() == 2 && callback != null) {
                                callback.onAccountStatusReceived(AccountAndCommentStatus);
                            }
                        }else {
                            Toast.makeText(getApplicationContext(),"Unexpected Error Post/Comment Page",Toast.LENGTH_LONG).show();
                        }
                    });
                }, 4000);

            }
        });
    }


    public interface AccountStatusCallback {
        void onAccountStatusReceived(String[] accountStatus);
    }
0

There are 0 answers