Java 函数调用期间出现空指针异常
当我调用函数时,我得到一个空指针异常。Java 函数调用期间出现空指针异常,java,android,string,nullpointerexception,function-call,Java,Android,String,Nullpointerexception,Function Call,当我调用函数时,我得到一个空指针异常。 这是我的密码: try{ ..... String [] forForm = new String[5]; forForm[0] = new String(parentElement.getNodeName()); forForm[1] = new String(childElement1.getNodeName()); forF
这是我的密码:
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
在输出中,我一直到“在showform 1中”假设forForm不为null,如最初的帖子所示,那么我能想到的唯一解释是showform就是引发异常的那个
您已经用3个空TextView初始化了TextView数组,您从未构造过这些TextView。这可能发生在.getnodename()上,如果该节点不存在,则表示您正在处理一个空节点,请检查该节点是否存在,然后再使用.getnodename()处理该节点。尝试单步执行调试器。确定。请添加异常的完整堆栈跟踪,可能是数组forForm或其他变量parentElement…等是异常的来源。为了确保这一点,请将字符串替换为简单的字符串,如(“forForm1,forForm2..etc”),并查看问题是否来自forForm数组或其他变量:parentElement…etcLog整个堆栈跟踪,而不仅仅是
e.toString()
。记住:如何进行stacktrace。很抱歉,我是新手。我正在获取forForm的所有索引的值。我将编辑qn,使其也显示2null
LinearLayout
s和2EditText
s。
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);
}