函数调用期间的空指针异常

时间:2011-09-10 21:46:52

标签: java android string nullpointerexception function-call

我在调用函数时遇到空指针异常。
这是我的代码:

    try{
            .....
            String [] forForm = new String[5];
            forForm[0] = new String(parentElement.getNodeName());
            forForm[1] = new String(childElement1.getNodeName());
            forForm[2] = new String(ce1);
            forForm[3] = new String(childElement2.getNodeName());
            forForm[4] = new String(ce2);
            Log.e(tag,forForm.toString());
            showForm(forForm);

    } catch (Exception e) {
        Log.e(tag, "error in try: "+e.toString());
    } 

这是我的输出:

09-10 21:58:47.919: ERROR/ca(867): <?xml version='1.0' encoding='ISO-8859-1'?><LoginInfo><Username>un</Username><Password>pw</Password></LoginInfo>
09-10 21:58:47.999: ERROR/ca(867): Username: un
09-10 21:58:47.999: ERROR/ca(867): Password: pw
09-10 21:58:48.019: ERROR/ca(867): [Ljava.lang.String;@44ed5570
09-10 21:58:48.019: ERROR/ca(867): in show form1
09-10 21:58:48.040: ERROR/ca(867): error in try: java.lang.NullPointerException

为什么我得到一个空指针异常,即使forForm有一个内存位置?


修改

private void showForm(String[] forForm) {
        Log.e(tag, "in show form1");
        LinearLayout ol = (LinearLayout) findViewById(R.id.OuterLayout);
        LinearLayout [] il = new LinearLayout [2];
        EditText [] et = new EditText[2];
        TextView [] tv = new TextView[3];

        tv[0].setText(forForm[0]);
        tv[0].setTextSize(30);
        tv[0].setLayoutParams(
                new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
        Log.e(tag, "in show form2");
        il[0].setOrientation(LinearLayout.HORIZONTAL);
            tv[1].setText(forForm[1]);
            tv[1].setTextSize(20);
            tv[1].setLayoutParams(
                    new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT,1f));
            il[0].addView(tv[1]);

            et[0].setText(forForm[2]);
            et[0].setId(1111);
            et[0].setTextSize(20);
            et[0].setLayoutParams(
                    new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT,1f));
            il[0].addView(et[0]);
            Log.e(tag, "in show form3");
        il[1].setOrientation(LinearLayout.HORIZONTAL);
            tv[2].setText(forForm[3]);
            tv[2].setTextSize(20);
            tv[2].setLayoutParams(
                    new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT,1f));
            il[1].addView(tv[1]);

            et[1].setText(forForm[2]);
            et[1].setId(2222);
            et[1].setTextSize(20);
            et[1].setLayoutParams(
                    new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT,1f));
            il[1].addView(et[1]);
            Log.e(tag, "in show form4");    
        ol.addView(tv[0]);
        ol.addView(il[0]);
        ol.addView(il[1]);

        Log.e(tag, "Set Content View.");
        this.setContentView(ol);
    }

在输出中,我要到“showform 1”

2 个答案:

答案 0 :(得分:1)

假设forForm不是null,如初始帖子中所示,那么我能想到的唯一解释是showForm是抛出异常的那个。

你初始化了一个带有3个null TextView的TextView数组,你永远不会构造那些TextView。

答案 1 :(得分:0)

这可能发生在.getnodename()中,如果该节点不存在,则您正在处理空节点,在使用.getnodename()处理该节点之前,请检查该节点是否存在。