本文介绍了抛出未处理的异常:system.nullreferenceexception:对象引用未设置为mainclass.main()中对象的实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试获取用户输入的列表中的数据。但是得到以下错误或异常。



输入要添加的入口数量

2

输入名称

name1

输入名称

name2



未处理例外:

System.NullReferenceException:对象引用未设置为对象的实例

at mainClass.Main()[0x00036] in< c0682968f3f849faa9c9866d3baf8ee0> ;:0

[错误]致命的未处理的异常:System.NullReferenceException:对象引用未设置为对象的实例

在mainClass.Main()[0x00036]的< c0682968f3f849faa9c9866d3baf8ee0>: 0

退出状态1



我尝试了什么:



使用System;

使用System.Collections.Generic;

公共类输入

{

public int entery;

public string userName,myString;

public void userEntery()

{

精读sole.WriteLine(输入要添加的入口数);

entery = int.Parse(Console.ReadLine());

for(int i = 0;我< entery; i ++)

{

Console.WriteLine(输入名称);

userName = Console.ReadLine();

List< string> myString = new List< string>();

myString.Add(userName);

}

}

}

公共类mainClass

{

public static void Main()

{

输入i =新输入();

i.userEntery();

foreach(i.myString中的char c)

{

Console.WriteLine(您输入名称{0},c);

}

}

}

trying to get data in list which is entered by user. but getting following error or exception.

enter how much enteries to add
2
Enter name
name1
Enter name
name2

Unhandled Exception:
System.NullReferenceException: Object reference not set to an instance of an object
at mainClass.Main () [0x00036] in <c0682968f3f849faa9c9866d3baf8ee0>:0
[ERROR] FATAL UNHANDLED EXCEPTION: System.NullReferenceException: Object reference not set to an instance of an object
at mainClass.Main () [0x00036] in <c0682968f3f849faa9c9866d3baf8ee0>:0
exit status 1

What I have tried:

using System;
using System.Collections.Generic;
public class input
{
public int entery;
public string userName,myString;
public void userEntery()
{
Console.WriteLine("enter how much enteries to add");
entery = int.Parse(Console.ReadLine());
for ( int i = 0; i < entery; i++ )
{
Console.WriteLine("Enter name");
userName = Console.ReadLine();
List<string> myString = new List<string>();
myString.Add(userName);
}
}
}
public class mainClass
{
public static void Main()
{
input i = new input();
i.userEntery();
foreach ( char c in i.myString )
{
Console.WriteLine("You enterd name {0}",c);
}
}
}

推荐答案


{
Console.WriteLine("Enter name");
userName = Console.ReadLine();
List<string> myString = new List<string>();
myString.Add(userName);
}



您正在为每个输入字符串创建一个名为 myString 的新局部变量。因此,当您稍后尝试在对象引用中使用名为 myString 的变量时,它不存在。更改您的代码,使其如下所示:


You are creating a new local variable called myString for each input string. So when you later try to use the variable named myString in your object reference it does not exist. Change your code so it looks like:

public class input
{
    public int entery;
    public string userName;
    public List<string> myString = new List<string>(); // add this line
    public void userEntery()
    { 
        Console.WriteLine("enter how much enteries to add");
        entery = int.Parse(Console.ReadLine());
        for ( int i = 0; i < entery; i++ )
        {
            Console.WriteLine("Enter name");
            userName = Console.ReadLine();
// Remove this line            List<string> myString = new List<string>();
            myString.Add(userName);
        }
    }
}



您还需要更改 foreach main中的一行来迭代List中的字符串,而不是字符串中的单个字符。

这有点快而又脏,但它现在应该可以工作。


You also need to change the foreach line in main to iterate the strings in the List, rather than single characters in a string.
That is a bit quick and dirty but it should work for now.


这篇关于抛出未处理的异常:system.nullreferenceexception:对象引用未设置为mainclass.main()中对象的实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-16 09:52