IOS [__NSPlaceholderArray initWithObjects:count:]: attempt to insert nil object from objects[0]' - ios

I am trying to create a dynamic slide menu like facebook.
I have to get some data from a json request. and display this data in one of the sections of the slide table view.
I'm new in objective-c, I also tried with NSMutableArrays and I had an error.
In other table views I do the same but without sections. only one MutableArray and I can show the table.
I do something like this:
-(void) requestJSONFinishedWithParsedObject:(NSDictionary *)parsedObject{
NSArray *projectsObjectArray = [parsedObject objectForKey:#"projects"];
[self createMainNavigationController];
self.section1 = [NSArray arrayWithObjects:#"Profile", #"Notifications", #"Exit", nil];
self.section2 = [NSArray arrayWithObjects:#"Main", nil];
for (NSDictionary *projectObject in projectsObjectArray)
{
Project *newProject = [[Project alloc] init];
newProject.title= [projectObject objectForKey:#"title"];
self.section3 = [NSArray arrayWithObject:newProject.title];
}
self.menu = [NSArray arrayWithObjects:self.section1, self.section2, self.section3, nil];
[menuTableView reloadData];
}
I am having this error :
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSPlaceholderArray initWithObjects:count:]: attempt to insert nil object from objects[0]'

It is because
newProject.title= [projectObject objectForKey:#"title"];
is nil, and you are trying to add it to the array. Check to see what that value is by logging it
NSLog(#"%#", [projectObject objectForKey:#"title"]);

Related

Trying to take objects out of an array, reverse them, and put them back in

as the title says I'm having an issue with taking objects out of an array, flipping them, and putting them back in. Below is the code I currently have that ends in this error
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayI removeObjectsAtIndexes:]: unrecognized selector sent to instance
I was wondering if anyone knew how to fix this? Here's a little more info on how I have it set up:
The object "PEG" is an NSString that displays "-0.6", "4.36"
GlobalSortedArray is an array filled with dictionary containing the PEG object
//Declare variables
NSMutableArray *negArray = [[NSMutableArray alloc]init];
NSMutableIndexSet *index = [[NSMutableIndexSet alloc]init];
int negcount = 0;
NSDictionary *forLoopDict;
for (forLoopDict in globalSortedArray)
{
if ([[forLoopDict objectForKey:#"PEG"] hasPrefix:#"-"])
{
[index addIndex:negcount];
}
negcount++;
}
NSLog(#"%#", negArray);
// Removes objects from main array. This is what seems to be messing up.
[globalSortedArray removeObjectsAtIndexes:index];
// Reverses the array
NSArray* reversedArray = [[negArray reverseObjectEnumerator] allObjects];
// insters them back into the main array
[globalSortedArray insertObjects:negArray atIndexes:0];
You are trying to remove an item from globalSortedArray. It is an NSArray and not mutable.
globalSortedArray as NSmutableArray
NSArray -- > NSMutableArray

I am Trying to add NSMutableDictionary To NSMutableArray with objectAtIndex and save in UserDefaults

I am getting this error on my project in Xcode;
NSRangeException [__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array
monthValueArray= [[[NSMutableArray alloc] init] mutableCopy];
if (quaterType == 1)
{
NSMutableDictionary *jan = [[[NSMutableDictionary alloc] init] mutableCopy];
jan=[monthValueArray objectAtIndex:0];
[jan setObject:slidertext.text forKey:#"monthPercentage"];
[self.monthValueArray addObject:jan];
}
if (quaterType == 1)
{
NSMutableDictionary *feb = [[[NSMutableDictionary alloc] init] mutableCopy];
feb=[monthValueArray objectAtIndex:1];
[feb setObject:slidertext1.text forKey:#"monthPercentage1"];
[monthValueArray addObject:feb];
}
if (quaterType == 1)
{
NSMutableDictionary *mar = [[[NSMutableDictionary alloc] init] mutableCopy];
mar=[monthValueArray objectAtIndex:2];
[mar setObject:totJantext2.text forKey:#"totalOfTheMonth2"];
[monthValueArray addObject:mar];
}
[[NSUserDefaults standardUserDefaults] setObject:[NSKeyedUnarchiver unarchiveObjectWithData:[NSKeyedArchiver archivedDataWithRootObject:monthValueArray]]forKey:#"MonthValues"];
[[NSUserDefaults standardUserDefaults] synchronize];
As Like QuarterType i have 4 Quarter and i am doing addObject of NSMUtableDictionary to monthValueArray.
I Want add dictionary to userdefaults to store the Value and show it.
QuarterType==1 [objectAtIndex:0][objectAtIndex:1][objectAtIndex:2]
QuarterType==2 [objectAtIndex:3][objectAtIndex:4][objectAtIndex:5]
QuarterType==3 [objectAtIndex:6][objectAtIndex:7][objectAtIndex:8]
QuarterType==4 [objectAtIndex:9][objectAtIndex:10][objectAtIndex:11]
when i am run the app this log Are Come:-
Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array'
The error most probably arises in one of these lines:
jan=[monthValueArray objectAtIndex:0];
feb=[monthValueArray objectAtIndex:1];
mar=[monthValueArray objectAtIndex:2];
The problem is that your monthValueArray is empty and you are trying to retrieve an object from an empty array. If you want to do so, you should first populate this array.
So right now you array is just []. If you try objectAtIndex: method, it will try to retrieve an object from the specified index that does not exist in an empty array
You can also clearly see what you are doing wrong from the error:
Terminating app due to uncaught exception 'NSRangeException', reason: '* -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array'
This simply means that your monthValueArray is empty array. You are trying to get data from EMPTY array.
1) you don't have to do [[[NSMutableDictionary alloc] init] mutableCopy], the instance you get from the first 2 methods call (alloc &init) give you a mutable instance of NSDictionary.
2) you are calling jan=[monthValueArray objectAtIndex:0]; 2 lines ofter you are creating the array monthValueArray= [[[NSMutableArray alloc] init] mutableCopy];,
EDIT
I think you are confusing two methods
[monthValueArray objectAtIndex: <indexNumber>] - get you a reference to the object in the in the array object
you need to call [monthValueArray addObject: <your object>] or [array insertObjectAtIndex: <indexNumber>]
From your code you can see that you first get a reference to your object by calling the [monthValueArray objectAtIndex: <indexNumber>] but the array is empty at that time. You need to call [monthValueArray addObject: <yourObject>] first in order to have any objects in the monthValueArray

App terminated attempt to insert nil object from objects[0]

This process is sending eMails, and it works fine, but the framework wants the body of the email to be an NSArray. So I try to convert the NSString to a NSDictionary to include with the NSArray here:
NSDictionary *plainPart = [NSDictionary dictionaryWithObjectsAndKeys:kSKPSMTPPartContentTypeKey,
message,kSKPSMTPPartMessageKey,kSKPSMTPPartContentTransferEncodingKey, nil];
then I implement that here:
smtpTestMessage.parts = [NSArray arrayWithObject:plainPart];
Overall, here is my code:
NSLog(#"Start eMail");
SKPSMTPMessage *smtpTestMessage = [[SKPSMTPMessage alloc] init];
smtpTestMessage.fromEmail = #"***#gmail.com";
smtpTestMessage.toEmail = #"***#yahoo.com";
smtpTestMessage.relayHost = #"smtp.gmail.com";
smtpTestMessage.requiresAuth = YES;
smtpTestMessage.login = #"***#gmail.com";
smtpTestMessage.pass = #"***";
smtpTestMessage.subject = #"***";
smtpTestMessage.wantsSecure = YES;
smtpTestMessage.delegate = self;
NSDictionary *plainPart = [NSDictionary dictionaryWithObjectsAndKeys:kSKPSMTPPartContentTypeKey,
message,kSKPSMTPPartMessageKey,kSKPSMTPPartContentTransferEncodingKey, nil];
smtpTestMessage.parts = [NSArray arrayWithObject:plainPart];
[NSString stringWithFormat:#"%#",message];
[smtpTestMessage send];
NSLog(#"sent");
But sadly enough, when I run the app, the eMail gets to the last stage... Then the app terminates saying
2013-10-15 00:43:50.512 BullyBox[3662:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSPlaceholderArray initWithObjects:count:]: attempt to insert nil object from objects[0]'
*** First throw call stack:
(0x2f61ae8b 0x399156c7 0x2f5540cb 0x2f556749 0xc77f3 0x31dd555f 0x31dd54fb 0x31dd54cb 0x31dc10f3 0x31dd4f13 0x31dd4bdd 0x31dcfc09 0x31da4f59 0x31da3747 0x2f5e5f27 0x2f5e53ef 0x2f5e3bdf 0x2f54e541 0x2f54e323 0x342852eb 0x31e051e5 0xc91b5 0x39e0eab7)
libc++abi.dylib: terminating with uncaught exception of type NSException
EDIT
Now I keep getting the error:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString appendString:]: nil argument'
Any help is greatly appreciated! Thank you for even reading this! xD <3
Make sure plainPart is created successfully and not a nil.
It's tough to judge but if I had to guess plainPart is not getting created. Why do I think this? Let's take a look at the error:
[__NSPlaceholderArray initWithObjects:count:]:
This points to an alloc/init sequence of calls. The only place in the code you shared where you are allocating an array is here:
smtpTestMessage.parts = [NSArray arrayWithObject:plainPart];
Therefore, if you are trying to create an array with a nil object, plainPart, then it makes sense why you are getting an error about trying to insert a nil object at location 0.
Try setting a breakpoint right at this call in your code. This will help you narrow down where the crash happens.
I am not sure what you are trying to do here
NSDictionary *plainPart = [NSDictionary dictionaryWithObjectsAndKeys:kSKPSMTPPartContentTypeKey,
message,kSKPSMTPPartMessageKey,kSKPSMTPPartContentTransferEncodingKey, nil];
but based on you naming convention you are storing a key in a value location. Also you could switch to the modern objective-C syntax
NSDictionary *plainPart = #{
message: kSKPSMTPPartContentTypeKey,
kSKPSMTPPartContentTransferEncodingKey: kSKPSMTPPartMessageKey
};
This is equivalent to what you wrote, but more readable. As stated in the other answers it look like there is a problem with the creation of plainPart. Hopefully looking at this should make it easier to debug.
Check first this line smtpTestMessage.parts = [NSArray arrayWithObject:plainPart];.
Whether it is smtpTestMessageallocated if yes then check any data exist inside this or not

Sorting an NSMutableArray by a key value throws exception

I am trying to sort an NSMutableArray with the following structure:
(
{
Code = "390954-150";
Size = "35";
},
{
Code = 790540MSO;
Size = "30";
}
)
I am trying to sort basing on the Code value, the NSMutableArray is called arrayProduct:
//Sort array by Code
NSSortDescriptor *aSortDescriptor = [[NSSortDescriptor alloc] initWithKey:#"Code" ascending:YES];
[arrayProduct sortUsingDescriptors:[NSArray arrayWithObject:aSortDescriptor]];
//
However the above code throws the following exception:
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[__NSCFArray replaceObjectAtIndex:withObject:]: mutating method sent to immutable object'
Your arrayProduct looks to be NSArray.
Try converting it into NSMutableArray and :
NSMutableArray *mutableProducts = [NSMutableArray arrayWithArray:arrayProduct];
[mutableProducts sortUsingDescriptors:[NSArray arrayWithObject:aSortDescriptor]];

UISegmentedControl CoreData Binding Error

I keep getting this error when i try to bind a nsobject to a segment control
UserLocation isEqualToString:]: unrecognized selector sent to instance 0x7477a60
2013-01-22 12:44:58.115 Momentum[39936:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UserLocation isEqualToString:]: unrecognized selector sent to instance 0x7477a60'
I have verified that my core data object has data.
NSarray *arrayuserlocation = [[MMIStore defaultStore] loadAllUserLocation];
UISegmentedControl *segControl = [[UISegmentedControl alloc]initWithItems:arrayuserlocation];
[segControl addTarget:self action:#selector(didChangeSegmentControl:) forControlEvents:UIControlEventValueChanged];
[segControl setSegmentedControlStyle:UISegmentedControlStyleBar];
[segControl setTintColor:[UIColor grayColor]];
EDIT
To the answer the question below
- (NSMutableArray *)loadAllUserLocation
{
if (!allItems) {NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *e = [[model entitiesByName] objectForKey:#"UserLocation"];
[request setEntity:e]
NSError *error;
NSArray *result = [context executeFetchRequest:request error:&error];
if (!result) {
[NSException raise:#"Fetch failed"
format:#"Reason: %#", [error localizedDescription]];
}
allItems = [[NSMutableArray alloc] initWithArray:result];
}
return allItems;
It returns an array
I was able to solve my problem by doing the following.
NSArray *arraylocation = [[MMIStore defaultStore] loadAllUserLocation];
NSMutableArray *newarray = [[NSMutableArray alloc] init];
for (UserLocation *user in arraylocation)
{
NSLog(#"%# found", user.locationNm);
[newarray addObject:user.locationNm];
}
And using newarray as the datasource for the segment control.
As I mentioned in comments, the issue is that you are passing userlocation objects instead of NSString or UIImage objects required.
As per the documentation your items array should be "an array of NSString objects (for segment titles) or UIImage objects (for segment images)."
You need to fetch the strings from user location as,
NSarray *arrayuserlocation = [[[MMIStore defaultStore] loadAllUserLocation] valueForKey:#"locationNm"];//use the param name here
This should give you an array of all strings from the array of objects.
The problem is, the arrayuserlocation array should contain NSStrings instead of NSManagedObjects.
The code that's throwing the exception is expecting you to pass it an NSString (this is the object that responds to isEqualToString:). However, you are passing it a UserLocation object. You need to load up arrayuserlocation with strings from the UserLocation object, not just send an array of objects themselves.

Resources