Java-构造函数将null指定给字符串而不是必需的值
我正在做一个小小的Java实践项目,我应该在那里编写一个约会应用程序。。。我们有这些在线单元测试来检查我们的工作,我总是喜欢在继续之前测试我编写的每个方法和类。在第一个类中,有几个方法没有通过测试,我不明白为什么我尝试了很多不同的方法。第一个方法称为getTitle,它只是一个普通的getter方法,它将返回分配给构造函数中标题的值(请参见下面的代码)。第二个没有传递的方法是.equals方法,我必须重写它。我将在下面的相应代码之后发布错误 下面是该类的构造函数:Java-构造函数将null指定给字符串而不是必需的值,java,string,nullpointerexception,Java,String,Nullpointerexception,我正在做一个小小的Java实践项目,我应该在那里编写一个约会应用程序。。。我们有这些在线单元测试来检查我们的工作,我总是喜欢在继续之前测试我编写的每个方法和类。在第一个类中,有几个方法没有通过测试,我不明白为什么我尝试了很多不同的方法。第一个方法称为getTitle,它只是一个普通的getter方法,它将返回分配给构造函数中标题的值(请参见下面的代码)。第二个没有传递的方法是.equals方法,我必须重写它。我将在下面的相应代码之后发布错误 下面是该类的构造函数: public class In
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.
}
}
测试一直在说:
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)
它一直在告诉我:
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);
}
包jpp.exames.dating;
导入java.util.Collections;
导入java.util.HashMap;
导入java.util.Map;
导入java.util.Objects;
公共阶级利益{
私有字符串标题;
私有地图替代方案;
公共利益(字符串标题、地图备选方案)
{
if(title.isEmpty()&&title!=null)//title必须包含某些内容。
抛出新的IllegalArgumentException();
if(alternations==null)//如果alternations指向null,我必须创建一个空映射。
this.alternations=newhashmap();
其他的
{
this.alternations=newhashmap(alternations);//映射必须是副本。
this.title=title;//这就是我的问题所在。
}
}
公共字符串getTitle()
{
返回此.title;
}
公共地图getAlternations()
{
返回新的HashMap(可选项);
}
公共浮点匹配替换(字符串替换)
{
如果(可选==null | | title==null)
抛出新的IllegalArgumentException();
else if(标题等同(备选))
返回1f;
else if(此.alternations.containsKey(alternative))
返回(浮动)this.alternations.get(alternative);
其他的
返回0f;
}
@凌驾
公共字符串toString()
{
字符串s=标题+“\n”;
for(Map.Entry:this.alternations.entrySet()){
String key=entry.getKey();
Float f=entry.getValue();
s=s.concat(“\t”+key+:“+f+”\n”);
}
s=s.substring(0,s.length()-1);//删除最后一行新行
返回s;
}
@凌驾
公共布尔等于(对象obj)
{
如果(obj==此)
返回true;
如果(!(obj利息实例)){
返回false;
}
if(obj instanceof Interest&&this.title==((Interest)obj.getTitle()&&this.alternations==((Interest)obj.getAlternations())
返回true;
如果(本==(利息)obj)
返回true;
其他的
返回false;
}
公共int hashCode()
{
返回Objects.hash(title);
}
}问题是您正在用alternations=null实例化类问题是您正在用alternations=null实例化类关于空标题:在构造函数中,如果
alternations
不为null,您只能为标题
赋值。取this.title=title代码>else块外的零件
关于equals方法:您与本应使用.equals
的=
进行了一些比较,因此请更改
if (obj instanceof Interest && this.title == ((Interest) obj).getTitle() && this.alternatives == ((Interest) obj).getAlternatives())
到
(您会注意到,我还删除了instanceof
-检查。这是不必要的,因为如果该检查为false,您可能已经返回了)关于空标题:在构造函数中,如果alternations
不为空,您只为标题
赋值。取this.title=title代码>else块外的零件
关于equals方法:您与本应使用.equals
的=
进行了一些比较,因此请更改
if (obj instanceof Interest && this.title == ((Interest) obj).getTitle() && this.alternatives == ((Interest) obj).getAlternatives())
到
(您会注意到,我还删除了instanceof
-检查。这在那里是不必要的,因为如果它为false,您可能已经返回了),因为您只在一个分支中分配了它…..您能进一步解释吗?请注意,您还调用了title.isEmpty()
不检查标题
是否为空
。这可能导致NPE。关于第二个问题,请阅读。对于第一个问题,发布类的代码,并发布测试的代码。如果在传递null时仍抛出NPE,则不检查其是否为null,这是一种不错的代码实践(只有在意外状态会悄然发生时才需要)因为您只在一个分支中分配它…..您能进一步解释一下吗?请注意,您调用title.isEmpty()
时没有检查title
是否为null
。这可能导致NPE。关于第二个问题,请阅读。对于第一个问题,发布类的代码,并发布测试的代码。如果在传递null时仍抛出NPE,则不检查其是否为null,这是一种不错的代码实践(只有在意外状态会悄然发生时才需要)
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()))