抛出NullReferenceException [英] Throwing NullReferenceException

查看:71
本文介绍了抛出NullReferenceException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

< g class =" gr_ gr_267 gr-alert gr_gramm gr_inline_cards gr_disable_anim_appear Grammar multiReplace"数据-GR-ID =" 267" ID = QUOT; 267">首先< / g取代;在论坛上发帖并且对编程很新,所以如果我在错误的地方发布
,请道歉。我写了一些代码,它读取一组字符串值并将其存储到List中。然后< g class =" gr_ gr_1534 gr-alert gr_spell gr_inline_cards gr_disable_anim_appear ContextualSpelling ins-del multiReplace"数据-GR-ID =" 1534"
id =" 1534"> foreach< / g> 列表中的值,它们将一行接一行地输入到现有Excel工作表的第一列中。它工作正常,直到我尝试将其重构为方法。

<g class="gr_ gr_267 gr-alert gr_gramm gr_inline_cards gr_disable_anim_appear Grammar multiReplace" data-gr-id="267" id="267">First</g> post on the forum and fairly new to programming, so apologies if I'm posting in the wrong place. I wrote a bit of code which reads a set of string values and stores it into a List. Then <g class="gr_ gr_1534 gr-alert gr_spell gr_inline_cards gr_disable_anim_appear ContextualSpelling ins-del multiReplace" data-gr-id="1534" id="1534">foreach</g> value in the list, they are entered into the first column of an existing excel sheet one row after another. It worked fine until I tried to refactor it into methods.

namespace WriteToExcel2
{
    class WriteToExcel2
    {
        public static void Main()
        {
            Excel.Application App = new Excel.Application();
            Excel.Workbook xBook = App.Workbooks.Open(@"C:\Users\Anon\Documents\Test Folder Excel file\Another excel layer\Successful Test.xlsx");
            Excel.Worksheet xSheet = App.ActiveSheet as Excel.Worksheet;

            while (true)
            {
                List<string> List = new List<string>();

                string value = Console.ReadLine();

                if (value == "exit")
                {
                    WriteToExcel2 List1 = new WriteToExcel2();
                    List.Add(value);
                    List1.addToSheet(List);
                    break;
                }
                else
                {
                    List.Add(value);
                    
                }
            }
            
            App.ActiveWorkbook.Save();
            App.Quit();

        }

        public void addToSheet(List<string> List)
        {
            Excel.Application App = new Excel.Application();
            Excel.Worksheet xSheet = App.ActiveSheet as Excel.Worksheet;

            int countRecords = 0;

            int endAdd = countRecords + 1;

            foreach (string l in List)
            {
                xSheet.Cells[endAdd, 1] = l; //<<<GETTING NULL REFERENCE EXCEPTION HERE<<<
                xSheet.Cells[endAdd, 2] = countRecords;
                endAdd += 1;
                countRecords += 1;

                
            }
        }
    }
}

它正在抛出 NullReferenceException  我不知道为什么。构建我的代码的任何一般编程建议也非常受欢迎:)

It's throwing a NullReferenceException and I've no idea why. Any general programming advice in structuring my code is more than welcome too :)

谢谢。

NB:很遗憾无法发布截图,因为我的帐户未经过验证。

NB: Unfortunately can't post screenshots as it appears my account isn't verified.

推荐答案

SolidCoder

re:NullReferenceException



我冒险大多数编程Excel的人使用VBA。

因此,识别您正在使用的语言会有所帮助... C#?

SolidCoder
re: NullReferenceException

I would venture that most who program Excel use VBA.
So it would have been helpful to identify the language you are using... C#?

此外,您的介绍段落也是乱码。 不是进一步冒险的诱惑。

如果作者完成,则允许编辑帖子。



您的代码问题...

  xSheet.Cells [endAdd,1] = l

可以是"l"。是小写字母L,而不是数字值。



VBA要求通过用引号括起来标识文本(字符串)。

我不熟悉C#语法。

Also, your introductory paragraph is garbled.  Not an enticement to venture further.
Editing a post is allowed, if done by the author.

Your code problem with...
  xSheet.Cells[endAdd, 1] = l
may be that "l" is the lower case letter L, not a numeric value.

VBA requires that text (strings) be identified as such by enclosing within quotation marks.
I am not familiar with C# syntax.

'---

Jim Cone

美国俄勒冈州波特兰市
https://goo.gl/ IUQUN2  (Dropbox)
'---
Jim Cone
Portland, Oregon USA
https://goo.gl/IUQUN2 (Dropbox)


这篇关于抛出NullReferenceException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆