null pointer exception during a function call
I am getting a null pointer exception when I am calling a function.
Here is my code: 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());
}
Here is my output :
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
Why am I getting a null pointer exception even though there is a memory location for forForm?
EDIT:private void showForm(String[] forForm) {
Log.e(tag, "in show form1");
LinearLayout ol = (LinearLayout) findViewById(R.id.Out开发者_如何学GoerLayout);
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 the output, I am getting till "in showform 1"
Assuming forForm is not null, as showed in the initial post, then the only explanation I can think of is that showForm is the one throwing the exception.
You have initializated a TextView array with 3 null TextView, you never construct those TextViews.
This might be happening with .getnodename(), if the node is not present, you are working on a null node, check for sure that the node is present before working on that node with .getnodename().
精彩评论