Java 方法,该方法始终返回null

Java 方法,该方法始终返回null,java,c#,null,nullreferenceexception,Java,C#,Null,Nullreferenceexception,我被一段从Java翻译成C的代码卡住了 基本上,我有一个Map(Dictionary),其中的键由Pair和由我创建的类(Square)表示的值组成;在这个类中只有一个字段是可选的(是的,我用C#创建了可选类) 在开始的时候,我用对填充这个字典,以使一个simil网格和空可选,正如您在下面的代码中看到的那样 class World { private Dictionary<Pair<int, int>, Square> map = new Dic

我被一段从Java翻译成C的代码卡住了

基本上,我有一个Map(Dictionary),其中的键由Pair和由我创建的类(Square)表示的值组成;在这个类中只有一个字段是可选的(是的,我用C#创建了可选类)

在开始的时候,我用对填充这个字典,以使一个simil网格和空可选,正如您在下面的代码中看到的那样

class World
{
    private Dictionary<Pair<int, int>, Square> map = 
        new Dictionary<Pair<int, int>, Square>();

    public World(int width, int height)
    {
        this.size = new Pair<int, int>(width, height);
        for (int w = 0; w < this.size.GetX(); w++)
        {
            for (int h = 0; h < this.size.GetY(); h++)
                this.map.Add(new Pair<int, int>(w, h), 
                    new Square(Optional<Entity>.Empty()));
        }
    }
}
阶级世界
{
专用字典映射=
新字典();
公共世界(整数宽度、整数高度)
{
此尺寸=新的一对(宽度、高度);
for(int w=0;w
这是方形类

class Square
{
    private Optional<Entity> entity;

    public Square (Optional<Entity> entity)
    {
        this.entity = entity;
    }

    public Optional<Entity> GetEntity() 
    {
        return this.entity;
    }

    public void SetEntity(Optional<Entity> entity)
    {
        this.entity = entity;
    }
}
classsquare
{
私人可选实体;
公共广场(可选实体)
{
this.entity=实体;
}
公共可选GetEntity()
{
返回该实体;
}
公共实体(可选实体)
{
this.entity=实体;
}
}
问题是,下面的函数总是返回null当我试图从字典中获取一个存在的值时,它抛出System.NullReferenceException:Object reference未设置为对象的实例。 在这段代码中,我去掉了所有的控件,但我知道我试图得到一个已经插入的值;另外,我尝试运行Dictionary.ContainsValue,结果返回false!但我确实已经把字典本地化了

public Square? GetSquare(int x, int y)
{
    if (y < this.size.GetY() && y >= 0 && < x this.size.GetX() && x >= 0)
    {
        this.map.TryGetValue(new Pair<int, int>(x, y), out Square? square);
        return square;
    }

    throw new InvalidOperationException("no square in this position!");
}
公共广场?GetSquare(整数x,整数y) { 如果(y=0&&x this.size.GetX()&&x>=0) { this.map.TryGetValue(新对(x,y),out Square?Square); 返回广场; } 抛出新的InvalidOperationException(“此位置无方块!”); }
我在这里也留下了可选类的代码,但我几乎100%确定这不是问题所在

public class Optional<T>
{
    private T value;
    public bool IsPresent { get; set; } = false;

    private Optional() { }

    public static Optional<T> Empty()
    {
        return new Optional<T>();
    }

    public static Optional<T> Of(T value)
    {
        Optional<T> obj = new Optional<T>();
        obj.Set(value);
        return obj;
    }

    private void Set(T value)
    {
        this.value = value;
        this.IsPresent = true;
    }

    public T Get()
    {
        return value;
    }
}
公共类可选
{
私人T值;
public bool IsPresent{get;set;}=false;
私有可选(){}
公共静态可选空()
{
返回新的可选();
}
公共静态可选值(T值)
{
可选对象=新的可选对象();
对象集(值);
返回obj;
}
私有无效集(T值)
{
这个值=值;
this.IsPresent=true;
}
公共部门得不到
{
返回值;
}
}
这是双人课

    public class Pair<X, Y>
    {
        private X first;
        private Y second;

        public Pair(X first, Y second)
        {
            this.first = first;
            this.second = second;
        }
        public X GetX()
        {
            return this.first;
        }

        public Y GetY()
        {
            return this.second;
        }

        public override string ToString()
        {
            return "<" + first + "," + second + ">";
        }
    }
公共类对
{
私人X优先;
二等兵;
公共对(X第一,Y第二)
{
this.first=first;
这个秒=秒;
}
公共X GetX()
{
先把这个还给我;
}
公共场所
{
把这个还给我;
}
公共重写字符串ToString()
{
返回“”;
}
}
好的,解决了。 正如你所说,问题在于结对班。
我用ValueTuple类代替它,现在一切都正常了,还是谢谢。

什么是
?它可能有一个错误的
GetHashCode
Equals
实现。使用值元组作为键可能更好(即
(int,int)
)。使用
this.map.TryGetValue(新对(x,y),out Square?Square)
除非
Pair
是结构类型,否则永远不会得到任何结果。我敢肯定,这在Java中也行不通。请看那一行中的逻辑:“给我与这个
new
对象关联的值”-因为这个对象是新的,所以没有值与它关联
new Pair(x,y)
将不匹配现有的字典键,除非您在
Pair
类中重写了
Equals
GetHashCode
。默认行为是使用引用比较来确定相等性,并且
new
任何引用都不会与其他引用匹配。请显示
类代码以获得实际答案的帮助。您的
不足以满足您的用例。如果不实现
GetHashCode
Equals
,Rufus L描述的正是正在发生的事情。同样,我建议使用值元组,
(int,int)
(即
ValueTuple
,而不是
tuple
)作为键。它正确地实现了这些方法(当然,需要元组中的类型也正确地实现它,而
int
就是这样做的)。