NullPointerException and the best way to handle this

Note. This homework / assignment is not responding if you do not want it.

Ok after some searching and reading this data:

How to check if an array element is null to avoid a NullPointerException in Java Gracefully avoiding a NullPointerException in Java http://c2.com/cgi/wiki?NullPointerException

I'm still not making any progress on how to deal with the NullPointerException error in my code, snippet for questionable code:

int findElement(String element) { int retval = 0; for ( int i = 0; i < setElements.length; i++) { if ( setElements[i].equals(element) ) { // This line 31 here return retval = i; } else { return retval = -1; } } return retval; } void add(String newValue) { int elem = findElement(newValue); if( numberOfElements < maxNumberOfElements && elem != -1 ) { setElements[numberOfElements] = newValue; numberOfElements++; } else { System.out.println("Element " + newValue + "already exist"); } } 

It compiles, but adding a new element to the set raises a NullPointerException error.

 D:\javaprojects>java SetDemo Enter string element to be added A You entered A Exception in thread "main" java.lang.NullPointerException at Set.findElement(Set.java:31) at Set.add(Set.java:44) at SetDemo.main(Set.java:145) 

I added one more check, although, to be honest, I don’t know if it has the right to line 31. if (setElements! = Null && setElements [i] .equals (element)), but it's still not a joy.

Documentation / advice or explanation is appreciated.

training, lupine

+4
source share
7 answers

Have you initialized setElements anywhere? Value:

 String[] setElements = new String[100]; 

If you just declare an array variable:

 String[] setElements; 

as a data element of your class, it is initialized to null . You must point to something. You can do it inline:

 public class MyClass { private String[] setElements = new String[100]; ... } 

or in the constructor:

 public class MyClass { private String[] setElements; public MyClass() { setElements = new String[100]; } ... } 
+4
source

This should be setElements[i] != null && setElements[i].equals(element) . If the collection contains null elements, you will try to dereference the null reference when you call the equals method on that element.

As for NullPointerException - you should never catch it. For things that should not be empty, they must be properly initialized. For those things that cannot be empty, they should be checked for zero before dereferencing them (i.e. Name methods on them).

The only use case for catching NullPointerException is when you use a third-party library for which you have no source and have an error that throws a NullPointerException . These cases are rare, and as you are just starting to learn Java, forget that I mentioned this and focused on more important things.

+3
source

For a loop in findElement does not make sense.

 for ( int i = 0; i < setElements.length; i++) { if ( setElements[i].equals(element) ) { // This line 31 here return retval = i; } else { return retval = -1; } } 

You must iterate over all values ​​before returning -1, only then you know that the set does not have an element corresponding to element .

+3
source

Publish an entire class - this snippet is useless.

You make two serious mistakes: do not believe the compiler and believe that your code is correct.

If the JVM tells you that line 31 is a problem, believe it.

I assume that setElements[i] is null.

+3
source

Try testing the element itself for a null, not an array:

 setElements[i] != null && setElements[i].equals(element) 
+1
source

You should not try to catch the null pointer exception. Instead, the best way to avoid null pointers is:

  • In any function that accepts parameters where you assume that the parameter is not null, always check that the parameter is not null and throw an IllegalArgumentException if it is null.
  • Whenever you call a function that does not allow null parameters, make sure that you do not pass a null pointer to this function; if you already know that the object is not null (since you already checked it and selected the IllegalArgumentException), you do not need to double-check; otherwise, you must double check that the object is not null before passing it.

Since you are not checking the parameters on your findElement element and adding functions, it is entirely possible that the parameters are the culprits. Add the appropriate check and throw an IllegalArgumentException if they are zero. If after that you get an IllegalArgumentException, you will solve your problem. If not, then you at least know that the problem is not a parameter and is located elsewhere in the code.

+1
source

Now it works, thanks to Lars, Igor and others who took the time to criticize the code, there was a logical error that was not verified, anyway, the corrected working code, finally, I worry that I'm cheating ?: (

 int findElement(String element) { int retval = 0; for ( int i = 0; i < setElements.length; i++) { //loop first to the array and only return -1 once we can't find it. //setElements[i] != null is the NullPointerException killer :) if ( setElements[i] != null && setElements[i].equals(element) ) { return retval = i; } retval = -1; } return retval; } void add(String newValue) { int elem = findElement(newValue); if( numberOfElements < maxNumberOfElements && elem == -1 ) { # == instead of != as I only need to add if elements is non-existing setElements[numberOfElements] = newValue; numberOfElements++; } } 

with thanks, lupine

0
source

Source: https://habr.com/ru/post/1309496/


All Articles