1

Suppose I have 10 lines of code. Any line maybe throw a NullPointerException. I don't want to care about these exceptions. If a line throws the exceptions, I want the executor jump to next line and go forward. Could I do this on Java? if yes, please give me some sample code.

UPDATE: (add sample source for more clear question)

first.setText(prize.first);
second.setText(prize.second);
third.setText(prize.third);
fourth.setText(prize.fourth);
fifth.setText(prize.fifth);
sixth.setText(prize.sixth);
seventh.setText(prize.seventh);
eighth.setText(prize.eighth);

Suppose I have 8 lines of code above. What I want is: if the line 4 (or 5, 6,...) throws an exception, all other lines of code works normally. Of course I can use try...catch to catch the exceptions line by line. But this way make my source very complex.

Nguyen Minh Binh
  • 23,891
  • 30
  • 115
  • 165
  • 2
    Why do you want to ignore NPEs instead of checking for a null value? – Chris Forrence Dec 12 '12 at 17:07
  • It would be something like `try / catch` on every single line... – dantuch Dec 12 '12 at 17:08
  • 4
    Exceptions shouldn't be used to hide mistakes which you don't want to find/fix. You should determine why you'd get NullPointerExceptions and fix it, not hide it. – Steve's a D Dec 12 '12 at 17:09
  • I have added sample codes on my question. Please check it for the reason I post this question. THanks – Nguyen Minh Binh Dec 12 '12 at 17:17
  • Why exactly would those lines throw NPEs? For example, if you have `first.setText(prize.first);`, what variable would be null? `first` or `prize`? – Natix Dec 12 '12 at 17:20
  • 1
    That sample code looks like a good candidate for being replaced with a loop and an array. – Boann Dec 12 '12 at 17:22
  • @NguyenMinhBinh Those are EditTexts? Why do you let them be null?? – Natix Dec 12 '12 at 17:27
  • EditText is just an sample here. The sense could be make with any object such as String – Nguyen Minh Binh Dec 12 '12 at 17:28
  • Passing nulls around like this can be dangerous. It relies on all your code having appropriate try { } catch block/null-checks and if you miss any, you'll have some nice run-time bugs to deal with. Better to catch them/check for them and then handle appropriately. – matt freake Dec 12 '12 at 17:35
  • 1
    @Tomas - Mentioned! If you wouldn't mind though, if you have further comments on my answer, comment on my answer – Chris Forrence Dec 12 '12 at 17:52

5 Answers5

7

This is impossible. If you think about it for a while, you'll realize that that is also a nonsense. Take this code for example:

String s = getSomeString(); // assigns null
int length = s.length; // throws NPE

if (length > 10) { // what would you do here?? length is un-initialized
    domeSomething();
}

Edit

Now that you've updated you question with a code snippet, there are two general approaches to deal with nulls:

1. Disallow them where they aren't supposed to be

In your code snippet, you've provided a list of EditTexts, where any o them might be null. Well, in my opinion if you have an Activity in Android which should have some Views, then it is a programming error if any of them is null for whatever reason. An activity without some of its views can't work properly, so throwing a NPE and exiting the application is a perfectly sensible thing to do.

The NPE tells you "hey, you've made a mistake and you have to fix it". Until then, it is not safe for the app in an unpredicted state to continue execution, because no one knows what can happen, what data can become corrupted.

2. Allow them where they make sense, and provide null-checks

There are some places where you can have variables whose value can be empty. There are several approaches how to express an empty value and null is the most obvious one.

In that case, you can't just ignore the possibilty of a NPE and you have to do some null-checks. But again, a NPE is not your enemy. It will tell you that you've forgotten to account for all the possible situations that you should have.

Of course, repeated null checks make the code cluttered and noisy, so you can create some methods that generalize the null checks for you. I could not come up with a sensible example, so take this method only for illustration:

public void clearEditTexts(EditText... editTexts) {
    for (EditText e : editTexts) {
        if (e != null) {
            e.setText("");
        }
    }
}

This method clears the contents of all the passed EditTexts. Any of them can be null, but this method does these null-checks internally, so you don't have to do it manually for each of them.


This question is also a very good reading about handling nulls: Avoiding != null statements

Community
  • 1
  • 1
Natix
  • 14,017
  • 7
  • 54
  • 69
6

This is not possible. If your variables can be null you will just have to check for that, or prevent them from being null. Very rarely it can be useful to create a dummy object implementation whose overridden methods do nothing, so it can be used in place of a valid object instead of null.

Boann
  • 48,794
  • 16
  • 117
  • 146
3

No, you cant and you should care, they try to tell you whats wrong.

MustSeeMelons
  • 737
  • 1
  • 10
  • 24
3

The fact that a variable is null is notable, and should not be ignored. Therefore, you'd want something like this:

...
if(first != null && prize.first != null)
{
    first.setText(prize.first);
}
...

If, for some reason, you absolutely need to throw NPEs, you would need to surround each line with its own try/catch; there's no way to automatically catch exceptions and have it advance to the next line. It's not, however, recommended to do this.

...
try
{
    first.setText(prize.first);
}
catch(NullPointerException e)
{
}
try
{
    second.setText(prize.second);
}
catch(NullPointerException e)
{
}
...
Chris Forrence
  • 10,042
  • 11
  • 48
  • 64
1

NullPointerException is a runtime (unchecked) exception and Java not reccomand to catch unchecked exceptions. You should have a solution to prevent it.

someone
  • 6,577
  • 7
  • 37
  • 60