[NEUTRON-54] Null Pointer Exception while converting from Boolean to boolean Created: 09/Jul/15  Updated: 11/Feb/16  Resolved: 11/Feb/16

Status: Resolved
Project: neutron
Component/s: neutron-spi
Affects Version/s: master
Fix Version/s: None

Type: Bug
Reporter: Ruijing Guo Assignee: Ruijing Guo
Resolution: Won't Do Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Operating System: All
Platform: All


External issue ID: 3968

 Description   

in neutron-spi/src/main/java/org/opendaylight/neutron/spi/NeutronNetwork.java, Null Pointer Exception while converting from Boolean to Boolean as:

example:

public boolean isAdminStateUp()

{ return adminStateUp; }

//Null Pointer Exception when adminStateUp is null.

Test Case:

vagrant@node1:~/neutron$ cat TestA.java; javac TestA.java ; java TestA
public class TestA {
Boolean adminStateUp;

public boolean isAdminStateUp()

{ return adminStateUp; }

public static void main(String[] args) {
TestA a = new TestA();
if (a.isAdminStateUp())

{ System.out.println("here"); }

}
}
Exception in thread "main" java.lang.NullPointerException
at TestA.isAdminStateUp(TestA.java:5)
at TestA.main(TestA.java:10)



 Comments   
Comment by Isaku Yamahata [ 09/Jul/15 ]

This applies to other classes in the directory.
We should address consistently as a whole.

There is two interfaces

  • boolean isXXX() with implicit conversion
    It may results in null pointer exception.
  • Boolean getXXX() may return null pointer with the current implementation.
    Is null pointer check the responsibility of the caller?

possible approach I can think of would be

  • add default initialization value.
    i.e. Boolean adminStateUp = false

This changes the semantics of Boolean getXXX(). it doesn't return null anymore.
If we go for this route, other T getXXX() should have same semantics.
(where T = String, Interger etc.)

  • add null pointer check to all of boolean isXXX()
  • Drop all of boolean isXXX()
  • any other better way?

thoughts?

Comment by Ruijing Guo [ 10/Jul/15 ]

in NeutronNetwork.java, I like to change:

1. Change public initDefaults to private function and construction call initDefaults

2. deprecated instead of drop isXXX or getXXX.

Comment by Robert Varga [ 21/Nov/15 ]

The following would work, too:

public boolean isAdminStateUp()

{ return Boolean.TRUE.equals(adminStateUp); }

The question is whether you need to differentiate the tri-state nature (up, down, unknown?). If not, I would suggest also changing the field type to 'boolean', as that is more memory-efficient.

Comment by Isaku Yamahata [ 11/Feb/16 ]

Since this issue was hit during Ruijing was working on his modification and there is no actual impact on the existing code.
So we won't fix this bug.

If this is hit as actual bug, please re-open this bug.

Generated at Wed Feb 07 20:25:26 UTC 2024 using Jira 8.20.10#820010-sha1:ace47f9899e9ee25d7157d59aa17ab06aee30d3d.