调用函数时出现空指针异常。
这是我的代码:
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);
}
在输出中,我得到了“in showform 1”