-1

In my cocoa application, i am uploading files to my server. And i have status bar to display the current status of file uploads. When i click on the status bar, it will show the status of current uploading file. For displaying the current status, i am adding retrieved value as objects to array. Here i given the code for adding objects to array.

 if(temp1 != Nil)
      {
      [printComplete addObject:temp1];
      }

Same type of code has been used in my code at multiple points. But sometime i am getting the below error even after checking the nil:

-[__NSArrayM insertObject:atIndex:]: object cannot be nil

I can't get this error when i run this code from Xcode / at debugging stage. It occurs rarely when i run this as app. Can anyone provide the solution to resolve this issue.

santhosh
  • 1,191
  • 4
  • 14
  • 28
  • 1
    Do you have a stacktrace? – trojanfoe Jul 14 '14 at 07:37
  • See link - http://stackoverflow.com/questions/8797282/object-cannot-be-nil-error – Yogendra Jul 14 '14 at 07:40
  • not sure but still a guess- your `temp1` is not `nil` but it returns `true`. so you can check by if `temp1` is `[nsnull null]` or not, and then `addObject:` – Anoop Vaidya Jul 14 '14 at 07:44
  • Add an exception breakpoint and post the stack trace. – Amin Negm-Awad Jul 14 '14 at 07:44
  • 2
    @AnoopVaidya Using `[NSNull null]` is the recommended way of adding "null" to an Objective-C collection class, so I don't see how that will help. – trojanfoe Jul 14 '14 at 07:50
  • 1. What let you think that he wants to add a null value? 2. Adding `NSNull`to an array is very often a code smell. It is the recommended way, *if* it is semantically correct to have a null value in the array. But it is in almost every case the better way, not to have null values in a collection. 3. Even he wants to do this, there *is* a location in his code (or code he uses), where he adds `nil` without checking it. Wouldn't it be nice to know that location? – Amin Negm-Awad Jul 14 '14 at 07:55
  • are you sure this snippet throws the exception? – holex Jul 14 '14 at 08:00
  • @AnoopVaidya, the `NSNull` is a valid object not a `nil` pointer, and it can be added to any array or dictionary safely. – holex Jul 14 '14 at 08:01
  • 1
    Would be nice if the OP **participated in this conversation**. – trojanfoe Jul 14 '14 at 08:10
  • @trojanfoe: as i mentioned *not sure, but a guess*. As this error look weird and should not come, as he is using correct way to check non-nil. – Anoop Vaidya Jul 14 '14 at 08:13
  • Looks like synchronisation problem. Yours temp1 object becomes nil after being checked. I would suggest create serial queue to access temp1 variable. – Cy-4AH Jul 14 '14 at 08:50
  • I think he simply has a dangling pointer. It is typical that in this case the configuration changes the behavior of the program. However "I do this every time swear!" is nothing that can be discussed on SO. – Amin Negm-Awad just now edit – Amin Negm-Awad Jul 14 '14 at 09:45
  • @Cy-4AH I will try to create serial queue and update here if any. – santhosh Jul 14 '14 at 09:47
  • @AminNegm-Awad I too want to know the location where it occurs. but every time i got disappointed if i run the code in Xcode. – santhosh Jul 14 '14 at 09:55
  • Yes, this let me think that it is a dangling pointer. In such a case a heisenbug (a bug that disappears, when you try to catch it) is quite usual. However you should have a stack trace, which is generated by the OS. Please add this. Moreover it is possible, IIRC, to compile the code for release, but still let Xcode generate the symbols file. Did you try that? – Amin Negm-Awad Jul 14 '14 at 10:14
  • @trojanfoe i am handling the exception in my app. If i get stack trace, can i able to find at which line error occurs. – santhosh Jul 28 '14 at 05:59

1 Answers1

0

Ok, so my original suggestion was to try the following, as OP was is testing for equality with Nil instead of nil:

if (temp1) {
    [printComplete addObject:temp1];
}

Check out NSHiptser's take on nil/Nil.

However, it turns out that they are actually defined as the same thing, so this wouldn't be the source of the problem.

I would still recommend (stylistically) using each for their intended use.

Here's another interesting answer on the subject: https://stackoverflow.com/a/1843034/1716763

I'll leave this answer here, unless people think it isn't necessary.

Community
  • 1
  • 1
JoeFryer
  • 2,751
  • 1
  • 18
  • 23
  • 1
    how this will fix the `nil` case issue? – Anoop Vaidya Jul 14 '14 at 07:40
  • 1
    Indeed; please explain the difference to the OPs code. – trojanfoe Jul 14 '14 at 07:41
  • Sorry guys, posted too early - I've added some further info. – JoeFryer Jul 14 '14 at 07:47
  • On my Mac both are defined as `__DARWIN_NULL` – Avt Jul 14 '14 at 07:47
  • 2
    What is the value of the answer? a) `Nil` equals `nil` by definition. (And in some cases it is impossible to distinguish between `nil`and `Nil`, so both *have to be equal* for the lifetime of the universe.) b) That you can omit `== nil`? No real value, especially not the problem of the OP. – Amin Negm-Awad Jul 14 '14 at 07:59
  • @Avt you're right. I used [NSHiptser](http://nshipster.com/nil/) as a reference for my answer (which describes that they have different intended uses, but doesn't mention that they are technically equivalent). I can delete the answer if it's not deemed useful though. – JoeFryer Jul 14 '14 at 10:19
  • @JoeFryer You can change it from solution to recommendation and add NSHipster proof link. After that it will be pretty useful. – Avt Jul 14 '14 at 10:50
  • +1 Didnt notice earlier. `Nil` and `nil` are indeed different ! Op is comparing objects with `Nil` when it should have been `nil`. @AnoopVaidya If you have seen in question, OP is checking with `Nil` when it should have been `nil` – GoodSp33d Jul 14 '14 at 13:44
  • An `id` var can refer to a class object or to an instance object. Sometimes you really need this. If it refers to nothing, do I have to store `nil` or `Nil`? Obviously both. Really difficult to do … `Nil` and `nil` *have to be equal*. This is not a technical detail, it has to be that way semantically. – Amin Negm-Awad Jul 14 '14 at 19:42