Note that there are some explanatory texts on larger screens.

plurals
  1. POJava - Unhandled exception error when using "continue" in a for loop?
    primarykey
    data
    text
    <p>This is a program that stores 10 unique strings. If the user enters a string that already exists in the array, the user will get an error. My code is working perfectly for the first string that I enter, but throws an exception after that and I don't know why. How do I fix this and make it work?</p> <p>P.S. I don't want to use a Set. I want to do it with an array.</p> <p>Edit: Error name: Exception in thread "main" java.lang.NullPointerException Java Result: 1</p> <p>Thanks.</p> <pre><code> public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); int stringNumber = 0; String[] stringArray = new String[10]; for (int i = 0; i &lt;= stringArray.length; i++) { boolean itemExists = false; out.println("\nEnter a string"); String input = keyboard.next(); if (i &gt; 0) { for (int j = 0; j &lt; stringArray.length; j++) { if (stringArray[j].equalsIgnoreCase(input)) { itemExists = true; out.println("Item \"" + input + "\" already exists."); break; } else { continue; // Unhandled exception error. If I don't have "continue" here, the program doesn't work properly after the first number. } } } if (itemExists == false) { stringArray[stringNumber] = input; out.println("\"" + stringArray[stringNumber] + "\"" + " has been stored."); } else { out.println("Try again."); } PrintArray(stringArray); stringNumber++; } } public static void PrintArray(String[] stringArray) { for (int i = 0; i &lt;= 9; i++) { if (stringArray[i] == null) { out.print(" "); } else { out.print("\nYour strings:"); out.print(" " +stringArray[i] + " "); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload