Note that there are some explanatory texts on larger screens.

plurals
  1. POJava Quick Array Error
    text
    copied!<p>I'm having a little trouble with a simple code. It is suppose to be a program where people can add Notes that get stored in an array. I know this code is long but hopefully some can help me out.</p> <pre><code>public class NoteOrganizer { int action = 0; public static Note[] myArray; public static void addNotes(int num) { String note; String date; for(int z = 0; z &lt;= num; z++) { Scanner getLi = new Scanner(System.in); System.out.println("Please enter a note (max 140 characters): \n"); note = getLi.nextLine(); System.out.println("Please enter a date:\n"); date = getLi.nextLine(); Note test = new Note(); test.id = z; test.myNote = note; test.date = date; myArray[z] = test; // THE ERROR IS IN THIS LINE, NOT THE LINE MENTIONED BEFORE } } public static void main(String[] args) { int action = 0; int y = 0; Scanner getLi = new Scanner(System.in); System.out.println("Please press 1 to add notes, 2 to delete notes or 3 to view " + "all notes:\n"); action = getLi.nextInt(); if(action == 1) { System.out.println("How many notes would you like to add: \n"); int d = getLi.nextInt(); //myArray = new Note[d]; addNotes(d); //System.out.println(myArray[0].print()); } else if(action == 3) { System.out.println(Arrays.toString(myArray)); } } } </code></pre> <p>The error that I am getting is the </p> <pre><code>Exception in thread "main" java.lang.NullPointerException at note.organizer.NoteOrganizer.addNotes(NoteOrganizer.java:46) at note.organizer.NoteOrganizer.main(NoteOrganizer.java:95) Java Result: 1 </code></pre> <p>I commented which line the error was in.</p> <p>Any help is greatly appreciated.</p> <p>Thanks,</p>
 

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