空指针除外,但为什么? [英] Null Pointer Except, but why?

查看:133
本文介绍了空指针除外,但为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  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 =什么?什么?什么?LOLOLOLOLOLOopechucktesta;
while(i< AddyBook.size())
{
if(getType(i).equalsIgnoreCase(type))
{
strTypeList + = getType一世);
strTypeList + =\\\
;
}
i ++;
}
return strTypeList;
}

我被指向的行是

  strType = Type.toString(); 

  if(getType(i).equalsIgnoreCase(type))

我也得到其他方法中的异常除了它们正在操作的变量之外是相同的。



我的问题是在我的驱动程序类中,我直接测试了getType并且它正常工作。然后,在displayByType方法中,我甚至说如果Type为null,这样做,但它仍然只是抛出异常。我不知道为什么,但我觉得这可能很简单明了,我刚刚在这个工作太久了,现在看到它。 :/



编辑1:Type = AddyBook.get(x)的内容是一个对象,而.getContactType()的结果是枚举。 >

解决方案

我相信这一行是错误的:

  if(type.equals(null))
strTypeList =什么?什么?什么?LOLOLOLOLOLOopechucktesta;

应该是:

  if(type == null)
strTypeList =什么?什么?什么?LOLOLOLOLOLOopechucktesta;

没有看到方法调用的内容:

  Type = AddyBook.get(x).getContactType(); 

这将很难调试,但如果我猜这将是getContactType( )正在返回null。



编辑:



尝试取消链接呼叫:



而不是:

  Type = AddyBook.get(x).getContactType(); 
strType = Type.toString();

尝试:

 code>入场条目; //我实际上并不知道什么.get()返回
entry = AddyBook.get(x);
if(entry == null)
throw new RuntimeException(entry in null at+ x +book size =+ AddyBook.size());

类型type = entry.getContactType();
if(type == null)
throw new RuntimeException(type from null from entry =+ entry.toString());

strType = Type.toString();


I'm getting a nullpointerexception when I run my program using these two methods:

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;
}

The lines that I'm being pointed to are

strType = Type.toString ( );

and

if(getType(i).equalsIgnoreCase(type))

I'm also getting the exception in other methods that are identical except for the variable they're operating on.

My issue is that in my driver class I tested the "getType" method directly, and it worked properly. Then, in the "displayByType" method I even say "if Type is null, do this," but it still just throws the exception. I have no clue why, but I feel like it's probably really simple and obvious; I've just been working on this for too long to see it right now. :/

Edit 1: The contents of Type = AddyBook.get(x) is an object, and the result of .getContactType() is an enumeration.

解决方案

I believe this line is wrong:

if(type.equals(null))
    strTypeList = "What?What?What?LOLOLOLOLOLnopechucktesta";

it should be:

if(type == null)
    strTypeList = "What?What?What?LOLOLOLOLOLnopechucktesta";

Without seeing the contents of the method calls here:

Type = AddyBook.get(x).getContactType();

it'll be hard to debug but if I were to guess it would be the method "getContactType()" is returning null.

EDIT:

Try unchaining the calls:

Instead of:

Type = AddyBook.get(x).getContactType();
strType = Type.toString ( );

Try:

Entry entry; // I don't actually know what .get() returns
entry = AddyBook.get(x);
if (entry == null)
    throw new RuntimeException("entry is null at "+x+" book size="+AddyBook.size());

Type type = entry.getContactType();
if (type == null)
    throw new RuntimeException("type is null from entry="+entry.toString());

strType = Type.toString ( );

这篇关于空指针除外,但为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆