Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. I having this error could anyone can help me :Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object at ValidParenthese.Solution.IsValid (System.String s) [0x00000] in <89120c6f256647e6a05011f274f6a

I having this error could anyone can help me :Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object at ValidParenthese.Solution.IsValid (System.String s) [0x00000] in <89120c6f256647e6a05011f274f6a

Scheduled Pinned Locked Moved C#
helpcsharpdatabaselinq
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    swethamaddi
    wrote on last edited by
    #1

    using System;
    using System.Collections.Generic;
    using System.Linq;

    namespace ValidParenthese {

    public class Solution {
        public bool IsValid(string s) {
            var result = s.ToCharArray().ToList();
            bool value = true;
    
            for (int i = 0; i < result.Count; i++) {
    
                if (result\[i\].ToString().Equals("(")) {
    
                    if (result\[i + 1\].ToString().Equals(")")) {
                        value = true;
                    } else {
                        var count = result.Count;
    
                        if (count >= 3) {
    
                            var index = result\[i + 1\];
    
                            if (result\[i + 2\].ToString().Equals(")")) {
                                value = false;
                            } else if ((index.ToString().Equals("{") && result\[i + 2\].ToString().Equals("}"))
                                || (index.ToString().Equals("\[") && result\[i + 2\].ToString().Equals("\]"))) {
    
                                if (result\[i + 3\].ToString().Equals(")")) {
                                    value = true;
                                } else {
                                    var ind = result\[i + 2\];
    
                                    if ((ind.ToString().Equals("{") && result\[i + 3\].ToString().Equals("}"))
                                        || (ind.ToString().Equals("\[") && result\[i + 3\].ToString().Equals("\]"))) {
    
                                        if ((index.ToString().Equals("{") && result\[i + 4\].ToString().Equals("}"))
                                            || (index.ToString().Equals("\[") && result\[i + 4\].ToString().Equals("\]"))) {
                                            if (result\[i + 5\].ToString().Equals(")")) {
                                                value = true;
                                            } else {
                                                value = false;
                                            }
                                        } else {
                                            value = false;
                                        }
                                    } else {
                                        value = false;
                                    }
                                }
                            }
    
                        } else {
                            value = false;
                        }
    
    
                    }
    
                } else if (result\[
    
    L D 2 Replies Last reply
    0
    • S swethamaddi

      using System;
      using System.Collections.Generic;
      using System.Linq;

      namespace ValidParenthese {

      public class Solution {
          public bool IsValid(string s) {
              var result = s.ToCharArray().ToList();
              bool value = true;
      
              for (int i = 0; i < result.Count; i++) {
      
                  if (result\[i\].ToString().Equals("(")) {
      
                      if (result\[i + 1\].ToString().Equals(")")) {
                          value = true;
                      } else {
                          var count = result.Count;
      
                          if (count >= 3) {
      
                              var index = result\[i + 1\];
      
                              if (result\[i + 2\].ToString().Equals(")")) {
                                  value = false;
                              } else if ((index.ToString().Equals("{") && result\[i + 2\].ToString().Equals("}"))
                                  || (index.ToString().Equals("\[") && result\[i + 2\].ToString().Equals("\]"))) {
      
                                  if (result\[i + 3\].ToString().Equals(")")) {
                                      value = true;
                                  } else {
                                      var ind = result\[i + 2\];
      
                                      if ((ind.ToString().Equals("{") && result\[i + 3\].ToString().Equals("}"))
                                          || (ind.ToString().Equals("\[") && result\[i + 3\].ToString().Equals("\]"))) {
      
                                          if ((index.ToString().Equals("{") && result\[i + 4\].ToString().Equals("}"))
                                              || (index.ToString().Equals("\[") && result\[i + 4\].ToString().Equals("\]"))) {
                                              if (result\[i + 5\].ToString().Equals(")")) {
                                                  value = true;
                                              } else {
                                                  value = false;
                                              }
                                          } else {
                                              value = false;
                                          }
                                      } else {
                                          value = false;
                                      }
                                  }
                              }
      
                          } else {
                              value = false;
                          }
      
      
                      }
      
                  } else if (result\[
      
      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      See my answer to your original question on this subject.

      1 Reply Last reply
      0
      • S swethamaddi

        using System;
        using System.Collections.Generic;
        using System.Linq;

        namespace ValidParenthese {

        public class Solution {
            public bool IsValid(string s) {
                var result = s.ToCharArray().ToList();
                bool value = true;
        
                for (int i = 0; i < result.Count; i++) {
        
                    if (result\[i\].ToString().Equals("(")) {
        
                        if (result\[i + 1\].ToString().Equals(")")) {
                            value = true;
                        } else {
                            var count = result.Count;
        
                            if (count >= 3) {
        
                                var index = result\[i + 1\];
        
                                if (result\[i + 2\].ToString().Equals(")")) {
                                    value = false;
                                } else if ((index.ToString().Equals("{") && result\[i + 2\].ToString().Equals("}"))
                                    || (index.ToString().Equals("\[") && result\[i + 2\].ToString().Equals("\]"))) {
        
                                    if (result\[i + 3\].ToString().Equals(")")) {
                                        value = true;
                                    } else {
                                        var ind = result\[i + 2\];
        
                                        if ((ind.ToString().Equals("{") && result\[i + 3\].ToString().Equals("}"))
                                            || (ind.ToString().Equals("\[") && result\[i + 3\].ToString().Equals("\]"))) {
        
                                            if ((index.ToString().Equals("{") && result\[i + 4\].ToString().Equals("}"))
                                                || (index.ToString().Equals("\[") && result\[i + 4\].ToString().Equals("\]"))) {
                                                if (result\[i + 5\].ToString().Equals(")")) {
                                                    value = true;
                                                } else {
                                                    value = false;
                                                }
                                            } else {
                                                value = false;
                                            }
                                        } else {
                                            value = false;
                                        }
                                    }
                                }
        
                            } else {
                                value = false;
                            }
        
        
                        }
        
                    } else if (result\[
        
        D Offline
        D Offline
        Dave Kreskowiak
        wrote on last edited by
        #3

        Hint: If you have to call .ToString more than twice in a single method, your design is showing signs of being flawed to the point of being unsalvageable and needs to be scrapped and your approach to the problem redesigned from scratch.

        Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
        Dave Kreskowiak

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups