• (2 Pages)
  • +
  • 1
  • 2

What does this error mean? Understanding the common errors.

#1 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5432
  • View blog
  • Posts: 11,645
  • Joined: 02-June 10

Posted 05 December 2011 - 09:49 AM

*
POPULAR

Understanding the most frequently asked about errors.

This is not meant to be a comprehensive debugging tutorial. Instead it is meant to help the new coding student understand the error Visual Studio is reporting so they can figure out for themselves what is cause and fix their own code. I would file it under "Give a man a fish and he eats for a day. Teach a man to fish and he eats for a lifetime."

It is still very important to work through the excellent debugging tutorials here. One or two hours spent on these tutorials will save you 100 hours on your first project. Not to mention help save your attitude and sanity. For the life of me I can't understand why debugging isn't taught as part of your university course. But judging by the questions we get here, it isn't. If you take the time to get good at it you will be head and shoulders above your class-mates.


Locate the error
First there should be no mystery about where the error is occurring. 99% of the time Visual Studio halts execution when the error is hit and highlights the line. At this point you can hover the mouse over any of your variables and the value will be displayed in the tooltip.

Attached Image


You should have the Autos and Locals pallets open. These let you see the values of all the variables in the current scope. So often just seeing the 4 important variables you are working with will guide you to the problem: All it takes is to see one of them at 0 when you were expecting 143 to point you to the problem.

Attached Image



Null reference... Value cannot be null...
Now that you know how to see the values of the variables this one should be easy. Just move your mouse from one variable to the next pausing briefly for the tooltip to update with the value, until you see which one is null.

Object {name} is not set to the instance of an object.
Take this example:
Button demo;
Demo.Text = "failure";

demo was declare but it was never instantiated, meaning it was never set to the instance of something. See the correction here?
Button demo = new Button;
Demo.Text = "success";


Stack overflow
This is almost always the result of an infinite loop of method/function calls. The list of methods called is stored on the Stack. This is so when a method is complete and can return, it knows where to return. But the Stack is of limited size. So if you have this situation:
void methodOne()
{
   methodTwo();
}

void methodTwo()
{
   methodOne();
}

You have a never ending circle of one calling two calling one calling two calling one calling two. Eventually overflowing the Stack: Thus "Stack overflow"

Cannot apply mathematical operation to string
"5" + "5" = "55" not "10"
You probably used a Textbox to get a numerical value. Textbox values are strings not numbers. So you either need to convert the string to a number before doing the math, or better yet use a GUI control meant for just numbers like a NumericUpDown or a NumericTextBox. If you choose to try to parse text to a number don't forget that it could fail and you need to code for that eventuality. When a user types "five" or "yogi bear" in a Textbox you have to handle it gracefully.

The object {name} does not exist in the given context.
This is all about scope or the lifetime of an object/variable. In the simplest terms a thing exists only within the code block it was defined in. As long as that block remains alive so does your thing and your ability to reference it. In this example just because we made a picnic.basket doesn't mean our Ranger knows anything about it because he wasn't part of that class. Its sad when you aren't invited to the picnic.

namespace jellystone
{
     public class picnic
     {
          public Object basket;  // This exists only within the picnic
          public NumberOfSandwiches = 6;
           void someMethod()
           {
                 int x;
           }

           void anotherMethod()
           {
                 int x;  // NOT the same x as in someMethod()
           }
     }


      class Ranger
      {
           void different method()
           int count = basket.NumberOfSandwiches;  // basket is unknown here. Undefined in the context of this method of this class
      }
}




Not all code paths return a value
You have some different paths of execution for your method, maybe through a series of if checks or switch statements. Your method is defined to return a value but one or more of those possible paths have no return.

string HiMediumOrLow(int someNumber)
{
   if (someNumber < 100) return "low";
   else if (someNumber > 500) return "high"; 
}

What happens if someNumber is 250? There is no path of execution that will return a value such as "medium". By adding a final return that is hit if no other condition is true we know we will always have something return.
string HiMediumOrLow(int someNumber)
{
   if (someNumber < 100) return "low";
   else if (someNumber > 500) return "high"; 
   return "medium";
}


Index was outside the bounds of the array/collection
So you have some array or collection. First realize that collects are zero-indexed meaning the first element is [0] The second is [1] The third is [2] The fourth is [3] ...
So if you have an array of 10 items they are elements 0-9. Element [10] would be the eleventh item out 10. 11 is outside the limits {bounds} of the collection. You need to make sure your collection has as many things as you are trying to access.
This includes strings because they are collections of characters. If you are trying to chop up a string input by the user you can't assume they gave you a long enough string to work with. If you try to get 10 characters starting from the 5th, you have to have at least 15 characters. If the user entered 12 and you don't check before reaching past the end of the string then you are reaching beyond the bounds of the array {of characters}.

Out of memory error - when trying to open a jpg
This error lies! You aren't really out of memory. It's just that the image is either still being written by another process or it is corrupt. If you are reacting to a FileSystemWatcher it is probably still being written. The FSW raises its new item event when a new entry is written in the file allocation table of the drive, not when the new file is done being written. Try adding a loop so you keep trying for 20 seconds. If it still fails the file might be corrupt. This can cause the GUI to look unresponsive if done on the same thread so loading files is best done on a new thread.

Is This A Good Question/Topic? 22
  • +

Replies To: What does this error mean? Understanding the common errors.

#2 dougyno1  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 77
  • Joined: 06-May 11

Posted 08 December 2011 - 09:04 AM

Well I can tell you that we do not cover debugging in my university course, it is kinda left for us to work out so grat tutorial as it covers some things I have had trouble with ;) !!
Was This Post Helpful? 0
  • +
  • -

#3 RexGrammer  Icon User is offline

  • Coding Dynamo
  • member icon

Reputation: 181
  • View blog
  • Posts: 777
  • Joined: 27-October 11

Posted 08 December 2011 - 09:54 AM

tlhIn`toq said:

One or two hours spent on these tutorials will save you 100 hours on your first project. Not to mention help save your attitude and sanity.


So true
Also I can't stress enough how important is debugging as tlhIn`toq said...

Great tutorial (as always :) )
Was This Post Helpful? 0
  • +
  • -

#4 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5432
  • View blog
  • Posts: 11,645
  • Joined: 02-June 10

Posted 08 March 2012 - 09:56 AM

I had always planned to EDIT and ADD new common errors.
I didn't realize I couldn't edit the original post after a certain period of time. Bummer.

Can't delete bitmap - It is in use by another process.
The file was probably opened with .FromFile(string path). This is always the first method everyone uses because it seems so darned simple and straighforward. But there is a drawback: It maintains a link to the file on HDD. That is an intended behavior from MS:
Spoiler


The two accepted ways around this are:
  • Read the image, then make a new bitmap as a .Clone of the first, then dispose of the first to release the link.
  • Read the file as a byte[] then make a new image object from the byte[]

Was This Post Helpful? 0
  • +
  • -

#5 bravo659  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 181
  • Joined: 18-May 08

Posted 08 March 2012 - 11:25 AM

View PosttlhIn`toq, on 05 December 2011 - 11:49 AM, said:

Understanding the most frequently asked about errors.

Not to mention help save your attitude and sanity. For the life of me I can't understand why debugging isn't taught as part of your university course.

That is true in most colleges debugging is mentioned but not taught as part of the curriculum.
This is why most students like myself; already finished past March 2011.
It is frustrating and when trying to get help bounce back to square one, figure it out yourself.

Debugging is essential and what it means is the most important part of programming where most students are having trouble.

You created this tutorial which i think is the best thing in dreamincode has happened. These tutorials will assist students and others to become more familiar if not intermediate to expert and try mastering the concept of debugging a program. At times i do struggle debugging a code but a little direction is also appreciated.

How can i go about doing a tutorial?

Thanks

-Desi
Was This Post Helpful? 0
  • +
  • -

#6 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5432
  • View blog
  • Posts: 11,645
  • Joined: 02-June 10

Posted 08 March 2012 - 11:43 AM

View Postbravo659, on 08 March 2012 - 12:25 PM, said:

You created this tutorial which i think is the best thing in dreamincode has happened.


Thanks! I appreciate the support.

View Postbravo659, on 08 March 2012 - 12:25 PM, said:

How can i go about doing a tutorial?


Attached Image


If you don't have that option, it could be because you are either too new, not created enough posts to establish yourself, or just may not be allowed with a negative reputation rating.

If you do create a new tutorial, don't expect to see it appear right away: One of the moderators will have to approve it for accuracy as well as need (not duplicating something already posted) and originality (not a duplicate of a tutorial on another site.

Of course no such approval is needed to post something in your own blog. It was pointed out to me that some information is best put out as an article in a blog, and doesn't necessarily fit the description of a 'tutorial'.
Was This Post Helpful? 0
  • +
  • -

#7 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5432
  • View blog
  • Posts: 11,645
  • Joined: 02-June 10

Posted 18 March 2012 - 01:08 PM

Sometimes you just have to go the extra mile to find a problem and you still never do actually find it.

There comes a time when the best thing you can do is rebuild. Do it in small steps, and test VERY often until something stops working.

I recently had my own encounter like this. Even after documenting every step of trying to find the problem it just never showed itself. Even though duplicate controls and classes were made that worked fine.

Thinking back I think it was caused by the sudden shutdown of the PC while the VS solution was open. I think a file in the deep dark nether regions of the Visual Studio where developers fear to tread was corrupted.
Was This Post Helpful? 0
  • +
  • -

#8 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4425
  • View blog
  • Posts: 7,694
  • Joined: 08-June 10

Posted 12 July 2012 - 06:21 AM

Just wanted to add another common cause for stack overflows: Properties that get themselves. It's an easy typo to make:

private string name;
public string Name{
  get{
    return Name;
  }
  set{
    Name = value;
  }
}


Easy to make, easy to overlook, but any time you use Name, you get a Stack Overflow. Resharper will let you know, but VS by default won't flag this before you run into it yourself.
Was This Post Helpful? 1
  • +
  • -

#9 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5432
  • View blog
  • Posts: 11,645
  • Joined: 02-June 10

Posted 12 July 2012 - 07:34 AM

If a person follows proper naming styles that shouldn't happen. Underscores leading the private backing field but otherwise the same. This makes it really easy to to do a find/replace and get both the property and the backingfield at the same time.

Name and _Name in this case.



Long ago I added this snippet to my workstations and gave it the shortcut of "propnot" (for property notify)



That gives shortcuts of
prop - short property with get/set
propdp - dependency property in WPF
propnot - INotify property with backing fields

Spoiler


Attached Image
Was This Post Helpful? 0
  • +
  • -

#10 adgjlsfhk  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 89
  • Joined: 08-February 13

Posted 11 March 2013 - 05:24 PM

Can you add the meaning of several errors? I have gotten these recently, and still don't fully understand them.
1. Segmentation Fault
2. pointer makes integer without a cast

This post has been edited by adgjlsfhk: 11 March 2013 - 05:24 PM

Was This Post Helpful? 0
  • +
  • -

#11 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5432
  • View blog
  • Posts: 11,645
  • Joined: 02-June 10

Posted 11 March 2013 - 05:39 PM

I've never heard of those. Are you quoting them exactly or re-phrasing? What coding language? Do you have example code that produces them or screenshots showing the exact messages?
Was This Post Helpful? 0
  • +
  • -

#12 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10364
  • View blog
  • Posts: 38,390
  • Joined: 27-December 08

Posted 11 March 2013 - 05:42 PM

I did a quick Google search, and it looks like a Segfault would occur if you were using Mono. Are you using Mono or are you working with C/C++?
Was This Post Helpful? 0
  • +
  • -

#13 adgjlsfhk  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 89
  • Joined: 08-February 13

Posted 11 March 2013 - 05:43 PM

I'm quoting exactly. This is just c. I think I have gotten rid of all of them by now, sorry
Was This Post Helpful? 0
  • +
  • -

#14 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10364
  • View blog
  • Posts: 38,390
  • Joined: 27-December 08

Posted 11 March 2013 - 05:48 PM

C and C# are two different languages. A Segfault in C means you are going out of bounds in an array (or pointer). Try debugging this way. If you are still encountering errors, please post in the appropriate forum (C/C++). :)
Was This Post Helpful? 1
  • +
  • -

#15 adgjlsfhk  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 89
  • Joined: 08-February 13

Posted 11 March 2013 - 05:58 PM

Sorry, I hadn't realized that this was in the objective c part. I didn't look at the top.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2