Java测试时出现空指针异常

发布于 2024-12-10 06:41:05 字数 861 浏览 1 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

薔薇婲 2024-12-17 06:41:05

好吧,您忽略了您的参数...您正在为第一行中的 name 分配一个新值。这意味着你的方法几乎肯定从一开始就被破坏了。

然后考虑fullname(我假设它是一个实例变量)将来自哪里。除非您将其设置为无参数构造函数中的值,否则默认情况下它将为 null。这真的是你想要的吗?

我建议您将 Name 类更改为具有采用名称参数的构造函数,并且验证它们不为空。这样,您就永远不会拥有无效的 Name 对象,其中全名为 null

编辑:现在您已经发布了更多代码,请查看您的无参数构造函数和 setName 方法:

public Name()
{
   setName(fullname);     
}

public void setName(String n)
{
    fullname = n;
}

这意味着您的无参数构造函数相当于:

public Name()
{
   fullname = fullname;
}

...即无操作。 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 be null 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 invalid Name object, where the full name is null.

EDIT: Now you've posted more code, look at your parameterless constructor and the setName method:

public Name()
{
   setName(fullname);     
}

public void setName(String n)
{
    fullname = n;
}

That means your parameterless constructor is equivalent to:

public Name()
{
   fullname = fullname;
}

... 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.

停顿的约定 2024-12-17 06:41:05
   name = fullname.substring(0,fullname.indexOf(' '));

如果您在该行上遇到空指针异常,则 fullnamenull。这确实是唯一的可能。

   name = fullname.substring(0,fullname.indexOf(' '));

If you get a null pointer exception on that line, then fullname is null. It's really the only possibility.

韬韬不绝 2024-12-17 06:41:05

如果没有更多代码,我无法确定,但我敢打赌,您的 fullname 为 null 并且从未设置为任何内容。

Without more code I can't say for sure, but I bet you fullname is null and never set to anything.

心欲静而疯不止 2024-12-17 06:41:05

如果在全名中找不到 ' ' 索引,它不会返回 -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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文