6

Assume there's code for an iphone project that is :

      IBOutlet UITextField* numberDisplay;
      @property (strong,nonatomic) IBOutlet UITextField *numberDisplay;

in implementation file and

@synthesize numberDisplay;

in implementation file. Also in implementation is

     -(IBAction)numberClicked:(id)sender {  
     UIButton *buttonPressed = (UIButton *)sender;  
    int val = buttonPressed.tag;  
     if ( [numberDisplay.text compare:@"0"] == 0 ) {  
    numberDisplay.text =[NSString  stringWithFormat:@"%d", val ];  

  } else {  
      numberDisplay.text = [NSString  
                     stringWithFormat:@"%@%d", numberDisplay.text, val ];  
    }

   }

When I run the app there is no display shown in the UITextfield, even though the connections were made with IB and is proven to be made by viewing the inspector. Even as a test if I add the lines

    numberDisplay.text = @"a";
    NSLog(@"the value is %@",numberDisplay.text);   
    NSLog(@"the value is %@",numberDisplay);

I get " the value is (null) in both cases.Any ideas.

Can someone please tell me what is wrong with these two lines?. Thank you.


Thanks to all. I started from scratch and now all works. Looks like I had a mislabeled file.

Garoal
  • 2,364
  • 2
  • 19
  • 31
  • 1
    Try printing the `numberDisplay` variable instead of it's `text` property. I do suspect that somewhere else you haven't initialized your text field; as you can't assign a variable to a null object you get null in return. – fearmint Dec 03 '11 at 04:11
  • how would you initialize the text field. In attributes inspector i have tried setting the text and/or placeholder and both to 0 to see if that would help but it doesn't – stephen kronwith Dec 03 '11 at 06:13
  • 1
    You must link up the UiTextView in your .xib file with the IBOutlet. – Thomas Nadin Dec 03 '11 at 09:07
  • I've dragged the text box in the xib file to the outlet and it does register, since in the connection inspector it shows the referenceing outlet being the file's owner IBOUtlet i created. – stephen kronwith Dec 03 '11 at 15:17
  • Everyone is assuming that this `UITextField` is not connected correctly, and this is likely true, but you should test for that. That can be done by adding a third line: `NSLog(@"the text field is %@",numberDisplay);` – NJones Dec 03 '11 at 16:31

4 Answers4

2

Null objects can receive selectors and they ignore them and return null object. the case you encounter is like this:

[(UITextField*)nil setText:@"a"];
NSLog(@"the value is %@", [(UITextField*)nil text]);

Make sure the text field is not null

Daniel
  • 30,896
  • 18
  • 85
  • 139
1

Following line will return non null value if you have set IBOutlet to Interface builder. It will declared in .h file.

IBOutlet UITextField* numberDisplay;

If you are not setting outlet to an IB, it will obviously return null value or you have to initialize it by programmatically.

UITextField *numberDisplay = [[UITextField alloc] initWithFrame:CGRectMake(10,10, 100,30)];
numberDisplay.font = [UIFont fontWithName:@"Verdana" size:12.0];
numberDisplay.background = [UIColor clearColor];
numberDisplay.text = @"123456789";
[self.view addSubview:numberDisplay];
NSLog(@"the value is %@",numberDisplay.text); // returns 123456789
alloc_iNit
  • 5,173
  • 2
  • 26
  • 54
0

It looks like you might not be properly setting your IBOutlets in Interface Builder.

Check out my answer to this question, regarding basically the same thing.

Community
  • 1
  • 1
Patrick Perini
  • 22,555
  • 12
  • 59
  • 88
0

you have to synthesize the textfield properly in following ways:- Add the line in .h

@property (nonatomic, retain) UITextField *numberDisplay;

and add the line in .m

@synthesize numberDisplay;

and then do what you did. You will not get the null value this time.

vipul
  • 426
  • 1
  • 5
  • 13
  • check whether your connections in IB are appropriate or nor.. – vipul Dec 03 '11 at 07:57
  • I've dragged the text box in the xib file to the outlet and it does register, since in the connection inspector it shows the referenceing outlet being the file's owner IBOUtlet i created. – stephen kronwith Dec 03 '11 at 19:16
  • First drag the UITextField from library to View and then Ctrl-drag from file's owner to textField in View and then select the textFiled declared in .h file. NOw,save and run..Hope this time it will work – vipul Dec 04 '11 at 10:02