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.

10
  • 2
    Why do you want to ignore NPEs instead of checking for a null value? Commented Dec 12, 2012 at 17:07
  • It would be something like try / catch on every single line... Commented Dec 12, 2012 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. Commented Dec 12, 2012 at 17:09
  • 1
    That sample code looks like a good candidate for being replaced with a loop and an array. Commented Dec 12, 2012 at 17:22
  • 1
    @Tomas - Mentioned! If you wouldn't mind though, if you have further comments on my answer, comment on my answer Commented Dec 12, 2012 at 17:52

5 Answers 5

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

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

Comments

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.

Comments

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) { } ... 

Comments

3

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

Comments

1

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.