【发布时间】:2013-10-09 01:32:09
【问题描述】:
当我使用这两种方法运行我的程序时,我得到了一个空指针异常:
public static String getType(int x)
{
Contact_Type Type;
String strType = "";
Type = AddyBook.get(x).getContactType();
strType = Type.toString ( );
return strType;
}
public static String displayByType(String type)
{
int i = 0;
String strTypeList = "";
if(type.equals(null))
strTypeList = "What?What?What?LOLOLOLOLOLnopechucktesta";
while(i < AddyBook.size())
{
if(getType(i).equalsIgnoreCase(type))
{
strTypeList += getType(i);
strTypeList += "\n";
}
i++;
}
return strTypeList;
}
我被指向的行是
strType = Type.toString ( );
和
if(getType(i).equalsIgnoreCase(type))
除了它们正在操作的变量之外,我还在其他相同的方法中遇到异常。
我的问题是,在我的驱动程序类中,我直接测试了“getType”方法,它工作正常。然后,在“displayByType”方法中,我什至说“如果 Type 为 null,请执行此操作”,但它仍然只是抛出异常。我不知道为什么,但我觉得这可能非常简单明了;我一直在研究这个太久了,现在看不到它。 :/
编辑1:Type = AddyBook.get(x)的内容是一个对象,.getContactType()的结果是一个枚举。
【问题讨论】:
-
类型为空。现在回头看看这里,
AddyBook.get(x).getContactType();看看为什么。 -
你在哪里填充你的 AddyBook - 它不起作用
-
我认为这不是问题所在。就像我说的那样,我直接测试了该方法并且它可以正常工作。我在另一个类中填充它,它工作正常。
-
@Riqqu:那么你的测试很糟糕,非常糟糕。在找到错误的根源之前不要做任何假设。我可以向您保证,
AddyBook.get(x).getContactType();正在为您在调用该方法时使用的某个变量 x 返回 null。保证。 -
这个
"Edit 1: The contents of Type = AddyBook.get(x) is an object, and the result of .getContactType() is an enumeration."什么也没告诉我们。没有代码,我们无法做出任何假设。显示代码。