0

I have a SerializableEnumeration and I am trying to add a vector elements into that SerializableEnumeration. Below is the code.

if(objTempVector !=null)
                {
                    CommonUtils.log("objTempVector:::::::::::::::::::::: objTempVector not null::::");

                try{

                    hlpOrder.mOrderDetail = new SerializableEnumeration(objTempVector);


                }
                catch(NullPointerException ex)
                {
                    CommonUtils.log("Exception in  SerializableEnumeration::::::::::::::::::::::" + ex.toString());

                } 
                }

but even though the vector is not null , the above code is generating null pointer exception.

error log: Exception in SerializableEnumeration::::::::::::::::::::::java.lang.NullPointerException

this is the method for SerializableEnumeration

public SerializableEnumeration(Vector data) {
            if (data == null) {
                size = 0;
            } else {
                this.data = data.toArray();
                this.size = data.size();
            }
            i = 0;
        }

Please help me to fix the problem.

This the whole class for SerializableEnumeration

import java.io.Serializable;
import java.util.Enumeration;
import java.util.NoSuchElementException;
import java.util.Vector;


    public class SerializableEnumeration implements Enumeration, Serializable {

    private static final long serialVersionUID = 1L;
    private Object[] data = null;
    private int i;
    private int size;

    public SerializableEnumeration(Vector data) {
        if (data == null) {
            size = 0;
        } else {
            this.data = data.toArray();
            this.size = data.size();
        }
        i = 0;
    }

    public boolean hasMoreElements() {
        return ((size > 0 ) ? true : false);
    }

    public Object nextElement() throws NoSuchElementException {
        try {
            Object element = data[i];
            ++i;
            --size;
            return element;
        } catch (Exception e) {
            throw new NoSuchElementException();
        }
    }

    public int getSize() {
        return size;
    }
    public void reset(){
        if(data==null){
            size=0;
        }else{
            size=data.length;
            i=0;
        }
    }

}

I am using the variable in a JSP File and when I trying to use the Enumeration I am getting the below error:

enter image description here

Sunil
  • 102
  • 1
  • 5
  • 19
  • can you show us the entire error you're getting – Edd Mar 28 '17 at 07:49
  • did you check if `hlpOrder` is not null ? Put a breakpoint on that line and check that out – Edd Mar 28 '17 at 08:07
  • What makes you think that the error comes from SerializableEnumeration? – Maurice Perry Mar 28 '17 at 09:02
  • @MauricePerry : I have added breakpoint and added try catch block to send the other variable to jsp page . Added the modified code now – Sunil Mar 28 '17 at 09:12
  • @Edd: I am adding value there. Please have a look on updated question. – Sunil Mar 28 '17 at 10:16
  • 1
    @Suman, I still think that `hlpOrder` can be the one that is null. I honestly cannot see what else could be null given that piece of code and what happens in SerializableEnumeration constructor. I think `hlpOrder` is null and when you do hlpOrder.mOrderDetail is like if you were doing null.mOrderDetail , therefore throwing NPE. Please confirm that is not the case – Edd Mar 28 '17 at 10:41
  • @Edd: Thanks , You were correct , hlporder was coming null. Thanks a lot for helping me. – Sunil Mar 28 '17 at 11:30
  • 1
    you got it mate – Edd Mar 28 '17 at 11:45

0 Answers0