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
lupin source share