简体   繁体   English

(由于未捕获的异常'NSInvalidArgumentException而终止应用程序)

[英](Terminating app due to uncaught exception 'NSInvalidArgumentException)

I am using uitableview for showing the json parsed data . 我正在使用uitableview显示json解析的数据。 the parsed data is stored in array and the array list is 100 asigned to uitableview. 解析的数据存储在数组中,并将数组列表100分配给uitableview。 but it crashing at objectAtIndex at {forloop} it showing crash report as 但它在{forloop}的 objectAtIndex崩溃,显示崩溃报告为

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: ' 由于未捕获的异常“ NSInvalidArgumentException”而终止应用程序,原因:“ -[NSMutableArray insertObject:atIndex:]: attempt to insert nil object at 0' * -[NSMutableArray insertObject:atIndex:]:尝试在0'处插入nil对象 *

plese help me 请帮助我

   - (void)viewDidLoad
{
    self.responseData = [NSMutableData data];
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:openexchangeURl]];
    [[NSURLConnection alloc] initWithRequest:request delegate:self];
    [super viewDidLoad];

}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 
{
    [responseData setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 
{
    [responseData appendData:data];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
    [connection release];
    self.responseData = nil;
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection 
{
    [connection release];

    NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
    self.responseData = nil;

    values = [responseString JSONValue];
    array = [[NSMutableArray alloc] init];
    NSMutableArray *arrTitle = [[NSMutableArray alloc] init];
    NSMutableArray *arrValues = [[NSMutableArray alloc] init];
    array =[values valueForKey:@"rates"];

    NSLog(@"array values:--> %@",array);
//    NSLog(@"values:--> %@",values);
//    NSLog(@"Particular values:--> %@",[[values valueForKey:@"rates"] valueForKey:@"AED"]);

    tempDict1 = (NSMutableDictionary *)array;            
    NSArray *arr;// =[[NSArray alloc]init];
    arr = [[tempDict1 valueForKey:@"rates"] componentsSeparatedByString:@";"];
    NSLog(@"arr-->%@",arr);
    NSString *subStar = @"=";
    [arrTitle removeAllObjects];
    [arrValues removeAllObjects];

    for (int i=0; i<[arr count]-1; i++)
    {
        [arrTitle addObject:[[arr objectAtIndex:i] substringToIndex:NSMaxRange([[arr objectAtIndex:i] rangeOfString:subStar])-1]];
        [arrValues addObject:[[arr objectAtIndex:i] substringFromIndex:NSMaxRange([[arr objectAtIndex:i] rangeOfString:subStar])]];
        NSLog(@"arrTitle is:--> %@",arrTitle);
    }

    tempDict1 = (NSMutableDictionary*)[array objectAtIndex:0];
    array = [values valueForKey:@"rates"];
    NSLog(@"tempDict--%@",[tempDict1 objectForKey:@"AED"]);

    [array retain];
    [tbl_withData reloadData];

}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSLog(@"array-->%@",array);
    return [array count];

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    intIndexPath = indexPath.row;
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        cell.textLabel.adjustsFontSizeToFitWidth = YES; 
        cell.textLabel.font = [UIFont systemFontOfSize:8]; 
        cell.textLabel.numberOfLines = 4; 

    }

//    NSLog(@"data is like:--> %@",array);
//    cell.textLabel.text= [NSString stringWithFormat:@"%@",[array objectAtIndex:intIndexPath]];
    cell.textLabel.text =[array objectAtIndex:intIndexPath];

    return cell;
}

There is only one item in your array, here is where you add just the one object: 数组中只有一项,在这里仅添加一个对象:

array = [[NSMutableArray alloc] init];
[array addObject:[values valueForKey:@"rates"]];

You should assign the the value that you get from the [values valueForKey:@"rates"] to the array variable. 您应该将从[values valueForKey:@"rates"]获得的值分配给数组变量。 Also make the array variable a property that will retain the value. 还使数组变量成为将保留值的属性。

@property (nonatomic, strong) NSArray *array;

then assign it to the property: 然后将其分配给属性:

self.array  = [values valueForKey:@"rates"];

Also move all styling of the cell to the if where you create the new cell. 如果您要在其中创建新的单元格,请将单元格的所有样式移到。 This will speed thing up, since you not change the style of the cell every time. 这将加快速度,因为您不必每次都更改单元格的样式。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
       cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease];
       cell.textLabel.adjustsFontSizeToFitWidth = YES;  
       cell.textLabel.font = [UIFont systemFontOfSize:8];  
       cell.textLabel.numberOfLines = 4;  

    }
    NSLog(@"data is like:--> %@",array);
    //    NSString *cellValue =[array objectAtIndex:indexPath.row];
    //    cell.textLabel.text = cellValue;

    cell.textLabel.text= [NSString stringWithFormat:@"%@",[array objectAtIndex:indexPath.row]];

    return cell;
}

Get your data from server in correct format...in your situation your data is in one array. 从服务器以正确的格式获取数据...在您的情况下,数据位于一个阵列中。 so make it in different different array.. 所以把它放在不同的数组中。

{"reply":["AED = 3.673188","AFN = 48.5725","","","",""]}

or you can parse your response and save single single data into another array... 或者您可以解析您的响应并将单个单个数据保存到另一个数组中...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 由于未捕获的异常&#39;NSInvalidArgumentException而终止应用程序 - Terminating app due to uncaught exception 'NSInvalidArgumentException 由于未捕获的异常“ NSInvalidArgumentException”而终止应用程序 - Terminating app due to uncaught exception 'NSInvalidArgumentException' 由于未捕获的异常“ NSInvalidArgumentException”而终止应用程序, - Terminating app due to uncaught exception 'NSInvalidArgumentException', 由于未捕获的异常“ NSInvalidArgumentException”而终止应用程序 - Terminating app due to uncaught exception 'NSInvalidArgumentException' iOS应用程序异常-由于未捕获的异常&#39;NSInvalidArgumentException&#39;而终止应用程序 - iOS App Exception - Terminating app due to uncaught exception 'NSInvalidArgumentException' 由于未捕获的异常“ NSInvalidArgumentException”而终止应用程序,原因:“-[NSDecimalNumber objectAtIndex:] - Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSDecimalNumber objectAtIndex:] iPhone上的stringByTrimmingCharactersInSet错误“由于未捕获的异常&#39;NSInvalidArgumentException&#39;而终止应用程序” - “Terminating app due to uncaught exception 'NSInvalidArgumentException'” error with stringByTrimmingCharactersInSet on iPhone 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[__NSCFString isDescendantOfView:]: - Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString isDescendantOfView:]: 在iPhone编程中由于未捕获的异常&#39;NSInvalidArgumentException&#39;终止了应用程序 - Terminating app due to uncaught exception 'NSInvalidArgumentException', in iPhone programming 由于未捕获的异常&#39;NSInvalidArgumentException&#39;而终止应用程序,原因:&#39;-[QBDDXMLElement attributeFloatValueForName:withDefaultValue:]: - Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[QBDDXMLElement attributeFloatValueForName:withDefaultValue:]:
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM