public class Solution {
public static String[] strings;
public static void main(String[] args) {
//write your code here
strings = new String[6];
String temp = "Somethig";
Scanner console = new Scanner(System.in);
for (int x = 0; x < 6; x++) {
strings[x] = console.nextLine();
}
for (int x = 0; x < 5; x++) {
if (strings[x].equals(null)) continue;
temp = strings[x];
for (int y = x + 1; y < strings.length -1; y++) {
if (temp.equals(strings[y])) strings[y] = null;
}
}
for (int i = 0; i < strings.length; i++) {
//System.out.print(strings[i] + ", ");
}
}
}
Why does my code results to NullPointerException
Under discussion
Comments (1)
- Popular
- New
- Old
You must be signed in to leave a comment
Lisa L
5 June 2022, 13:27
you check for null but that's not done right
if (strings[x].equals(null)) continue;
equals requires an object. If you invoke it on null then a NullPointerException is thrown.
// try that code to see what I mean
0