How do I fix this dereference of nullable error?
up vote
1
down vote
favorite
I've been trying to get rid of the dereference of a possible null-reference graph for the longest time, but nothing seems to work.
This is my original code:
if (!pluto.get(boxer.getName()).contains(boxer)) {
pluto.get(boxer.getName()).add(boxer);
}
where pluto is private final Map> pluto;
and I have also tried
if (pluto.get(boxer.getName()) != null) {
if (!pluto.get(boxer.getName()).contains(boxer)) {
pluto.get(boxer.getName()).add(boxer);
}
}
But this also doesn't work. Please help.
[dereference.of.nullable] dereference of possibly-null reference pluto.get(boxer.getName())
java null dereference
add a comment |
up vote
1
down vote
favorite
I've been trying to get rid of the dereference of a possible null-reference graph for the longest time, but nothing seems to work.
This is my original code:
if (!pluto.get(boxer.getName()).contains(boxer)) {
pluto.get(boxer.getName()).add(boxer);
}
where pluto is private final Map> pluto;
and I have also tried
if (pluto.get(boxer.getName()) != null) {
if (!pluto.get(boxer.getName()).contains(boxer)) {
pluto.get(boxer.getName()).add(boxer);
}
}
But this also doesn't work. Please help.
[dereference.of.nullable] dereference of possibly-null reference pluto.get(boxer.getName())
java null dereference
What pluto.get(boxer.getName()) returns?
– efex09
Nov 8 at 5:44
can you please share full code?
– GauravRai1512
Nov 8 at 5:45
it returns the set of strings that it is mapped to. I don't know why, but stack overflow removed my implementation for my graph. it is private final Map<String, Set<String>> pluto
– Adrian Markoe
Nov 8 at 5:45
There might be two possibility one map is null and another possibility is boxer.getName is null also you can verify by run your code is debug mode so you can inspect the element and check what is getting null.
– GauravRai1512
Nov 8 at 5:50
You would need to check if pluto is null or if boxer is null before calling "if (pluto.get(boxer.getName()) != null)". If any of them is null it will throw a NullpointerException.
– cokeby190
Nov 8 at 9:39
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I've been trying to get rid of the dereference of a possible null-reference graph for the longest time, but nothing seems to work.
This is my original code:
if (!pluto.get(boxer.getName()).contains(boxer)) {
pluto.get(boxer.getName()).add(boxer);
}
where pluto is private final Map> pluto;
and I have also tried
if (pluto.get(boxer.getName()) != null) {
if (!pluto.get(boxer.getName()).contains(boxer)) {
pluto.get(boxer.getName()).add(boxer);
}
}
But this also doesn't work. Please help.
[dereference.of.nullable] dereference of possibly-null reference pluto.get(boxer.getName())
java null dereference
I've been trying to get rid of the dereference of a possible null-reference graph for the longest time, but nothing seems to work.
This is my original code:
if (!pluto.get(boxer.getName()).contains(boxer)) {
pluto.get(boxer.getName()).add(boxer);
}
where pluto is private final Map> pluto;
and I have also tried
if (pluto.get(boxer.getName()) != null) {
if (!pluto.get(boxer.getName()).contains(boxer)) {
pluto.get(boxer.getName()).add(boxer);
}
}
But this also doesn't work. Please help.
[dereference.of.nullable] dereference of possibly-null reference pluto.get(boxer.getName())
java null dereference
java null dereference
asked Nov 8 at 5:40
Adrian Markoe
155
155
What pluto.get(boxer.getName()) returns?
– efex09
Nov 8 at 5:44
can you please share full code?
– GauravRai1512
Nov 8 at 5:45
it returns the set of strings that it is mapped to. I don't know why, but stack overflow removed my implementation for my graph. it is private final Map<String, Set<String>> pluto
– Adrian Markoe
Nov 8 at 5:45
There might be two possibility one map is null and another possibility is boxer.getName is null also you can verify by run your code is debug mode so you can inspect the element and check what is getting null.
– GauravRai1512
Nov 8 at 5:50
You would need to check if pluto is null or if boxer is null before calling "if (pluto.get(boxer.getName()) != null)". If any of them is null it will throw a NullpointerException.
– cokeby190
Nov 8 at 9:39
add a comment |
What pluto.get(boxer.getName()) returns?
– efex09
Nov 8 at 5:44
can you please share full code?
– GauravRai1512
Nov 8 at 5:45
it returns the set of strings that it is mapped to. I don't know why, but stack overflow removed my implementation for my graph. it is private final Map<String, Set<String>> pluto
– Adrian Markoe
Nov 8 at 5:45
There might be two possibility one map is null and another possibility is boxer.getName is null also you can verify by run your code is debug mode so you can inspect the element and check what is getting null.
– GauravRai1512
Nov 8 at 5:50
You would need to check if pluto is null or if boxer is null before calling "if (pluto.get(boxer.getName()) != null)". If any of them is null it will throw a NullpointerException.
– cokeby190
Nov 8 at 9:39
What pluto.get(boxer.getName()) returns?
– efex09
Nov 8 at 5:44
What pluto.get(boxer.getName()) returns?
– efex09
Nov 8 at 5:44
can you please share full code?
– GauravRai1512
Nov 8 at 5:45
can you please share full code?
– GauravRai1512
Nov 8 at 5:45
it returns the set of strings that it is mapped to. I don't know why, but stack overflow removed my implementation for my graph. it is private final Map<String, Set<String>> pluto
– Adrian Markoe
Nov 8 at 5:45
it returns the set of strings that it is mapped to. I don't know why, but stack overflow removed my implementation for my graph. it is private final Map<String, Set<String>> pluto
– Adrian Markoe
Nov 8 at 5:45
There might be two possibility one map is null and another possibility is boxer.getName is null also you can verify by run your code is debug mode so you can inspect the element and check what is getting null.
– GauravRai1512
Nov 8 at 5:50
There might be two possibility one map is null and another possibility is boxer.getName is null also you can verify by run your code is debug mode so you can inspect the element and check what is getting null.
– GauravRai1512
Nov 8 at 5:50
You would need to check if pluto is null or if boxer is null before calling "if (pluto.get(boxer.getName()) != null)". If any of them is null it will throw a NullpointerException.
– cokeby190
Nov 8 at 9:39
You would need to check if pluto is null or if boxer is null before calling "if (pluto.get(boxer.getName()) != null)". If any of them is null it will throw a NullpointerException.
– cokeby190
Nov 8 at 9:39
add a comment |
2 Answers
2
active
oldest
votes
up vote
0
down vote
What if your map is null?
try this:
if (isNull(pluto)) { // import static java.util.Objects.isNull;
return; // to minimise nesting
}
Optional.ofNullable(pluto.get(boxer.getName()))
.filter(value -> !value.isEmpty())
.ifPresent(value -> { /*do whatever you want*/ });
add a comment |
up vote
0
down vote
Try this. You need to check for nulls.
if (pluto != null && pluto.get(boxer.getName()) != null && !pluto.get(boxer.getName()).contains(boxer)) {
pluto.get(boxer.getName()).add(boxer);
}
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
What if your map is null?
try this:
if (isNull(pluto)) { // import static java.util.Objects.isNull;
return; // to minimise nesting
}
Optional.ofNullable(pluto.get(boxer.getName()))
.filter(value -> !value.isEmpty())
.ifPresent(value -> { /*do whatever you want*/ });
add a comment |
up vote
0
down vote
What if your map is null?
try this:
if (isNull(pluto)) { // import static java.util.Objects.isNull;
return; // to minimise nesting
}
Optional.ofNullable(pluto.get(boxer.getName()))
.filter(value -> !value.isEmpty())
.ifPresent(value -> { /*do whatever you want*/ });
add a comment |
up vote
0
down vote
up vote
0
down vote
What if your map is null?
try this:
if (isNull(pluto)) { // import static java.util.Objects.isNull;
return; // to minimise nesting
}
Optional.ofNullable(pluto.get(boxer.getName()))
.filter(value -> !value.isEmpty())
.ifPresent(value -> { /*do whatever you want*/ });
What if your map is null?
try this:
if (isNull(pluto)) { // import static java.util.Objects.isNull;
return; // to minimise nesting
}
Optional.ofNullable(pluto.get(boxer.getName()))
.filter(value -> !value.isEmpty())
.ifPresent(value -> { /*do whatever you want*/ });
edited Nov 8 at 5:55
answered Nov 8 at 5:47
Abhishek
337114
337114
add a comment |
add a comment |
up vote
0
down vote
Try this. You need to check for nulls.
if (pluto != null && pluto.get(boxer.getName()) != null && !pluto.get(boxer.getName()).contains(boxer)) {
pluto.get(boxer.getName()).add(boxer);
}
add a comment |
up vote
0
down vote
Try this. You need to check for nulls.
if (pluto != null && pluto.get(boxer.getName()) != null && !pluto.get(boxer.getName()).contains(boxer)) {
pluto.get(boxer.getName()).add(boxer);
}
add a comment |
up vote
0
down vote
up vote
0
down vote
Try this. You need to check for nulls.
if (pluto != null && pluto.get(boxer.getName()) != null && !pluto.get(boxer.getName()).contains(boxer)) {
pluto.get(boxer.getName()).add(boxer);
}
Try this. You need to check for nulls.
if (pluto != null && pluto.get(boxer.getName()) != null && !pluto.get(boxer.getName()).contains(boxer)) {
pluto.get(boxer.getName()).add(boxer);
}
answered Nov 8 at 5:56
efex09
1809
1809
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53202104%2fhow-do-i-fix-this-dereference-of-nullable-error%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
What pluto.get(boxer.getName()) returns?
– efex09
Nov 8 at 5:44
can you please share full code?
– GauravRai1512
Nov 8 at 5:45
it returns the set of strings that it is mapped to. I don't know why, but stack overflow removed my implementation for my graph. it is private final Map<String, Set<String>> pluto
– Adrian Markoe
Nov 8 at 5:45
There might be two possibility one map is null and another possibility is boxer.getName is null also you can verify by run your code is debug mode so you can inspect the element and check what is getting null.
– GauravRai1512
Nov 8 at 5:50
You would need to check if pluto is null or if boxer is null before calling "if (pluto.get(boxer.getName()) != null)". If any of them is null it will throw a NullpointerException.
– cokeby190
Nov 8 at 9:39