简体   繁体   English

由于未捕获的异常“ NSInvalidArgumentException”而终止应用程序,原因:“数据参数为nil”

[英]Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'data parameter is nil'

I was developing apps using web services when I run the application it got responses but some time its crashes and show error like this: 运行应用程序时,我正在使用Web服务开发应用程序,但它得到了响应,但有时会崩溃并显示如下错误:

NSInvalidArgumentException', reason: 'data parameter is nil' NSInvalidArgumentException”,原因:“数据参数为nil”

Here is my coding: 这是我的编码:

NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://api.espn.com/v1/sports/tennis/news/headlines?apikey=th98vzm67pufc36ka42xxxmy"]];
[request setHTTPMethod:@"GET"];
[request setValue:@"application/json;charset=UTF-8" forHTTPHeaderField:@"content-type"];

NSError *err;
NSURLResponse *response;

NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];
NSDictionary *jsonArray=[NSJSONSerialization JSONObjectWithData:responseData  options:NSJSONReadingMutableContainers error:&err];
NSArray *headlinetop=[jsonArray objectForKey:@"headlines"];

for (int i=0; i<[headlinetop count]; i++)
{
    NSString *headstr=[[headlinetop objectAtIndex:i]objectForKey:@"headline"];
    [loadingcell addObject:headstr];
}

I suspect the "headline" object isn't always available; 我怀疑"headline"对象并不总是可用; guard against that using: 防止使用以下内容:

for (int i=0; i<[headlinetop count]; i++)
{
    NSString *headstr=[[headlinetop objectAtIndex:i]objectForKey:@"headline"];
    if (headstr)
        [loadingcell addObject:headstr];
}

If loadcell is an Objective-C collection class, it will object to headstr being nil as they cannot store NULL objects. 如果loadcell是Objective-C集合类,则它将反对headstrnil因为它们不能存储NULL对象。

The problem is this code: 问题是此代码:

NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];
NSDictionary *jsonArray=[NSJSONSerialization JSONObjectWithData:responseData  options:NSJSONReadingMutableContainers error:&err];

Sending a URL request you should always expect that something went wrong. 发送URL请求时,您始终应该期望出现问题。 There are many reasons for it. 原因有很多。 In this case responseData is nil . 在这种情况下, responseDatanil NSJSONSerialization expects the data arg not to be nil : Error. NSJSONSerialization期望data arg不为nil :错误。

When receiving a response for a request you should always check it for being nil . 收到请求响应时,应始终检查它是否为nil Always. 总是。

So the code should look like this: 因此,代码应如下所示:

NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];
if (responseData == nil)
{
  //  Maybe logging. But getting nil as a response of a URL request isn't exciting.
  return; // or whatever you have to (not) do
}
NSDictionary *jsonArray=[NSJSONSerialization JSONObjectWithData:responseData  options:NSJSONReadingMutableContainers error:&err];

I think your problem is Url which you are using. 我认为您的问题是您正在使用的网址。 Please check the following link: 'NSInvalidArgumentException', reason: 'data parameter is nil' 请检查以下链接: “ NSInvalidArgumentException”,原因:“数据参数为nil”

暂无
暂无

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

相关问题 由于未捕获的异常&#39;NSInvalidArgumentException&#39;而终止应用程序,原因:&#39;UICollectionView必须使用非nil布局参数初始化 - Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'UICollectionView must be initialized with a non-nil layout parameter **由于未捕获的异常&#39;NSInvalidArgumentException&#39;而终止应用程序,原因:&#39;应用程序试图在目标上显示nil模态视图控制器 - ** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Application tried to present a nil modal view controller on target 由于未捕获的异常“ NSInvalidArgumentException”而终止应用程序,原因:“ *** setObjectForKey:键不能为零” - Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** setObjectForKey: key cannot be nil' Swift:由于未捕获的异常“ NSInvalidArgumentException”而终止应用程序,原因:“实体名称不能为零。” - Swift : Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Entity name must not be nil.' 由于未捕获的异常“ NSInvalidArgumentException”而终止应用程序,原因:“应用程序试图在目标上显示nil模态视图控制器 - Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Application tried to present a nil modal view controller on target 得到“ ***由于未捕获的异常&#39;NSInvalidArgumentException&#39;而终止应用程序,原因:&#39;-[__ NSCFNumber compare:]:nil参数&#39;” - getting “*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFNumber compare:]: nil argument'” 由于未捕获的异常“ NSInvalidArgumentException”而终止应用程序,原因:“ *** setObjectForKey:对象不能为nil(键:索引)” - Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** setObjectForKey: object cannot be nil (key: index)' 由于未捕获的异常&#39;NSInvalidArgumentException&#39;而终止应用程序,原因:&#39;+ entityForName:nil不是合法的NSManagedObjectContext - Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+entityForName: nil is not a legal NSManagedObjectContext 由于未捕获的异常“ NSInvalidArgumentException”而终止应用程序,原因:“ ***-[__ NSCFConstantString stringByAppendingString:]:无参数” - Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSCFConstantString stringByAppendingString:]: nil argument' 由于未捕获的异常“ NSInvalidArgumentException”而终止应用程序,原因:“-[SWRevealViewController manifestToggel:] - Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[SWRevealViewController revealToggel:]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM