System.NullReferenceException: 未将对象引用设置到对象的实例
在学习微软认知服务,用C#编写了一个简单的接口,但是出现了“未将对象引用设置到对象的实例 (System.NullReferenceException)”这个问题。在网上查了原因,好像有以下几种说法:
“System.NullReferenceException: 未将对象引用设置到对象的实例”问题可能原因如下:
1、ViewState 对象为Null。
2、DateSet 空。
3、sql语句或Datebase的原因导致DataReader空。
4、声明字符串变量时未赋空值就应用变量。
5、未用new初始化对象。
6、Session对象为空。
7、对控件赋文本值时,值不存在。
8、使用Request.QueryString()时,所获取的对象不存在,或在值为空时未赋初始值。
9、使用FindControl时,控件不存在却没有做预处理。
10、重复定义造成未将对象引用设置到对象的实例错误.
第一次接触C#,看不出来究竟属于哪一类错误,求问各位大神这个错误应该怎么修正。编译器提示的错误在代码中注释出来了
以下附小弟源码:
using System.Collections.Generic;
using System.Net.Http;
using System.Text;
using Newtonsoft.Json;
using System;
using System.Net.Http.Headers;
using System.Web;
public class Program
private const string ApiKey = "804e4631f32a421b8cf8066ca2d54fb0";
private const string DescribeImageApiUrl =
"https://api.projectoxford.ai/vision/v1.0/describe?maxCandidates=3";
private const string ContentType = "application/json";
public static void Main(string[] args)
var imageUrls = new[] { "http://i.imgur.com/OJ6lzhz.jpg", "http://i.imgur.com/OV80Pr8.jpg" };
foreach(var imageUrl in imageUrls)
var response = DescribeImage(imageUrl);
Console.WriteLine("Image: {http://img.taopic.com/uploads/allimg/110910/2316-110910124R147.jpg}");
Console.WriteLine("Descriptions:");
foreach (var caption in response.Description.Captions)//提示错误出现在这一行
Console.WriteLine(caption.Text);
Console.WriteLine(new string('-', 5));
Console.Read();
private static DescribeImageResponse DescribeImage(string imageUrl)
var client = new HttpClient();
client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", ApiKey);
var imageJson = new { url = imageUrl };
var response = client.PostAsync(
DescribeImageApiUrl,
new StringContent(
JsonConvert.SerializeObject(imageJson),
Encoding.UTF8, ContentType)).Result;
var content = response.Content.ReadAsStringAsync().Result;
return JsonConvert.DeserializeObject<DescribeImageResponse>(content);
private class DescribeImageResponse
public DescribeImageResponseDescription Description { get; set; }
public string RequestId { get; set; }
public DescribeImageResponseMetadata Metadata { get; set; }
private class DescribeImageResponseDescription
public List<string> Tags { get; set; }
public List<DescribeImageResponseDescriptionCaption> Captions { get; set; }
private class DescribeImageResponseDescriptionCaption
public string Text { get; set; }
public decimal Confidence { get; set; }
private class DescribeImageResponseMetadata
public double Width { get; set; }
public double Height { get; set; }
public string Format { get; set; }
0 提建议
邀请回答
编辑 收藏 删除 结题 收藏 举报 追加酬金 (90%的用户在追加酬金后获得了解决方案) 
当前问题酬金
¥ 0
(可追加 ¥500)
支付方式
余额支付
余额: ¥ 499
扫码支付
提供问题酬金的用户不参与问题酬金结算和分配
支付即为同意
《付费问题酬金结算规则》
4
条回答
默认
最新
查看更多回答(-1条)
报告相同问题?
相关推荐
更多相似问题