#27 Null Value in String Array causes NPE

closed
None
5
2015-03-11
2005-12-05
No

I was using Dozer to map String arrays (String []) to a
List. When doing so if the first element of the String
array is null that causes a null pointer exception
within the dozer code of the succeeding elements. The
exception occurs in method:

MappingProcessor.addOrUpdateToList(FieldMap fieldMap,
Collection sourceCollectionValue, ClassMap classMap,
Object destObj, Class destEntryType) at approximately
line 250.

The original code in that area looks like:

Object destValue = null;
Iterator iter = sourceCollectionValue.iterator();
Object sourceValue = null;
while (iter.hasNext()) {
sourceValue = iter.next();
if (destEntryType == null) {
destEntryType =
fieldMap.getDestHintType(sourceValue.getClass());
}

Here is a snippet of some code which shows the example:

System.out.println("Null Array Test");
ObjectG og = new ObjectG();
ObjectGPrime ogprime = new ObjectGPrime();
og.getStrarray()[0]=null;
og.getStrarray()[1]=null;
og.getStrarray()[3]="abcdefg";
Mapper.map(og,ogprime);
System.out.println("ogprime.abc: "+ogprime.getAbc());
System.out.println("ogprime.list:
"+ogprime.getSomelist());

Here is the mapping entry:

<mapping>
<classA>pkg.code.ObjectG</classA>
<classB>pkg.code.ObjectGPrime</classB>
<field>
<A>strarray</A>
<B>somelist</B>
<destinationTypeHint>java.lang.String</destinationTypeHint>

</field>
</mapping>

Here is ObjectG:

package pkg.code;
public class ObjectG {
private String abc = "ABCD";
private String[] strarray = new String[4];
public static void main(String[] args) {
}
/**
* @return Returns the abc.
*/
public String getAbc() {
return abc;
}
/**
* @param abc The abc to set.
*/
public void setAbc(String abc) {
this.abc = abc;
}
/**
* @return Returns the starray.
*/
public String[] getStrarray() {
return strarray;
}
/**
* @param starray The starray to set.
*/
public void setStrarray(String[] strarray) {
this.strarray = strarray;
}
}

Here is ObjectGPrime:

package pkg.code;
import java.util.List;

public class ObjectGPrime {
private String abc;
private List somelist;
private String[] somearray;

/**
* @return Returns the somearray.
*/
public String[] getSomearray() {
return somearray;
}
/**
* @param somearray The somearray to set.
*/
public void setSomearray(String[] somearray) {
this.somearray = somearray;
}
public static void main(String[] args) {
}
/**
* @return Returns the abc.
*/
public String getAbc() {
return abc;
}
/**
* @param abc The abc to set.
*/
public void setAbc(String abc) {
this.abc = abc;
}
/**
* @return Returns the somelist.
*/
public List getSomelist() {
return somelist;
}
/**
* @param somelist The somelist to set.
*/
public void setSomelist(List somelist) {
this.somelist = somelist;
}
}

I modified your code for a fix we need in our situation:

Object destValue = null;
Iterator iter = sourceCollectionValue.iterator();
Object sourceValue = null;
// Fix for code RM112805
Class prevDestEntryType = null;
while (iter.hasNext()) {
sourceValue = iter.next();
if (destEntryType == null) {
if (sourceValue == null)
destEntryType = prevDestEntryType;
else
destEntryType =
fieldMap.getDestHintType(sourceValue.getClass());
}
// end fix code RM 112805

Discussion

  • Franz Garsombke

    Franz Garsombke - 2005-12-05
    • status: open --> pending
     
  • Franz Garsombke

    Franz Garsombke - 2005-12-05

    Logged In: YES
    user_id=550744

    Code is fixed and in CVS under v15 branch.

     
  • Franz Garsombke

    Franz Garsombke - 2005-12-09
    • status: pending --> closed
     

Log in to post a comment.

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks