C# 设置对象成员时出现NullReferenceException
运行以下程序时,我得到一个NullReferenceException。我认为问题在于我正在创建一个C# 设置对象成员时出现NullReferenceException,c#,class,exception,nullreferenceexception,C#,Class,Exception,Nullreferenceexception,运行以下程序时,我得到一个NullReferenceException。我认为问题在于我正在创建一个行,其中包含点类 using System; class Driver { static void Main() { Point pOne = new Point(); Point pTwo = new Point(2, 1); Console.Write("Point pOne: "); PrintPoint(p
行
,其中包含点
类
using System;
class Driver
{
static void Main()
{
Point pOne = new Point();
Point pTwo = new Point(2, 1);
Console.Write("Point pOne: ");
PrintPoint(pOne);
Console.Write("Point pthree: ");
PrintPoint(pTwo);
Line lOne = new Line(pOne, pTwo);
Console.WriteLine("Line lOne: ");
PrintLine(lOne);
//Rectangle rOne = new Rectangle();
Rectangle rOne = new Rectangle(lOne);
Console.WriteLine("Rectangle rOne: ");
PrintRectangle(rOne);
Console.ReadLine();
}
// The PrintPoint method
// purpose: display the coordinates of a Point
// Parameters: a Point object
// returns: none
static void PrintPoint(Point p)
{
Console.Write("({0},{1})", p.GetXCoord(), p.GetYCoord());
}
// the PrintLine method
// purpose: display the endpoints of a line
// Parameters: a Line object
// returns: none
static void PrintLine(Line aline)
{
// notice how we get the point objects from the line
// and then print their coordinates
Point p1 = aline.GetStartPoint();
Point p2 = aline.GetEndPoint();
Console.Write(" \t");
PrintPoint(p1);
Console.Write(" - ");
PrintPoint(p2);
}
static void PrintRectangle(Rectangle aRec)
{
Line Left = aRec.getLeft();
Line Top = aRec.gettop();
Line Right = aRec.getRight();
Line Bottem = aRec.getBottem();
Console.Write("\t Left: ");
PrintLine(Left);
Console.Write("\n\t Top: ");
PrintLine(Top);
Console.Write("\n\t Right: ");
PrintLine(Right);
Console.Write("\n\t Bottem: ");
PrintLine(Bottem);
}
}
class Rectangle
{
private Line left;
private Line top;
private Line right;
private Line bottem;
public Rectangle()
{
Point zero = new Point();
left.setEndPoint(zero);
left.SetStartPoint(zero);
top.setEndPoint(zero);
top.SetStartPoint(zero);
right.setEndPoint(zero);
right.SetStartPoint(zero);
bottem.setEndPoint(zero);
bottem.SetStartPoint(zero);
}
public Rectangle(Line enter)
{
Point stDgl = new Point();
Point endDgl = new Point();
stDgl = enter.GetStartPoint();
endDgl = enter.GetEndPoint();
//stDgl
int a = stDgl.GetXCoord();
int b = stDgl.GetYCoord();
//endDgl
int c = endDgl.GetXCoord();
int d = endDgl.GetYCoord();
Point endright = new Point();
endright.SetXCoord(c);
endright.SetYCoord(b);
Point endleft = new Point();
endleft.SetXCoord(a);
endleft.SetYCoord(d);
//LEFT
left.SetStartPoint(stDgl); // **NullReferenceException**
left.setEndPoint(endleft);
//TOP
top.SetStartPoint(endleft);
top.setEndPoint(endDgl);
//RIGHT
right.SetStartPoint(endDgl);
right.setEndPoint(endright);
//BOTTEM
bottem.SetStartPoint(endright);
bottem.setEndPoint(stDgl);
}
public Line getLeft()
{
return left;
}
public Line gettop()
{
return top;
}
public Line getRight()
{
return right;
}
public Line getBottem()
{
return bottem;
}
}
// the Line class
class Line
{
// data members - notice that they are Point objects
private Point startPoint;
private Point endPoint;
// default constructor
// purpose: initialize data members to zero
// Parameters: none
// returns: none
public Line()
{
// notice how we call methods in the Point class
**startPoint.SetXCoord(0);** ***NullReferenceException***
startPoint.SetYCoord(0);
endPoint.SetXCoord(0);
endPoint.SetYCoord(0);
}
// parameterized constructor
// purpose: initialize data members to p1 and p2
// Parameters: Point objects p1 and p2
// returns: none
public Line(Point p1, Point p2)
{
startPoint = p1;
endPoint = p2;
}
/*
//LEFT
Point endleft = new Point();
endleft.SetXCoord(a);
endleft.SetYCoord(d);
left.SetStartPoint(stDgl);
left.setEndPoint(endleft);
* */
// the GetStartPoint method
// purpose: return the value of the starting point
// Parameters: none
// returns: the value of the starting point as a Point object
public Point GetStartPoint()
{
return startPoint;
}
// the GetEndPoint method
// purpose: return the value of the ending point
// Parameters: none
// returns: the value of the ending point as a Point object
public Point GetEndPoint()
{
return endPoint;
}
// the SetStartPoint method
// purpose: store the value of the starting point
// Parameters: the value of the starting point as a Point object
// returns: none
public void SetStartPoint(Point p1)
{
startPoint = p1;
}
// the SetEndPoint method
// purpose: store the value of the ending point
// Parameters: the value of the ending point as a Point object
// returns: none
public void setEndPoint(Point p2)
{
endPoint = p2;
}
}
// The Point class
class Point
{
// data members
private int xCoord;
private int yCoord;
// default constructor
// purpose: initialize data members to zero
// Parameters: none
// returns: none
public Point()
{
xCoord = 0;
yCoord = 0;
}
// parameterized constructor
// purpose: initialize data members to x an y
// Parameters: two integers x and y
// returns: none
public Point(int x, int y)
{
xCoord = x;
yCoord = y;
}
// the GetXCoord method
// purpose: return the value of the x-coordinate
// Parameters: none
// returns: the value of the x-coordinate as an int
public int GetXCoord()
{
return xCoord;
}
// the GetYCoord method
// purpose: return the value of the y-coordinate
// Parameters: none
// returns: the value of the y-coordinate as an int
public int GetYCoord()
{
return yCoord;
}
// the SetXCoord method
// purpose: stores the value of the x-coordinate
// Parameters: the value of the x-coordinate as an int
// returns: none
public void SetXCoord(int x)
{
xCoord = x;
}
// the SetYCoord method
// purpose: stores the value of the y-coordinate
// Parameters: the value of the y-coordinate as an int
// returns: none
public void SetYCoord(int y)
{
yCoord = y;
}
}
您试图在创建对象之前设置对象的成员。例如:
class Line
{
private Point startPoint;
private Point endPoint;
public Line()
{
startPoint.SetXCoord(0);
...
在构造函数的开头,startPoint
将是空引用。您需要创建一个新的点
对象,并将引用指定给起始点
,如下所示:
startPoint = new Point();
startPoint.SetXCoord(0); // etc
我还建议您在觉得合适的时候尽快将get/set方法更改为properties,这样您的代码看起来更像是惯用C而不是Java。您试图在创建对象之前设置对象的成员。例如:
class Line
{
private Point startPoint;
private Point endPoint;
public Line()
{
startPoint.SetXCoord(0);
...
在构造函数的开头,startPoint
将是空引用。您需要创建一个新的点
对象,并将引用指定给起始点
,如下所示:
startPoint = new Point();
startPoint.SetXCoord(0); // etc
我还建议您在觉得合适的时候尽快将get/set方法更改为properties,这样您的代码看起来更像惯用的C而不是Java。在使用这些点对象之前,您必须对它们调用new,否则它们将保持null,从而引发null引用异常
startPoint = new Point();
endPoint = new Point();
在使用这些点对象之前,必须对其调用new,否则它们将保持null,从而引发null引用异常
startPoint = new Point();
endPoint = new Point();
执行此操作时:
public Rectangle(Line enter)
{
// ...
实际上,您正在创建一个新的构造函数。此构造函数将运行,但默认构造函数不会运行
我猜您希望默认构造函数public Rectangle()
也能运行。您可以通过使用构造函数链接轻松地完成这一点
public Rectangle(Line enter) : this() // Call the default constructor as well
{
// ...
这将消除空引用异常,并提供我认为您期望的行为
但是,我也建议阅读其他帖子中的一些建议,特别是-提供一些好的建议,例如使用属性。当您这样做时:
public Rectangle(Line enter)
{
// ...
实际上,您正在创建一个新的构造函数。此构造函数将运行,但默认构造函数不会运行
我猜您希望默认构造函数public Rectangle()
也能运行。您可以通过使用构造函数链接轻松地完成这一点
public Rectangle(Line enter) : this() // Call the default constructor as well
{
// ...
这将消除空引用异常,并提供我认为您期望的行为
但是,我也建议阅读其他帖子中的一些建议,特别是-提供一些好的建议,比如使用属性。我认为他的问题是他希望构造函数默认链接到默认构造函数,当然不是这样。@Reed:这可能对矩形构造函数有帮助,但这对我在这里展示的线条构造器没有帮助。非常正确:)我注意到了另一个,但我认为部分原因是对构造器如何工作缺乏了解。我认为他的问题是,他希望构造器在默认情况下链接到默认构造器,这当然没有…@Reed:这可能对矩形构造器有帮助,但对我在这里展示的直线构造器没有帮助。非常正确:)我注意到了另一个构造器,但我认为部分原因是对构造器的工作原理缺乏了解。