Java - 构造函数将null赋给String而不是所需的值

时间:2017-03-01 22:09:42

标签: java string nullpointerexception

我正在研究这个小型Java实践项目,我应该编写一种约会应用程序...我们有这些在线单元测试来检查我们的工作,我总是喜欢测试每个我在前进之前写的方法和类。在其中一个课程中,有两种方法没有通过测试,我无法弄清楚为什么我尝试了很多不同的东西。第一种方法叫做getTitle,它只是一个普通的getter方法,它让我回到了构造函数中赋给标题的值(参见下面的代码)。没有传递的第二种方法是我必须覆盖的.equals方法。我会在各自的代码之后发布我在下面发生的错误。

以下是该类的构造函数:

public class Interest {
private String title;
private Map<String, Float> alternatives;

public Interest(String title, Map<String, Float> alternatives)
{
    if (title.isEmpty()) //Title must contain something.
        throw new IllegalArgumentException();
    if (alternatives == null) //If alternatives points at null, I have to create an empty map.
        this.alternatives =  new HashMap<String, Float>();
    else
    {
        this.alternatives = new HashMap<String, Float>(alternatives); //Map must be a copy. 
        this.title = title; //This is where my problem is happening.
    }
}

以下是getTitle方法的代码:

    public String getTitle() 
{
    return this.title;
}

测试一直说:

testGetTitle

Cause of failure:
java.lang.AssertionError: expected:<Family Guy> but was:<null>
at org.junit.Assert.fail(Assert.java:88)
at org.junit.Assert.failNotEquals(Assert.java:743)
at org.junit.Assert.assertEquals(Assert.java:118)
at org.junit.Assert.assertEquals(Assert.java:144)
at TestInterest.testGetTitle(TestInterest.java:56)

我尝试了一些不同的东西,比如在我尝试创建String的副本的构造函数中,或者在getTitle方法中,我试图返回一个新的String(this.title),但我仍然遇到了同样的错误。 ..我也尝试过使用Concat,但它没有用。

测试只是尝试使用为每种方法预先指定的值和测试来运行程序。

我遇到问题的第二种方法如下:

    @Override
public boolean equals(Object obj)
{
    if (obj == this) 
        return true;
    if (!(obj instanceof Interest)) {
        return false;
    }

    if (obj instanceof Interest && this.title == ((Interest) obj).getTitle() && this.alternatives == ((Interest) obj).getAlternatives())
        return true;
    if (this == (Interest) obj)
        return true;
    else 
        return false; 
}

它一直告诉我:

testEqualsObject

Cause of failure:
java.lang.AssertionError: Two Interests should be equal when identical.
at org.junit.Assert.fail(Assert.java:88)
at org.junit.Assert.assertTrue(Assert.java:41)
at TestInterest.testEqualsObject(TestInterest.java:104)

我以为我考虑了所有平等的选择,但不确定......

任何帮助都会受到赞赏,我没有那么多编程经验,而且我正在努力学习Java,有时候所有这些单元测试都会令人沮丧......

整个类代码如果有帮助:

package jpp.exams.dating;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

public class Interest {
private String title;
private Map<String, Float> alternatives;

public Interest(String title, Map<String, Float> alternatives)
{
    if (title.isEmpty() && title != null) //Title must contain something.
        throw new IllegalArgumentException();
    if (alternatives == null) //If alternatives points at null, I have to create an empty map.
        this.alternatives =  new HashMap<String, Float>();
    else
    {
        this.alternatives = new HashMap<String, Float>(alternatives); //Map must be a copy. 
        this.title = title; //This is where my problem is happening.
    }
}

public String getTitle() 
{
    return this.title;
}

public Map<String, Float> getAlternatives()
{
    return new HashMap<String, Float>(alternatives);
}

public float matchAlternative(String alternative)
{
    if (alternative == null || title == null)
        throw new IllegalArgumentException();
    else if (title.equals(alternative))
        return 1f;
    else if (this.alternatives.containsKey(alternative))
        return (float) this.alternatives.get(alternative);
    else 
        return 0f;
}

@Override
public String toString()
{
    String s = title + "\n";

    for (Map.Entry<String, Float> entry : this.alternatives.entrySet()) {
        String key = entry.getKey();
        Float f = entry.getValue();
        s = s.concat("\t" + key + ": " + f + "\n");
    }
    s = s.substring(0, s.length() - 1); //removes last new line

    return s;
}

@Override
public boolean equals(Object obj)
{
    if (obj == this) 
        return true;
    if (!(obj instanceof Interest)) {
        return false;
    }

    if (obj instanceof Interest && this.title == ((Interest) obj).getTitle() && this.alternatives == ((Interest) obj).getAlternatives())
        return true;
    if (this == (Interest) obj)
        return true;
    else 
        return false; 
}

public int hashCode()
{
    return Objects.hash(title);
}

}

2 个答案:

答案 0 :(得分:2)

关于空标题:在构造函数中,如果title不为空,则只为alternatives分配值。将this.title = title;部分从else块中取出。

关于equals方法:您与==进行了一些比较,您应该使用.equals,所以更改

if (obj instanceof Interest && this.title == ((Interest) obj).getTitle() && this.alternatives == ((Interest) obj).getAlternatives())

if (this.title.equals(((Interest) obj).getTitle()) && this.alternatives.equals(((Interest) obj).getAlternatives()))

(您注意到我也删除了instanceof - 检查。因为如果这样做是假的,您就已经退回了,这是不必要的)

答案 1 :(得分:0)

问题是您使用alternatives = null

实例化该类