如何在未打开新选项卡的情况下在新选项卡中打开文件。我希望文件在打开时为自己打开一个新选项卡 [英] How do I open file in a new tab without first opening a new tab.i want the file to open a new tab for itself when it opens

查看:105
本文介绍了如何在未打开新选项卡的情况下在新选项卡中打开文件。我希望文件在打开时为自己打开一个新选项卡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在研究这个项目,但是当我打开任何文件而没有先打开一个标签时,它会抛出异常System.NullReferenceException。

这里是代码



I've been working on this project but when I open any file without first opening a tab,it throws an exception System.NullReferenceException.
here is the code

private void openToolStripMenuItem_Click(object sender, EventArgs e)
       {
           Stream sp;
           OpenFileDialog op = new OpenFileDialog();
           op.Title = "Select File to Open";
           op.Filter = "All files (*.*)|*.*";
           if (op.ShowDialog() == DialogResult.OK)
           {
               if ((sp = op.OpenFile()) != null)
               {
                   string st = op.FileName;
                   string filetext = File.ReadAllText(st);
                   Getnew().Text = filetext;
               }
           }
       }





我尝试过:



我尝试过使用流函数但它也给了我同样的错误。



What I have tried:

I've tried using the stream function but it gives me the same error as well.

推荐答案

如果它在这一行失败:
Getnew().Text = filetext;



那么 Getnew 正在执行,它返回一个空值 - 因此当您尝试获取Text属性(或任何其他属性,或使用任何方法)时,您将收到numm引用错误。你无法修复 - 发生的事情是绝对正确和正确的。



这是我们被问到的最常见问题之一,也是我们所遇到的问题之一最没有能力回答,但你最有能力回答自己。



让我解释一下错误的含义:你试图使用变量,属性或者一个方法返回值,但它包含null - 这意味着变量中没有类的实例。

这有点像一个口袋:你的衬衫里有一个口袋,你用它来握笔。如果你进入口袋并发现那里没有笔,你就不能在一张纸上签名 - 如果你尝试的话,你会得到非常有趣的外观!空口袋给你一个空值(这里没有笔!)所以你不能做任何你检索笔通常做的事情。它为什么空?这就是问题 - 可能是你今天早上离开家时忘了拿起你的笔,或者你昨晚把它拿到昨天的衬衫口袋里时可能会把笔留下来。



我们无法分辨,因为我们不在那里,更重要的是,我们甚至看不到你的衬衫,更不用说口袋里的东西了!



回到计算机,你做了同样的事情,不知何故 - 我们看不到你的代码,更不用说运行它了,找不到包含null的东西。

但是你可以 - 而Visual Studio将在这里帮助你。在调试器中运行程序并在该行上放置断点。当它到达时,VS将停止,让你控制。然后,您可以进入该方法以查看哪个值为null并开始回顾您的代码以找出原因。



但我们不能这样做 - 我们没有您的代码,如果我们拥有它,我们不知道如何使用它,我们不会有你的数据。所以尝试一下 - 看看你能找到多少信息!


then whatever Getnew is doing, it is returning a null value - so when you try to get the Text property (or any other property, or use any method) you will get a numm reference error. You cant "fix" that - what's happening is absolutely right and correct.

This is one of the most common problems we get asked, and it's also the one we are least equipped to answer, but you are most equipped to answer yourself.

Let me just explain what the error means: You have tried to use a variable, property, or a method return value but it contains null - which means that there is no instance of a class in the variable.
It's a bit like a pocket: you have a pocket in your shirt, which you use to hold a pen. If you reach into the pocket and find there isn't a pen there, you can't sign your name on a piece of paper - and you will get very funny looks if you try! The empty pocket is giving you a null value (no pen here!) so you can't do anything that you would normally do once you retrieved your pen. Why is it empty? That's the question - it may be that you forgot to pick up your pen when you left the house this morning, or possibly you left the pen in the pocket of yesterdays shirt when you took it off last night.

We can't tell, because we weren't there, and even more importantly, we can't even see your shirt, much less what is in the pocket!

Back to computers, and you have done the same thing, somehow - and we can't see your code, much less run it and find out what contains null when it shouldn't.
But you can - and Visual Studio will help you here. Run your program in the debugger and put a breakpoint on that line. When it reaches it, VS will stop, and let you take control. You can then step into the method to see what value is null and start looking back through your code to find out why.

But we can't do that - we don't have your code, we don't know how to use it if we did have it, we don't have your data. So try it - and see how much information you can find out!


OP发布更多代码......



如果你单步执行代码,第一次你会意识到t为null所以它跳转到返回r; ......但是你只设置了r null。

尝试这样的事情(未经测试)
Following the OP posting some more code...

If you step through your code, the first time you will realise that t is null so it jumps to the line return r;... but you have only set r to null.
Try something like this (untested)
private RichTextBox Getnew()
 {

     TabPage t = tabControl1.SelectedTab;
     if (t == null)
     {
          t = tabControl1.TabPages[0]; // point to first tabpage
     }
     return t.Controls[0] as RichTextBox;

}






or

private RichTextBox Getnew()
 {
     if(tabControl1.SelectedIndex < 0) tabControl1.SelectedIndex = 0; //actuall select the first tab

     TabPage t = tabControl1.SelectedTab;
     return t.Controls[0] as RichTextBox;

}





I.e.尝试确保返回值永远不为空或者如果用户没有选择标签页然后为它们选择一个



I.e. try to make sure that the return value is never null OR if the user hasn't selected a tabpage then select one for them


这篇关于如何在未打开新选项卡的情况下在新选项卡中打开文件。我希望文件在打开时为自己打开一个新选项卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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