2

I have the following code:

        Map<BigInteger, AttributeValue> values = getParameters(elementHandler);

        AttributeValue value = values.get(attrID);
        AttributeValue auxValue = null;
        if (auxAttrID != null)
            auxValue = values.get(auxAttrID);

        try {
            if (value == null) {
                // some code here
            }
            if (value != null) {
                assert (value != null) : "value is null";
                if (value.getValue() == null ) {
                    // some code here
                } else if (auxAttrID != null && auxValue != null) {
                    // some code here
                }
            }
        } catch (Exception e) {
            log.error("Error at getting attribute value (attr#" + attrID + ", auxAttrId#" + auxAttrID + ")", e);
        }
        return value;
    }

It produces NullPointerException at line

if (value.getValue() == null ) 

right after assertion.

AttributeValue is a simple POJO class. Why this happens and how can I fix it?

14
  • 5
    What is there in the getValue() method? Commented Jul 13, 2015 at 8:31
  • 5
    what does your getValue()method do? Commented Jul 13, 2015 at 8:32
  • 7
    I think the Exception appears inside the getValue() method. Commented Jul 13, 2015 at 8:32
  • 2
    @bmscomp: There's several things guaranteeing that value itself isn't null in that code block. Commented Jul 13, 2015 at 8:34
  • 5
    Add the exception stack trace, please. Commented Jul 13, 2015 at 8:37

1 Answer 1

1

It produces NullPointerException at line if (value.getValue() == null )

if (value != null) {
    //...
    if (value.getValue() == null ) {
        //....

The above if condition states that, value is not NULL when you are invoking value.getValue(). Still you are getting NullPointerException for value.getValue() means the exception is coming from method getValue() (e.g you are trying to access any class attributes with a NULL object etc).

Edit your question with the implementation of getValue().

Use getStackTrace() to find out where the problem occurs. Read this for more information.

Sign up to request clarification or add additional context in comments.

2 Comments

Yes, you're not technically wrong, but this much is definitely obvious to us all. It's not really explaining what specifically is causing the NPE, and until we get more information, we don't know that information.
Shame on me. You are right: getValues() method produces NPE.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.