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. C# Exception: An object reference does not point to an instance of an objec

C# Exception: An object reference does not point to an instance of an objec

Scheduled Pinned Locked Moved C#
csharpvisual-studiocomdebuggingworkspace
10 Posts 8 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.
  • M Offline
    M Offline
    Member_15583139
    wrote on last edited by
    #1

    Quote:

    IDE: Integrated development environment C#, Visual Studio 2019, Windows10Pro in 2.1

    Exception: An object reference does not point to an instance of an object.

    Quote:

    using System; using System.IO; using System.Text; // - using System.Text.StringBuilder; using System.Globalization; using System.Windows.Forms; using System.Threading; // using Microsoft.CSharp; namespace WFA_BankPercent { public partial class Form7 : Form { public Form7() { InitializeComponent(); } //- string m_sWork44 = ""; // C# требует инициализации private void Form7_Load(object sender, EventArgs e) { } // below on forum www.codeproject.com 29032022 private void button21_Click(object sender, EventArgs e) { string line = System.String.Empty; string s_sourse = System.String.Empty; bool b_beforeRead = false; bool b_afterRead = false; string s_begin = "Begin"; string s_end = "End"; bool b_begin = false; bool b_end = false; int i_begin = 0; int i_end = 0; try { StreamReader sr = new StreamReader(@"D:\Input_my.txt"); StreamWriter sw = new StreamWriter("D:\\Out_my.txt", false, Encoding.UTF8); line = sr.ReadLine(); byte k = 0; // temporary for debug while (line != null) { // k++; // temporary // //+if (k > 6) // read only the first 6 lines of the input file ////+ if (k > 17) // 15 lines were written to the output file, but there was no Exception // if (k > 18) // 0 lines were written to the output file, Exception was // goto Close_my; if ((line.Trim().Length == 0)) goto label_read; b_begin = line.Contains(s_begin); b_end = line.Contains(s_end); if (b_begin && b_end) { i_begin = line.IndexOf(s_begin); i_end = line.IndexOf(s_end); s_sourse = line.Substring(0, i_begin); MessageBox.Show("in line found 'Begin' and 'End'\n" + line); goto label_wr; } else

    Richard DeemingR OriginalGriffO L R 5 Replies Last reply
    0
    • M Member_15583139

      Quote:

      IDE: Integrated development environment C#, Visual Studio 2019, Windows10Pro in 2.1

      Exception: An object reference does not point to an instance of an object.

      Quote:

      using System; using System.IO; using System.Text; // - using System.Text.StringBuilder; using System.Globalization; using System.Windows.Forms; using System.Threading; // using Microsoft.CSharp; namespace WFA_BankPercent { public partial class Form7 : Form { public Form7() { InitializeComponent(); } //- string m_sWork44 = ""; // C# требует инициализации private void Form7_Load(object sender, EventArgs e) { } // below on forum www.codeproject.com 29032022 private void button21_Click(object sender, EventArgs e) { string line = System.String.Empty; string s_sourse = System.String.Empty; bool b_beforeRead = false; bool b_afterRead = false; string s_begin = "Begin"; string s_end = "End"; bool b_begin = false; bool b_end = false; int i_begin = 0; int i_end = 0; try { StreamReader sr = new StreamReader(@"D:\Input_my.txt"); StreamWriter sw = new StreamWriter("D:\\Out_my.txt", false, Encoding.UTF8); line = sr.ReadLine(); byte k = 0; // temporary for debug while (line != null) { // k++; // temporary // //+if (k > 6) // read only the first 6 lines of the input file ////+ if (k > 17) // 15 lines were written to the output file, but there was no Exception // if (k > 18) // 0 lines were written to the output file, Exception was // goto Close_my; if ((line.Trim().Length == 0)) goto label_read; b_begin = line.Contains(s_begin); b_end = line.Contains(s_end); if (b_begin && b_end) { i_begin = line.IndexOf(s_begin); i_end = line.IndexOf(s_end); s_sourse = line.Substring(0, i_begin); MessageBox.Show("in line found 'Begin' and 'End'\n" + line); goto label_wr; } else

      Richard DeemingR Offline
      Richard DeemingR Offline
      Richard Deeming
      wrote on last edited by
      #2

      The answer has not changed from your previous copy of this error: Exception: an object reference does not point to an instance of an object.[^]


      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

      1 Reply Last reply
      0
      • M Member_15583139

        Quote:

        IDE: Integrated development environment C#, Visual Studio 2019, Windows10Pro in 2.1

        Exception: An object reference does not point to an instance of an object.

        Quote:

        using System; using System.IO; using System.Text; // - using System.Text.StringBuilder; using System.Globalization; using System.Windows.Forms; using System.Threading; // using Microsoft.CSharp; namespace WFA_BankPercent { public partial class Form7 : Form { public Form7() { InitializeComponent(); } //- string m_sWork44 = ""; // C# требует инициализации private void Form7_Load(object sender, EventArgs e) { } // below on forum www.codeproject.com 29032022 private void button21_Click(object sender, EventArgs e) { string line = System.String.Empty; string s_sourse = System.String.Empty; bool b_beforeRead = false; bool b_afterRead = false; string s_begin = "Begin"; string s_end = "End"; bool b_begin = false; bool b_end = false; int i_begin = 0; int i_end = 0; try { StreamReader sr = new StreamReader(@"D:\Input_my.txt"); StreamWriter sw = new StreamWriter("D:\\Out_my.txt", false, Encoding.UTF8); line = sr.ReadLine(); byte k = 0; // temporary for debug while (line != null) { // k++; // temporary // //+if (k > 6) // read only the first 6 lines of the input file ////+ if (k > 17) // 15 lines were written to the output file, but there was no Exception // if (k > 18) // 0 lines were written to the output file, Exception was // goto Close_my; if ((line.Trim().Length == 0)) goto label_read; b_begin = line.Contains(s_begin); b_end = line.Contains(s_end); if (b_begin && b_end) { i_begin = line.IndexOf(s_begin); i_end = line.IndexOf(s_end); s_sourse = line.Substring(0, i_begin); MessageBox.Show("in line found 'Begin' and 'End'\n" + line); goto label_wr; } else

        OriginalGriffO Offline
        OriginalGriffO Offline
        OriginalGriff
        wrote on last edited by
        #3

        As I said last time you asked this:

        Quote:

        This is one of the most common problems we get asked, and it's also the one we are least equipped to answer, but you are most equipped to answer yourself. Let me just explain what the error means: You have tried to use a variable, property, or a method return value but it contains null - which means that there is no instance of a class in the variable. It's a bit like a pocket: you have a pocket in your shirt, which you use to hold a pen. If you reach into the pocket and find there isn't a pen there, you can't sign your name on a piece of paper - and you will get very funny looks if you try! The empty pocket is giving you a null value (no pen here!) so you can't do anything that you would normally do once you retrieved your pen. Why is it empty? That's the question - it may be that you forgot to pick up your pen when you left the house this morning, or possibly you left the pen in the pocket of yesterday's shirt when you took it off last night. We can't tell, because we weren't there, and even more importantly, we can't even see your shirt, much less what is in the pocket! Back to computers, and you have done the same thing, somehow - and we can't see your code, much less run it and find out what contains null when it shouldn't. But you can - and Visual Studio will help you here. Run your program in the debugger and when it fails, it will show you the line it found the problem on. You can then start looking at the various parts of it to see what value is null and start looking back through your code to find out why. So put a breakpoint at the beginning of the method containing the error line, and run your program from the start again. This time, the debugger will stop before the error, and let you examine what is going on by stepping through the code looking at your values. But we can't do that - we don't have your code, we don't know how to use it if we did have it, we don't have your data. So try it - and see how much information you can find out!

        Use the debugger. We can't do that for you!

        "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!

        "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
        "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

        1 Reply Last reply
        0
        • M Member_15583139

          Quote:

          IDE: Integrated development environment C#, Visual Studio 2019, Windows10Pro in 2.1

          Exception: An object reference does not point to an instance of an object.

          Quote:

          using System; using System.IO; using System.Text; // - using System.Text.StringBuilder; using System.Globalization; using System.Windows.Forms; using System.Threading; // using Microsoft.CSharp; namespace WFA_BankPercent { public partial class Form7 : Form { public Form7() { InitializeComponent(); } //- string m_sWork44 = ""; // C# требует инициализации private void Form7_Load(object sender, EventArgs e) { } // below on forum www.codeproject.com 29032022 private void button21_Click(object sender, EventArgs e) { string line = System.String.Empty; string s_sourse = System.String.Empty; bool b_beforeRead = false; bool b_afterRead = false; string s_begin = "Begin"; string s_end = "End"; bool b_begin = false; bool b_end = false; int i_begin = 0; int i_end = 0; try { StreamReader sr = new StreamReader(@"D:\Input_my.txt"); StreamWriter sw = new StreamWriter("D:\\Out_my.txt", false, Encoding.UTF8); line = sr.ReadLine(); byte k = 0; // temporary for debug while (line != null) { // k++; // temporary // //+if (k > 6) // read only the first 6 lines of the input file ////+ if (k > 17) // 15 lines were written to the output file, but there was no Exception // if (k > 18) // 0 lines were written to the output file, Exception was // goto Close_my; if ((line.Trim().Length == 0)) goto label_read; b_begin = line.Contains(s_begin); b_end = line.Contains(s_end); if (b_begin && b_end) { i_begin = line.IndexOf(s_begin); i_end = line.IndexOf(s_end); s_sourse = line.Substring(0, i_begin); MessageBox.Show("in line found 'Begin' and 'End'\n" + line); goto label_wr; } else

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          label_read:
          b_beforeRead = line.Contains(s_begin);
          line = sr.ReadLine();
          b_afterRead = line.Contains(s_end);

          In the above code, if ReadLine hits the end of the input file then line will be null. But this code is so badly constructed it needs a complete rework.

          D M 2 Replies Last reply
          0
          • L Lost User

            label_read:
            b_beforeRead = line.Contains(s_begin);
            line = sr.ReadLine();
            b_afterRead = line.Contains(s_end);

            In the above code, if ReadLine hits the end of the input file then line will be null. But this code is so badly constructed it needs a complete rework.

            D Offline
            D Offline
            Dave Kreskowiak
            wrote on last edited by
            #5

            ...which JSOP wrote for him and he won't use it... I smell a help vampire in the making.

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

            G 1 Reply Last reply
            0
            • L Lost User

              label_read:
              b_beforeRead = line.Contains(s_begin);
              line = sr.ReadLine();
              b_afterRead = line.Contains(s_end);

              In the above code, if ReadLine hits the end of the input file then line will be null. But this code is so badly constructed it needs a complete rework.

              M Offline
              M Offline
              Member_15583139
              wrote on last edited by
              #6

              Показываю фрагмент вх-го файла qqqqqqqqqqqqqqqqqqq 2. wwwwwwwwwwwwwwww 3. eeeeeeee Begin yyyyyyy End zxcvb begin * row_6 * row_7 * row_8 e n d End 9. qwerty 11. aaaaa после обработки должен быть таким:

              Quote:

              1. qqqqqqqqqqqqqqqqqqq 2. wwwwwwwwwwwwwwww 3. eeeeeeee zxcvb 9. qwerty 11. aaaaa

              предложите АЛГОРИТМ, а придумал как есть, что НЕ ПРАВИЛЬНО, поэтому прошу помощи у более СИЛЬНЫХ и УМНЫХ.

              1 Reply Last reply
              0
              • D Dave Kreskowiak

                ...which JSOP wrote for him and he won't use it... I smell a help vampire in the making.

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

                G Offline
                G Offline
                Gary R Wheeler
                wrote on last edited by
                #7

                Dave Kreskowiak wrote:

                help vampire

                My tech vocabulary just grew. :-D

                Software Zen: delete this;

                P 1 Reply Last reply
                0
                • G Gary R Wheeler

                  Dave Kreskowiak wrote:

                  help vampire

                  My tech vocabulary just grew. :-D

                  Software Zen: delete this;

                  P Offline
                  P Offline
                  Pete OHanlon
                  wrote on last edited by
                  #8

                  We've had a few of them over the years.

                  Advanced TypeScript Programming Projects

                  1 Reply Last reply
                  0
                  • M Member_15583139

                    Quote:

                    IDE: Integrated development environment C#, Visual Studio 2019, Windows10Pro in 2.1

                    Exception: An object reference does not point to an instance of an object.

                    Quote:

                    using System; using System.IO; using System.Text; // - using System.Text.StringBuilder; using System.Globalization; using System.Windows.Forms; using System.Threading; // using Microsoft.CSharp; namespace WFA_BankPercent { public partial class Form7 : Form { public Form7() { InitializeComponent(); } //- string m_sWork44 = ""; // C# требует инициализации private void Form7_Load(object sender, EventArgs e) { } // below on forum www.codeproject.com 29032022 private void button21_Click(object sender, EventArgs e) { string line = System.String.Empty; string s_sourse = System.String.Empty; bool b_beforeRead = false; bool b_afterRead = false; string s_begin = "Begin"; string s_end = "End"; bool b_begin = false; bool b_end = false; int i_begin = 0; int i_end = 0; try { StreamReader sr = new StreamReader(@"D:\Input_my.txt"); StreamWriter sw = new StreamWriter("D:\\Out_my.txt", false, Encoding.UTF8); line = sr.ReadLine(); byte k = 0; // temporary for debug while (line != null) { // k++; // temporary // //+if (k > 6) // read only the first 6 lines of the input file ////+ if (k > 17) // 15 lines were written to the output file, but there was no Exception // if (k > 18) // 0 lines were written to the output file, Exception was // goto Close_my; if ((line.Trim().Length == 0)) goto label_read; b_begin = line.Contains(s_begin); b_end = line.Contains(s_end); if (b_begin && b_end) { i_begin = line.IndexOf(s_begin); i_end = line.IndexOf(s_end); s_sourse = line.Substring(0, i_begin); MessageBox.Show("in line found 'Begin' and 'End'\n" + line); goto label_wr; } else

                    R Offline
                    R Offline
                    Raphael Adeniji
                    wrote on last edited by
                    #9

                    Yes, I tested your code and saw the error. I do not think writing from one text file to another should require the volume of codes I saw, I may be wrong, even when you need to filter some lines. If I know precisely what you want to do, I can help you, I did something similar before. You may give detailed explanation of what you want to achieve.

                    1 Reply Last reply
                    0
                    • M Member_15583139

                      Quote:

                      IDE: Integrated development environment C#, Visual Studio 2019, Windows10Pro in 2.1

                      Exception: An object reference does not point to an instance of an object.

                      Quote:

                      using System; using System.IO; using System.Text; // - using System.Text.StringBuilder; using System.Globalization; using System.Windows.Forms; using System.Threading; // using Microsoft.CSharp; namespace WFA_BankPercent { public partial class Form7 : Form { public Form7() { InitializeComponent(); } //- string m_sWork44 = ""; // C# требует инициализации private void Form7_Load(object sender, EventArgs e) { } // below on forum www.codeproject.com 29032022 private void button21_Click(object sender, EventArgs e) { string line = System.String.Empty; string s_sourse = System.String.Empty; bool b_beforeRead = false; bool b_afterRead = false; string s_begin = "Begin"; string s_end = "End"; bool b_begin = false; bool b_end = false; int i_begin = 0; int i_end = 0; try { StreamReader sr = new StreamReader(@"D:\Input_my.txt"); StreamWriter sw = new StreamWriter("D:\\Out_my.txt", false, Encoding.UTF8); line = sr.ReadLine(); byte k = 0; // temporary for debug while (line != null) { // k++; // temporary // //+if (k > 6) // read only the first 6 lines of the input file ////+ if (k > 17) // 15 lines were written to the output file, but there was no Exception // if (k > 18) // 0 lines were written to the output file, Exception was // goto Close_my; if ((line.Trim().Length == 0)) goto label_read; b_begin = line.Contains(s_begin); b_end = line.Contains(s_end); if (b_begin && b_end) { i_begin = line.IndexOf(s_begin); i_end = line.IndexOf(s_end); s_sourse = line.Substring(0, i_begin); MessageBox.Show("in line found 'Begin' and 'End'\n" + line); goto label_wr; } else

                      R Offline
                      R Offline
                      Raphael Adeniji
                      wrote on last edited by
                      #10

                      //Try this procedure and manipulate it public void RewriteFile(string SourceFileFullName, string TargetFileFullName, string UnWantedFilterString = "") { //UnWantedFilterString variable allows a line to be eliminated in the target file if it is contained in the line, this can be fine-tuned depending on your need. //SourceFileFullName and TargetFileFullName should be full file path and name if (!File.Exists(SourceFileFullName)) { MessageBox.Show("Source File Does Not Exist, Confirm on the Disk."); return; } string CurrentLine = ""; string CurrentLineTrimedUcase = ""; string UnWantedFilterUpperTrimed = UnWantedFilterString.Trim().ToUpper(); //This is string of anything you do not want to eliminate a line try { StringBuilder sb2 = new StringBuilder(""); using (StreamReader sr = File.OpenText(SourceFileFullName)) { while ((CurrentLine = sr.ReadLine()) != null) { CurrentLineTrimedUcase = CurrentLine.Trim().ToUpper().Replace("\t", ""); //Avoid tabs, trim and change to upper case if (UnWantedFilterUpperTrimed == "") //Test for unwated Filter string { sb2.AppendLine(CurrentLine); // Append line string to StringBuilder if unwanted filter empty } else { if (CurrentLineTrimedUcase.IndexOf(UnWantedFilterUpperTrimed) < 0) { sb2.AppendLine(CurrentLine); // Only Append line string to StringBuilder if unwanted filter not in string } } } } if (sb2.Length == 0)//Empty file will not be written { MessageBox.Show("Empty File will not be re-written"); return; } //Delete target file if it exists if (File.Exists(TargetFileFullName)) { File.Delete(TargetFileFullName); } //Write file now using (StreamWriter sw = File.

                      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