【问题标题】:Android Array StringAndroid 数组字符串
【发布时间】:2010-12-22 16:56:40
【问题描述】:

大家好,如果我尝试将字符串分配给字符串数组,我会收到 NULL POINTER EXCEPTION.. 请给我一个解决方案..

这是我的代码,我在这一行中得到空指针 thumbs[j]=title; 作为代码如下...

for (int i = 0; i < items.getLength(); i++) {
        Node item = items.item(i);
        NodeList properties = item.getChildNodes();
        for (int j = 0; j < properties.getLength(); j++) {
            Node property = properties.item(j);
            String name = property.getNodeName();
            if (name.equalsIgnoreCase("title")) {
                try{
                    property.normalize();
                    Spanned title2 = Html.fromHtml(property.getFirstChild().getNodeValue().replaceAll("\\<.*?>", ""));
                    title =  title2.toString();  
                }catch(Exception e){
                     Log.v("Exception",""+e);
                }
                thumbs[j]=title;
            }
        }
    }

【问题讨论】:

  • 你在哪里初始化thumbs?可以给我们看看吗?

标签: android arrays


【解决方案1】:

我的猜测是 thumbs[] 还没有被初始化,比如:

String [] thumbs = new String [properties.getLength()];

虽然很难说 - 你的代码没有显示你对这个数组的声明。

【讨论】:

  • 不不,我已经检查过了..如果我没有初始化意味着它会在 ECLIPSE 中显示出来
  • 你能发布完整的课程吗?如果 title 为 null,则不会出现空指针异常,因此 thumbs[j] 必须为 null。它可能为空的唯一方法是如果它没有正确初始化。就像我说的,如果没有看到整个类文件就不可能确定:-)
  • 我得到了另一个解决方案。只是我创建了一个 ArrayList 并将标题一一添加到列表中。现在我正在获取数组列表中标题的所有值。但是字符串数组仅抛出 NPE ..
【解决方案2】:

唯一的可能是 thumbs 为空。如果你不初始化它不是编译错误。不要依赖 IDE 告诉您初始化的东西是否通过检查您的代码向您自己证明,或者在您每次使用它之前打印出 thumbs 的值。

如果 j > thumbs.length-1,该行唯一的其他异常可能性是 ArrayIndexOutOfBoundsException。但同样,这不是 NPE。如果 title 为 null,那很好,也不例外。

【讨论】:

    猜你喜欢
    • 2021-10-30
    • 1970-01-01
    • 1970-01-01
    • 2011-04-24
    • 1970-01-01
    • 2011-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多