Jump to content

C# - NullReferenceException was unhandled

AlTech
Go to solution Solved by squirrl,
Just now, AluminiumTech said:

That's not what fixed the issue though.........

 

I tried playing around and changed a few lines of code and I had no more issues. So idk what happened.

 

Ah well, If the issue wasn't in the code that you posted then there's no way we would have found it then. :P

2 minutes ago, AluminiumTech said:

 


if(OnTask_AllowedExtension.Contains(URL) & OnTask_AllowedWebsites.Contains(URL))

 

Normally you'll want to use && and || over & and | because they use short-circuit evaluation.

 

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, AluminiumTech said:

or actually I think I can simplify that further.

 


if(OnTask_AllowedExtension.Contains(URL) & OnTask_AllowedWebsites.Contains(URL))
   {
//Do something here
   }
else if(OnTask_NotAllowedWebsites.Contains(URL))
{
  //Do something here
}

 

 

That changes the logic and introduces another issue. if you're setting onTask to false in the last if statement, you're not changing anything since it's false to start with. You could remove the whole last if statement and have the same functionality.

 

What exactly is the functionality that you want here with the allowed and not allowed URLs?

Aragorn (WS): 250D | 6800k | 840 Pro 512GB | Intel 530 480GB  | Asus X99-M WS | 64GB DDR4 | Corsair HX720i | GTX 1070 | Corsair H115i | Philips BDM4350UC 43" 3840x2160 IPS

Gimli (server):  Node 304 | G4560 | ADATA XPG SX8000 128GB | 2x 5TB WD Red | ASROCK H270M-ITX/AC  | 8GB DDR4 | Seasonic 400FL

 Omega (server):                 Fractal Arc Mini R2 | i3 4130 | 500GB Maxtor | 2TB WD Red : Raid 1 | 3TB Seagate Barracuda | 16GB RAM | Seasonic G-450w
Alpha (WS): 900D | 4770k | GTX 780  | 840 Pro 512GB  | GA-Z87X-OC | Corsair RM 850 | 24GB 2400mhz | Samsung S27B970D 2560x1440

                              ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, squirrl said:

That changes the logic and introduces another issue. if you're setting onTask to false in the last if statement, you're not changing anything since it's false to start with. You could remove the whole last if statement and have the same functionality.

 

What exactly is the functionality that you want here with the allowed and not allowed URLs?

AHHHH. I'm getting false positives lol.

 

It's telling me it's not allowed even if it is allowed.

Judge a product on its own merits AND the company that made it.

How to setup MSI Afterburner OSD | How to make your AMD Radeon GPU more efficient with Radeon Chill | (Probably) Why LMG Merch shipping to the EU is expensive

Oneplus 6 (Early 2023 to present) | HP Envy 15" x360 R7 5700U (Mid 2021 to present) | Steam Deck (Late 2022 to present)

 

Mid 2023 AlTech Desktop Refresh - AMD R7 5800X (Mid 2023), XFX Radeon RX 6700XT MBA (Mid 2021), MSI X370 Gaming Pro Carbon (Early 2018), 32GB DDR4-3200 (16GB x2) (Mid 2022

Noctua NH-D15 (Early 2021), Corsair MP510 1.92TB NVMe SSD (Mid 2020), beQuiet Pure Wings 2 140mm x2 & 120mm x1 (Mid 2023),

Link to comment
Share on other sites

Link to post
Share on other sites

3 minutes ago, madknight3 said:

Normally you'll want to use && and || over & and | because they use short-circuit evaluation.

 

Ok. But it's telling me now that some strings which are "Allowed" are "not allowed"

Judge a product on its own merits AND the company that made it.

How to setup MSI Afterburner OSD | How to make your AMD Radeon GPU more efficient with Radeon Chill | (Probably) Why LMG Merch shipping to the EU is expensive

Oneplus 6 (Early 2023 to present) | HP Envy 15" x360 R7 5700U (Mid 2021 to present) | Steam Deck (Late 2022 to present)

 

Mid 2023 AlTech Desktop Refresh - AMD R7 5800X (Mid 2023), XFX Radeon RX 6700XT MBA (Mid 2021), MSI X370 Gaming Pro Carbon (Early 2018), 32GB DDR4-3200 (16GB x2) (Mid 2022

Noctua NH-D15 (Early 2021), Corsair MP510 1.92TB NVMe SSD (Mid 2020), beQuiet Pure Wings 2 140mm x2 & 120mm x1 (Mid 2023),

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, AluminiumTech said:

Ok. But it's telling me now that some strings which are "Allowed" are "not allowed"

Read what I've been posting above. What are you trying to achieve with having a blacklist and whitelist at the same time? And what should happen to URLs which aren't on either list?

Aragorn (WS): 250D | 6800k | 840 Pro 512GB | Intel 530 480GB  | Asus X99-M WS | 64GB DDR4 | Corsair HX720i | GTX 1070 | Corsair H115i | Philips BDM4350UC 43" 3840x2160 IPS

Gimli (server):  Node 304 | G4560 | ADATA XPG SX8000 128GB | 2x 5TB WD Red | ASROCK H270M-ITX/AC  | 8GB DDR4 | Seasonic 400FL

 Omega (server):                 Fractal Arc Mini R2 | i3 4130 | 500GB Maxtor | 2TB WD Red : Raid 1 | 3TB Seagate Barracuda | 16GB RAM | Seasonic G-450w
Alpha (WS): 900D | 4770k | GTX 780  | 840 Pro 512GB  | GA-Z87X-OC | Corsair RM 850 | 24GB 2400mhz | Samsung S27B970D 2560x1440

                              ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, AluminiumTech said:

Ok. But it's telling me now that some strings which are "Allowed" are "not allowed"

If you just want to replace the exact same functionality, try the loop I suggested.  I don't want to discourage you from going for a better option if one exists in this language for your scenario, but it's always there to fall back on :)

Solve your own audio issues  |  First Steps with RPi 3  |  Humidity & Condensation  |  Sleep & Hibernation  |  Overclocking RAM  |  Making Backups  |  Displays  |  4K / 8K / 16K / etc.  |  Do I need 80+ Platinum?

If you can read this you're using the wrong theme.  You can change it at the bottom.

Link to comment
Share on other sites

Link to post
Share on other sites

5 minutes ago, AluminiumTech said:

Ok. But it's telling me now that some strings which are "Allowed" are "not allowed"

Sorry, I was merely providing some information on the operators, not checking to see if your logic was correct or not.

 

Since there have been some changes, how about you post the full OnTask class again.

Link to comment
Share on other sites

Link to post
Share on other sites

33 minutes ago, madknight3 said:

Sorry, I was merely providing some information on the operators, not checking to see if your logic was correct or not.

 

Since there have been some changes, how about you post the full OnTask class again.

   class OnTask
    {
        Dialog dialog = new Dialog();

        public string URL;
        public bool IsOnTask;

        public string[] OnTask_AllowedWebsites = new string[11];
        public string[] OnTask_NotAllowedWebsites = new string[22];
        public string[] OnTask_AllowedExtension = new string[8];

        //Check if the website is in the allowed list
        public void AssignValues()
        {
            OnTask_AllowedWebsites[0] = "onedrive";
            OnTask_AllowedWebsites[1] = "google";
            OnTask_AllowedWebsites[2] = "icloud";
            OnTask_AllowedWebsites[3] = "youtube";
            OnTask_AllowedWebsites[4] = "dropbox";
            OnTask_AllowedWebsites[5] = "microsoft";
            OnTask_AllowedWebsites[6] = "bing";
            OnTask_AllowedWebsites[7] = "gmail";
            OnTask_AllowedWebsites[8] = "linkedin";
            OnTask_AllowedWebsites[9] = "stackoverflow";
            OnTask_AllowedWebsites[10] = "slack";

            OnTask_NotAllowedWebsites[0] = "steampowered";
            OnTask_NotAllowedWebsites[1] = "hub";
            OnTask_NotAllowedWebsites[2] = "amazon";
            OnTask_NotAllowedWebsites[3] = "ebay";
            OnTask_NotAllowedWebsites[4] = "spotify";
            OnTask_NotAllowedWebsites[5] = "linustechtips";
            OnTask_NotAllowedWebsites[6] = "apple";
            OnTask_NotAllowedWebsites[7] = "xbox";
            OnTask_NotAllowedWebsites[8] = "wccftech";
            OnTask_NotAllowedWebsites[9] = "fake";
            OnTask_NotAllowedWebsites[10] = ".con";
            OnTask_NotAllowedWebsites[11] = ".cm";
            OnTask_NotAllowedWebsites[12] = "skype";
            OnTask_NotAllowedWebsites[13] = "ad";
            OnTask_NotAllowedWebsites[14] = "game";
            OnTask_NotAllowedWebsites[15] = "facebook";
            OnTask_NotAllowedWebsites[16] = "instagram";
            OnTask_NotAllowedWebsites[17] = "twitter";
            OnTask_NotAllowedWebsites[18] = "wikipedia";
            OnTask_NotAllowedWebsites[19] = "yahoo";
            OnTask_NotAllowedWebsites[20] = "tube";
            OnTask_NotAllowedWebsites[21] = "red";

            OnTask_AllowedExtension[0] = ".edu";
            OnTask_AllowedExtension[1] = ".org";
            OnTask_AllowedExtension[2] = ".com";
            OnTask_AllowedExtension[3] = ".lithium";
            OnTask_AllowedExtension[4] = ".html";
            OnTask_AllowedExtension[5] = ".info";
            OnTask_AllowedExtension[6] = ".net";
            OnTask_AllowedExtension[7] = ".aspx";
        }

        public void OnTask_TempNavigate()
        {
            AssignValues();

            if (OnTask_AllowedExtension.Contains(URL) && OnTask_AllowedWebsites.Contains(URL))
            {
                IsOnTask = true;
            }
           if (OnTask_NotAllowedWebsites.Contains(URL))
            {
                IsOnTask = false;
           }
        }
    }

Here it is.

Judge a product on its own merits AND the company that made it.

How to setup MSI Afterburner OSD | How to make your AMD Radeon GPU more efficient with Radeon Chill | (Probably) Why LMG Merch shipping to the EU is expensive

Oneplus 6 (Early 2023 to present) | HP Envy 15" x360 R7 5700U (Mid 2021 to present) | Steam Deck (Late 2022 to present)

 

Mid 2023 AlTech Desktop Refresh - AMD R7 5800X (Mid 2023), XFX Radeon RX 6700XT MBA (Mid 2021), MSI X370 Gaming Pro Carbon (Early 2018), 32GB DDR4-3200 (16GB x2) (Mid 2022

Noctua NH-D15 (Early 2021), Corsair MP510 1.92TB NVMe SSD (Mid 2020), beQuiet Pure Wings 2 140mm x2 & 120mm x1 (Mid 2023),

Link to comment
Share on other sites

Link to post
Share on other sites

26 minutes ago, AluminiumTech said:

Here it is.

My guess is the problem is what URL contains. In order for Contains(URL) to return true, there has to be an exact match. You can't have a string that matches a value in both the OnTask_AllowedExtension and OnTask_AllowedWebsites arrays.

// Example
URL = "www.google.com"
OnTask_AllowedExtension.Contains(URL) // returns false
// because "www.google.com" is not in the array
// ".com" != "www.google.com"

 

 

Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, madknight3 said:

My guess is the problem is what URL contains. In order for Contains(URL) to return true, there has to be an exact match. You can't have a string that matches a value in both the OnTask_AllowedExtension and OnTask_AllowedWebsites arrays.


// Example
URL = "www.google.com"
OnTask_AllowedExtension.Contains(URL) // returns false
// because "www.google.com" is not in the array
// ".com" != "www.google.com"

 

 

But I only asked for it to look for "Google" in the array. It doesn't look for "Google.com"

Judge a product on its own merits AND the company that made it.

How to setup MSI Afterburner OSD | How to make your AMD Radeon GPU more efficient with Radeon Chill | (Probably) Why LMG Merch shipping to the EU is expensive

Oneplus 6 (Early 2023 to present) | HP Envy 15" x360 R7 5700U (Mid 2021 to present) | Steam Deck (Late 2022 to present)

 

Mid 2023 AlTech Desktop Refresh - AMD R7 5800X (Mid 2023), XFX Radeon RX 6700XT MBA (Mid 2021), MSI X370 Gaming Pro Carbon (Early 2018), 32GB DDR4-3200 (16GB x2) (Mid 2022

Noctua NH-D15 (Early 2021), Corsair MP510 1.92TB NVMe SSD (Mid 2020), beQuiet Pure Wings 2 140mm x2 & 120mm x1 (Mid 2023),

Link to comment
Share on other sites

Link to post
Share on other sites

8 minutes ago, AluminiumTech said:

But I only asked for it to look for "Google" in the array. It doesn't look for "Google.com"

It doesn't matter as the logic is still wrong. Here's the example worked out

URL = "Google"

if (OnTask_AllowedExtension.Contains(URL) && OnTask_AllowedWebsites.Contains(URL)) // doesn't get entered
// because OnTask_AllowedExtension.Contains(URL) && OnTask_AllowedWebsites.Contains(URL) doesn't evaluate to true
  
OnTask_AllowedExtension.Contains(URL) // returns false. OnTask_AllowedExtension doesn't contain "Google"
// and
OnTask_AllowedWebsites.Contains(URL) // also returns false because it also doesn't contain "Google".
// Remember, string comparison is case sensitive so "Google" != "google"
  
// even if you used URL = "google"
// OnTask_AllowedExtension.Contains(URL) && OnTask_AllowedWebsites.Contains(URL) still evaluates to false
// because false && true == false

 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×