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?

mazhar islam
  • 5,561
  • 3
  • 20
  • 41
wind_gan
  • 132
  • 9
  • Your output is inconsistent with the code you have posted. – Tim Biegeleisen Jul 13 '15 at 08:31
  • 5
    What is there in the getValue() method? – Ouney Jul 13 '15 at 08:31
  • 5
    what does your `getValue()`method do? – griFlo Jul 13 '15 at 08:32
  • I guess we will need a [Verifiable Example](http://stackoverflow.com/help/mcve), because this shouldn't ideally happen. – Codebender Jul 13 '15 at 08:32
  • 7
    I think the Exception appears inside the `getValue()` method. – maja Jul 13 '15 at 08:32
  • Mean that the `value` object is `null` – bmscomp Jul 13 '15 at 08:33
  • 2
    @bmscomp: There's *several* things guaranteeing that `value` itself isn't `null` in that code block. – Makoto Jul 13 '15 at 08:34
  • getValue() method returns String – wind_gan Jul 13 '15 at 08:36
  • do you paste your pojo class code – Man Programmer Jul 13 '15 at 08:36
  • 1
    Add your `AttributeValue` class into your question. It'll be a *lot* faster to look at that than to throw darts at the wall, which is what we've been reduced to for now. – Makoto Jul 13 '15 at 08:37
  • my suggestion will be try to recompile your project and run it again, there is no possibility to have this output with provided code. unless there is some doggy code in `getValue` method – user902383 Jul 13 '15 at 08:37
  • 5
    Add the exception stack trace, please. – Tagir Valeev Jul 13 '15 at 08:37
  • possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – Madhawa Priyashantha Jul 13 '15 at 08:40
  • 2
    With all those null checks we can safely say that the `value` isn't null and isn't causing the `NullPointerException`. Like @maja said, the exception occurs inside the `getValue()` method. Could you post the code of everything inside that method and, more importantly, could you post the entire `StackTrace` of the error like @TagirValeev suggested. The `StackTrace` will point you to the place where the exception is actually occuring and will most likely also state what object is null inside that method. By posting a `StackTrace` the people at StackOverflow can help you better with Exceptions. – Kevin Cruijssen Jul 13 '15 at 08:51

1 Answers1

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.

Community
  • 1
  • 1
mazhar islam
  • 5,561
  • 3
  • 20
  • 41
  • 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. – Makoto Jul 13 '15 at 08:50
  • Shame on me. You are right: `getValues()` method produces NPE. – wind_gan Jul 13 '15 at 08:57