我有一个静态类:
namespace GVN
{
static class ClData
{
public static string cltNM {get; set;}
}
}
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试使用它时,
label_nm.Text = ClData.cltNM;
Run Code Online (Sandbox Code Playgroud)
出现此错误:(http://msdn.microsoft.com/library/vstudio/sxw2ez55)
即使我手动执行此操作:
ClData.cltNM = "12345";
Run Code Online (Sandbox Code Playgroud)
前:
label_nm.Text = ClData.cltNM;
Run Code Online (Sandbox Code Playgroud)
我怎样才能避免这个错误?
在这个类中,我设置elp到ElType在构造函数中.
我可以elp在构造函数中访问精细属性(该// ...位是我访问其elp属性的位置),但是当我尝试访问elp另一种方法时ucp()- 我的程序崩溃了NullReferenceException.
我无法弄清楚我在这里做错了什么,虽然它可能是非常直接的.
[Serializable]
public class ElBase : RectangleNode
{
public ElementParameters elp;
public ElBase(ElementParameters ElType)
{
this.elp = ElType;
// ...
}
private void ucp()
{
int i = 0;
if (this.elp.HasInput)
{
// ...
}
}
Run Code Online (Sandbox Code Playgroud) "你调用的对象是空的."
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
namespace XNAdev
{
class Sprite
{
//The size of the Sprite
public Rectangle Size;
//Used to size the Sprite up or down from the original image
public float Scale = 1.0f;
//The current position of the Sprite
public Vector2 Position = new Vector2(115, 0);
//The texture object used when drawing the sprite
private Texture2D mSpriteTexture;
//Load the texture for the sprite using the Content Pipeline …Run Code Online (Sandbox Code Playgroud) 前几天我遇到了一些问题,我的界面滞后,在询问有人建议使用using语句来处理webbrowser.
现在实施后我不断得到:
用户代码未对NullReferenceException进行处理 - 未将对象引用设置为对象的实例.
我有一个奇怪的问题,因为即使我正在运行与另一个无关的代码,我仍然在那里得到null引用错误,这是没有意义的.
这是我的代码示例:
using (System.Windows.Forms.WebBrowser webBrowser1 =
new System.Windows.Forms.WebBrowser())
{
// issue happens here
if (webBrowser1.Url.AbsoluteUri.Contains("/signup"))
{
// rest of the code
}
}
Run Code Online (Sandbox Code Playgroud) 长时间读者,第一次海报:
我一直在尝试拿起C#并编写一个游戏程序,但我很想知道如何从逻辑类向GUI传输数据,反之亦然.传输字符串是理想的,看起来很简单,所以我希望有人可以帮助我.
在最基本的情况下,我想知道为什么这不起作用:
namespace MyGame
{
public partial class GameWindow : Form
{
public LogicClass gameLogic;
public GameWindow()
{
InitializeComponent();
}
private void startButton_Click(object sender, EventArgs e)
{
string toDisplay = gameLogic.getString();
this.label1.Text = toDisplay;
}
}
Run Code Online (Sandbox Code Playgroud)
和游戏逻辑类
namespace MyGame
{
public class LogicClass
{
public string getString()
{
return "success!";
}
}
}
Run Code Online (Sandbox Code Playgroud)
我已经尝试了几天,找出为什么我在行上得到nullReferenceException
string toDisplay = gameLogic.getString();
Run Code Online (Sandbox Code Playgroud)
当然,这不是我想要做的最终结果,但是如果有人能够向我解释这里发生了什么,以及如何将字符串从不同的类传递到GUI我认为我将成为能够立足于如何做我喜欢的其他事情.我已经看到很多关于将数据从表单传输到其他表单的东西,但是我找不到关于表单的表格所需要的东西.我知道可以做到; 它太简单了,不可能.谁能帮我吗?
谢谢!可靠的人
我一直绞尽脑汁(不难做到)试图把这个异常搞清楚.我有一个显示用户信息的表单.当我设置断点并逐步执行代码时.方法调用返回对象(在即时窗口中检查),但是当我将标识的用户属性分配给它时.
这是我的代码:
public Form2()
{
UserInfo user = FileAccess.UserInfoFromXML();
// All the members of user exist.
label1.Text = "Screen Name: " + user.ScreenName; //This is throwing an Null Reference exception the UserInfo is not null
label2.Text = "Full Name: "+ user.FirstName + " " + user.LastName;
label3.Text = "Address: " + user.StreetAddress + " " + user.City + " " + user.State + " " + user.Zip;
label4.Text = "Email Address: " + user.email;
label5.Text = "Date Of Birth: " …Run Code Online (Sandbox Code Playgroud) 当我运行我的代码时,它告诉我adr的对象是null,这是真的,但是当它在同一个方法的副本中工作时,为什么它不会工作,使用insert而不是select的例外.
代码如下所示:
public City doesExist(string postnr, string navn, City city, SqlConnection con)
{
DatabaseConnection.openConnection(con);
using (var command = new SqlCommand("select Id from [By] where Postnummer='" + postnr + "' and Navn='" + navn + "'", con))
{
command.Connection = con;
SqlDataReader reader = command.ExecuteReader();
if (reader.Read())
{
city.id = reader.GetInt32(0);
city.postnr = postnr;
city.navn = navn;
reader.Close();
return city;
}
reader.Close();
return null;
}
}
public City create(string postnr, string navn, City city, SqlConnection con)
{
DatabaseConnection.openConnection(con);
using (var command …Run Code Online (Sandbox Code Playgroud) 我是编程的新手,这可能是一个简单的问题,但我似乎无法弄明白.我的课程中有以下两个课程:
public class TrackingObject
{
public int OrderId { get; set; }
public int ProjectCount { get; set; }
public IList<ProjectInfo> Projects { get; set; }
}
public class ProjectInfo
{
public int ProjectId { get; set; }
public string ProjectType { get; set; }
public int ImageCount { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
为了测试我创建了一个控制台应用程序并使用了以下代码:
static void Main(string[] args)
{
TrackingObject oTracking = new TrackingObject();
ProjectInfo pInfo = new ProjectInfo();
oTracking.OrderId = 1;
oTracking.ProjectCount = 1;
pInfo.ProjectId = 1;
pInfo.ProjectType = …Run Code Online (Sandbox Code Playgroud) 我有以下代码从列表中获取值.
GetList().SingleOrDefault(x => x.Key == "MyKey").MyValue;
Run Code Online (Sandbox Code Playgroud)
当列表中存在Key具有值的属性时,MyKey它工作正常,但是当列表中没有Key具有值的属性时,MyKey它会抛出一个 NullReferenceException.如何返回null值而不是异常.
System.NullReferenceException
HResult=0x80004003
Message=Object reference not set to an instance of an object.
Run Code Online (Sandbox Code Playgroud) 今天大家好,我正在努力制作API.当我看到C#编译器的不同行为时,我感到很困惑.让我解释:
我得到例外:
If(userLoginModel.UserId>0)
{
//do something
}
else{
//do something
}
Run Code Online (Sandbox Code Playgroud)
例外:{"对象引用未设置为对象的实例."}
是的它是空的; 但如果写:
if (userLoginReturnModel != null && userLoginReturnModel.UserId >0)
{
//do something
}
else
{
//do something
}
Run Code Online (Sandbox Code Playgroud)
此代码没有抛出任何异常.
注意:我的问题是为什么编译器没有在第二个代码上抛出任何异常,因为如果存在&& condition,当编译器检查userLoginReturnModel.userId> 0时,那么这也需要抛出异常,因为object为null.而且我们知道两种情况都会被检入.任何答案都有很好的解释.
c# exception-handling compiler-errors nullreferenceexception