Java测试时出现空指针异常
public boolean isItMyName(String name)
{
name = fullname.substring(0,fullname.indexOf(' '));
...
}
然后我的代码就会继续下去。当我运行这个时,我得到一个空指针异常,错误指向这一行。
我还创建了一个 JUnit 测试器,用于测试输入是 true 还是 false。
Name myName = new Name();
assertEquals(false, myName.isItMyName("Troll");
它还在 assertEquals(false, myName.isItMyName("Troll"));
处给了我一个空指针异常,
我已经无计可施了。我该如何解决这个问题?
@ Greg Hewgill:
在我的 Name 类中,我这样做:
public class Name(String n)
{
name = n;
}
但是在我的 main 方法中,我传入字符串:Name myName = new Name("Johnathy");
所以字符串不为空。
全名:
public class EmailAddress {
private String address;
public EmailAddress(String a)
{
address = a;
}
public String toString()
{
return address;
}
public boolean isItMyName(String name)
{
name = fullname.substring(0,fullname.indexOf(' '));
...
}
Then my code goes on and on. When I run this, I get a null pointer exception and the error is pointing to this line.
I also created a JUnit tester, where it tests whether the input is true or false.
Name myName = new Name();
assertEquals(false, myName.isItMyName("Troll");
It also gives me a null pointer exception at assertEquals(false, myName.isItMyName("Troll"));
I'm at my wit's end. How do I fix this?
@ Greg Hewgill:
In my Name class, I do this:
public class Name(String n)
{
name = n;
}
But in my main method, I pass in the string: Name myName = new Name("Johnathy");
So string isn't null.
FULLNAME:
public class EmailAddress {
private String address;
public EmailAddress(String a)
{
address = a;
}
public String toString()
{
return address;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
好吧,您忽略了您的参数...您正在为第一行中的
name
分配一个新值。这意味着你的方法几乎肯定从一开始就被破坏了。然后考虑
fullname
(我假设它是一个实例变量)将来自哪里。除非您将其设置为无参数构造函数中的值,否则默认情况下它将为null
。这真的是你想要的吗?我建议您将
Name
类更改为仅具有采用名称参数的构造函数,并且验证它们不为空。这样,您就永远不会拥有无效的Name
对象,其中全名为null
。编辑:现在您已经发布了更多代码,请查看您的无参数构造函数和
setName
方法:这意味着您的无参数构造函数相当于:
...即无操作。
fullname
将为 null,因此会出现异常。我的建议是完全摆脱无参数构造函数,并让另一个验证参数。
Well you're ignoring your parameter... you're assigning a new value to
name
in the first line. That means your method is almost certainly broken to start with.Then consider where
fullname
(which I assume is an instance variable) is going to come from. Unless you're setting it to a value within your parameterless constructor, it's going to benull
by default. Is that really what you want?I suggest you change your
Name
class to only have constructors taking name parameters, and that you validate that they're not null. That way you can never have an invalidName
object, where the full name isnull
.EDIT: Now you've posted more code, look at your parameterless constructor and the
setName
method:That means your parameterless constructor is equivalent to:
... i.e. a no-op.
fullname
will be null, hence the exception.My advice is to get rid of the parameterless constructor entirely, and make the other one validate the parameter.
如果您在该行上遇到空指针异常,则
fullname
为null
。这确实是唯一的可能。If you get a null pointer exception on that line, then
fullname
isnull
. It's really the only possibility.如果没有更多代码,我无法确定,但我敢打赌,您的
fullname
为 null 并且从未设置为任何内容。Without more code I can't say for sure, but I bet you
fullname
is null and never set to anything.如果在全名中找不到 ' ' 索引,它不会返回 -1 ,从而导致子字符串抓取 0,-1 为空...也许将其扔进 if 中,如果索引 <> 则跳过该步骤。 0 ...并且您传入的参数不应该是全名
if no ' ' index is found in the full name wouldnt it return -1 thus causing substring to grab 0,-1 which is null... perhaps toss it in an if and skip that step if the index is < 0... and shouldnt your parameter that is incomming be fullname