sheyang2433 2017-07-07 08:33 采纳率: 33.3%
浏览 3925

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; }
    }
}
  • 写回答

4条回答 默认 最新

  • 老马不亏 2017-07-07 08:39
    关注

    断点进去看哪里空了 空指针异常很常见的

    评论

报告相同问题?

悬赏问题

  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗
  • ¥15 钢筋实图交点识别,机器视觉代码
  • ¥15 如何在Linux系统中,但是在window系统上idea里面可以正常运行?(相关搜索:jar包)
  • ¥50 400g qsfp 光模块iphy方案
  • ¥15 两块ADC0804用proteus仿真时,出现异常
  • ¥15 关于风控系统,如何去选择