-1

Is there any explanation why the NSLog always displays null when running this code?

ExtratoDeNotasSideMenuViewController *extratoDeNotasSideMenuViewController =   [[ExtratoDeNotasSideMenuViewController alloc] init];
extratoDeNotasSideMenuViewController.userImageView.image = [UIImage imageNamed:@"Icone_SideBar.png"];
NSLog(@"%@", extratoDeNotasSideMenuViewController.userImageView.image);

2014-10-28 13:40:50.594 E-proinfo[913:51418] (null)

Douglas Pfeifer
  • 147
  • 1
  • 8
  • By "returns" you mean "displays"? If `NSLog()` displays "null" it is because `extratoDeNotasSideMenuViewController.userImageView.image` is `null`. So perhaps the question should be "Why does `extratoDeNotasSideMenuViewController.userImageView.image` return `null`?" – zaph Oct 28 '14 at 15:57
  • maybe because userImageView is nil – kabarga Oct 28 '14 at 15:58

1 Answers1

1

Potential causes:

  • Your ExtratoDeNotasSideMenuViewController init returned nil because there was an error during initialization.

    NSLog(@"%@", extratoDeNotasSideMenuViewController); // Does this display '(null)'?
    
  • Your userImageView isn't initialized yet because you don't initialize it in ExtratoDeNotasSideMenuViewController init.

    NSLog(@"%@", extratoDeNotasSideMenuViewController.userImageView); // Does this display '(null)'?
    
  • Your image is nil because you don't have an image named Icone_SideBar.png.

You should either add NSLog statements or add a breakpoint and use po to inspect your objects.

Edit: If you want to set something on your view controller before your view is created (or as a persistent thing between view creations if you expect your view controller to destroy its view and then recreate it at some point), you should use a property.

Consider the following:

@interface ExtratoDeNotasSideMenuViewController
@property (strong) UIImage *myUserImage;
...
@end

@implementation ExtratoDeNotasSideMenuViewController
...
- (void)loadView {
  [super loadView];
  ...
  [self.userImageView setImage:self.myUserImage];
  ...
}
...
@end

...
  ExtratoDeNotasSideMenuViewController *extratoDeNotasSideMenuViewController =   [[ExtratoDeNotasSideMenuViewController alloc] init];
  extratoDeNotasSideMenuViewController.myUserImage = [UIImage imageNamed:@"Icone_SideBar.png"];
Ian MacDonald
  • 13,472
  • 2
  • 30
  • 51
  • When running NSLog(@"%@", extratoDeNotasSideMenuViewController); 2014-10-28 14:08:18.015 E-proinfo[1026:194071] was displayed When running NSLog(@"%@", extratoDeNotasSideMenuViewController.userImageView); 2014-10-28 14:08:18.015 E-proinfo[1026:194071] (null) Was displayed – Douglas Pfeifer Oct 28 '14 at 16:09
  • So then the second bullet applies... Your `userImageView` is `nil` because you aren't initializing it in your view controller's `init`. You're probably initializing it in your `viewDidLoad` or `loadView`, neither of which will have been called yet. – Ian MacDonald Oct 28 '14 at 16:12