Why the compiler gives an error when I'm right-clicking NOT on the UI element

syntax error
semantic error
compile time error
types of errors in programming pdf
syntax error example
example of syntax error and logical error?
c++ programs with errors and solutions
linker error in c++ undefined symbol

My compiler gives an error ArgumentOutOfRangeException in the file List.cs when I click on the empty space but if I click on the UI elements it's doesn't gives an error. But how it gives an error if I have a check

if (results != null)

There is my code :

public void Update()
{

    if (Input.GetKey(KeyCode.Mouse1))
    {
        eventData = new PointerEventData(eventSystem);
        eventData.position = Input.mousePosition;

        List<RaycastResult> results = new List<RaycastResult>();

        _raycaster.Raycast(eventData, results);

        if (results != null)
        {
            if (results[0].gameObject.tag == "ItemIcon")
            {
                if(currentMenu != null)
                {
                    Destroy(currentMenu);
                }
                currentMenu = Instantiate(SplitMenu, transform);
            }
        }
    }
} 
1.    List<RaycastResult> results = new List<RaycastResult>();

     _raycaster.Raycast(eventData, results);

2.    if (results != null)
    {
3.        if (results[0].gameObject.tag == "ItemIcon")

Line 1 initialises results, so that check on line 3 will never be false. Just because results is initialised, doesn't mean it has any entries in it, so line 3 will give an error if there are no results.

As others have pointed out, line 2 should be:

if(results.Any())

.Any() is better that .Count > 0 because it implies the intent that 'something is in the list', rather than 'the count is greater than 0'.

Compile errors and warnings, . Declare all C symbols in an extern "C" block; either wrap your #include directives in such a block, or put it in the headers themselves. Trying to populate the list with information from a file. The compiler gives the following: undefined reference to `OpenRusEng' [Error] ld returned 1 exit status Can't understand why? The C language (not found in the list)

My best guess is that you are checking that results != null, but you are not checking if results actually has any elements in it. So using results[0] on an empty array results in the ArgumentOutOfRangeException you are seeing.

Adding && results.Any() to the if check should resolve it.

How to resolve the linker error in C++ compiler, . These are errors where the compiler finds something wrong with your program, and you can't even try to execute it. Compiler errors are restricted to single source code files and are the result of 'syntax errors'. What this really means is that you've done something that the compiler cannot understand. For instance, the statement "for (;)" isn't correct syntax because a for loop always needs to have three parts.

The best option : we need to use results.Count != 0 instead of null.

Syntax errors, All these errors are detected by compiler and thus are known as compile-time errors. Most frequent syntax errors are: Missing Parenthesis (}); Printing the value of  To the compiler, the error is actually with the 'if', since it is the first place that something can be detected as being wrong. To report a different line, the compiler would have to report the last properly parsed token as the error, rather than the first place the error is detected.

What is the difference between run-time error and compiler error , This paper provides further awareness of the importance of compiler error messages and propose to apply the body of knowledge of the Human-Computer​  This doesn't make any sense. Why would the compiler call new EmpStruct in the second block but not the first? This answer doesn't explain why there is a difference between the two blocks of code. – jason Sep 23 '11 at 14:27

Compiler, Linker and Run-Time Errors, The Types of Compilation Errors. First, let's distinguish between the types of errors: most compilers will give three types of compile-time alerts: compiler warnings,  Why is this declaration not compatible? Why does this covariance declaration compile? [duplicate] Why does the compiler reject the declaration of a 2D generic array? TS1146: Declaration expected .. why? What is the purpose of this line? (Function declaration) XML markup declaration error; Getting error: implicit declaration of function 'proc

Errors in C/C++, I agree with Paul Olaru on this one, for some reason your compiler is taking 'y' and 'Y' as variables not character representations. Perhaps this can be due to  Run-Time Errors: Errors which occur during program execution (run-time) after successful compilation are called run-time errors. One of the most common run-time error is division by zero also known as Division error. These types of error are hard to find as the compiler doesn’t point to the line at which the error occurs.

Comments
  • please try results.Count != 0 instead of null
  • Actually, consider if (results != null && results.Any()) { .... The exception is very likely the result of you looking for the first element in an empty collection. By the way, that's an exception thrown by your code, it's not emitted by the compiler.
  • Ali Kanat, it helped me. Thank you.
  • Or just && result[0] != null since anyway only results[0] is used later. Prevents unnecessary gc activity caused by using LinQ
  • @derHugo: No, && result[0] != null would throw the same exception. results.Count != 0 is more efficient than using Any though, assuming this is a List<T>.
  • If you want to avoid Linq, you could do results.Length > 0 as well (or is it .Count? It's been a while since I've worked with C# and I can't remember if Count was another LInq extension or not).
  • @CodeMonkey: Count() is an extension method, Count is a property in IList<T>/IReadOnlyList<T> etc. Length is the property on arrays... yay for consistency ;)