Click here to Skip to main content
15,890,506 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
bsically i have one button and two rich text boxes, i am trying to add "0x" and a "," to my hex bytes, example from 00 01 02 to 0x00, 0x01, 0x02 my problem is i keep getting a Object reference not set to an instance of an object error, here is my code

C#
public int i { get; set; }
     public int amount { get; set; }


     private void button2_Click(object sender, EventArgs e)
     {
         if (richTextBox1.Text.Length >= 2)
         {


             string text =  richTextBox1.Text.Replace( "", "");
             int num = text.Length;
             for (int i = 0; i <= num; i += 6)
             {
                 num = text.Length + 2;
                 text = text.Insert(i,( "0x"));
                 text = text.Insert(i + 4,( ","));
             }


             richTextBox2.Text = text.Remove(text.Length - 2);
         }



i am getting the error here, if (richTextBox1.Text.Length >= 2)

here is the full error


An unhandled exception of type 'System.NullReferenceException' occurred in byte converter.exe

Additional information: Object reference not set to an instance of an object.



btw im a beginner coder so be easy on me lol, any helf would be greatly appreciated.
Posted
Updated 8-Jun-15 21:23pm
v2
Comments
CPallini 9-Jun-15 3:22am    
It looks your richTextBox1 is an invalid reference (i.e. is not bound to the actual control).
Member 11536857 9-Jun-15 3:23am    
you have a suggestion on how to fix that? sorry man, pretty new to all this
CPallini 9-Jun-15 3:33am    
Make sure it referes to an actual object. Did you create the RichTextBoxes dynamically (or did you add it in the design window?) ?
Member 11536857 9-Jun-15 3:34am    
i added it in the design window, i added thw button and then the two richtextboxes
CPallini 9-Jun-15 3:37am    
Use the debugger to step into the code and find the offending reference.

1 solution

Start by checking in the debugger: as CPallini says, it's likely that your richTextBox1 is not a valid control - which presumably means you created it in your code instead of allocating it via the designer. So it won't be displayed either!

Look at the designer, highlight the rich text box you think is going to get the user input, and look at the properties pane to find it's name. Use that in your Click event handler, instead of richTextBox1 and it may start to work.

But... what do you think this code does?
C#
string text =  richTextBox1.Text.Replace( "", "");
Why are you trying to replace an empty string with an empty string?
The code you are using to insert your "0x" and commas is a little odd as well: Consider using a StringBuilder instead, or better break your input string into the hex digits first, then convert them to "0xNN" and use String.Join to add all the commas - it's a "cleaner" solution that is a lot easier to read! :laugh:
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900