标题:if (books[i].IndexOf(s) != -1)未处理NullReferenceException怎么办!
只看楼主
jay520
Rank: 1
等 级:新手上路
帖 子:12
专家分:0
注 册:2011-2-11
结帖率:83.33%
已结贴  问题点数:20 回复次数:1 
if (books[i].IndexOf(s) != -1)未处理NullReferenceException怎么办!
using System;
using System.Collections.Generic;
using System.Text;

namespace BooksEnqiries
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] books = new string[5];
            int[] n = new int[5];
            int i, j;
            string s;
            books[0] = "Vicual C# 程序设计案例教程";
            books[0] = "Vicual Basic 程序设计案例教程";
            books[0] = "Vicual 程序设计案例教程";
            books[0] = "Vicual C++ 程序设计案例教程";
            books[0] = " 案例教程";
            Console.WriteLine("                图书查询\n");
            Console.Write("输入要查询的内容:");
            s = Console.ReadLine();
            j = 0;
            for (i = 0; i < books.Length; i++)
            {
                //利用string类的IndexOf方法来查询图书名称中是否有与输入的s相符的
                //如果没有,则返回-1;如果有,则返回相符字符的起始位置索引
             if (books[i].IndexOf(s) != -1)
                {
                    n[j] = i;
                    j++;
                }
            }
            if (j > 0)
            {
                Console.Write("与{0} 相关的图书有:", s);
                for (i = 0; i < j; i++)
                {
                    Console.Write("{0}  ", books[n[i]]);
                }
            }
            else
            {
                Console.WriteLine("没有符合条件的图书!");
            }
            Console.ReadLine();

        }
    }
}
搜索更多相关主题的帖子: 程序设计 
2011-02-27 14:38
qq1023569223
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:湖南科技大学
等 级:贵宾
威 望:26
帖 子:2753
专家分:13404
注 册:2010-12-22
得分:20 
if (books[i].IndexOf(s) != -1)未处理NullReferenceException怎么办!
using System;
using System.Collections.Generic;
using System.Text;

namespace BooksEnqiries
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] books = new string[5];
            int[] n = new int[5];
            int i, j;
            string s;
            books[0] = "Vicual C# 程序设计案例教程";
            books[1] = "Vicual Basic 程序设计案例教程";
            books[2] = "Vicual 程序设计案例教程";
            books[3] = "Vicual C++ 程序设计案例教程";
            books[4] = " 案例教程";
            Console.WriteLine("                图书查询\n");
            Console.Write("输入要查询的内容:");
            s = Console.ReadLine();
            j = 0;
            for (i = 0; i < books.Length; i++)
            {
                //利用string类的IndexOf方法来查询图书名称中是否有与输入的s相符的
                //如果没有,则返回-1;如果有,则返回相符字符的起始位置索引
             if (books[i].IndexOf(s) != -1)
                {
                    n[j] = i;
                    j++;
                }
            }
            if (j > 0)
            {
                Console.Write("与{0} 相关的图书有:", s);
                for (i = 0; i < j; i++)
                {
                    Console.Write("{0}  ", books[n[i]]);
                }
            }
            else
            {
                Console.WriteLine("没有符合条件的图书!");
            }
            Console.ReadLine();

        }
    }
}
怎么都是books[0]?这是我找出的一个错误,其他的不知道了。

   唯实惟新 至诚致志
2011-02-27 17:33



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-332872-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.091588 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved