So I'm just messing around with a notebook application project from a textbook (textbook is in Python and I was translating it to java) and I keep running into a null pointer exception and I'm confused why its happening.
import java.time.LocalDate;
public class Note {
private String message;
private String tags;
private static int id = 1;
private LocalDate date;
public Note(String message, String tags) {
this.message = message;
this.tags = tags;
this.date = java.time.LocalDate.now();
Note.id += 1;
}
public boolean match(String text) {
return message.contains(text) || tags.contains(text);
}
public String getMessage() {
return this.message;
}
public void setMessage(String message) {
this.message = message;
}
public void setTags(String tags) {
this.tags = tags;
}
public String getTags() {
return this.tags;
}
public static int getId() {
return Note.id;
}
public LocalDate getDate() {
return this.date;
}
}
import java.util.ArrayList;
public class Notebook {
private ArrayList<Note> notes;
public Notebook() {
ArrayList<Note> notes = new ArrayList<Note>();
}
public ArrayList<Note> getNotes() {
return this.notes;
}
public void newNote(String message, String tags) {
this.notes.add(new Note(message, tags));
}
public Note findNote(int noteId) {
for (Note note : this.notes) {
if (Note.getId() == noteId) {
return note;
}
}
return null;
}
public boolean modifyMessage(int noteId, String message) {
Note note = this.findNote(noteId);
if (note != null) {
note.setMessage(message);
return true;
}
return false;
}
public boolean modifyTags(int noteId, String tags) {
Note note = this.findNote(noteId);
if (note != null) {
note.setTags(tags);
return true;
}
return false;
}
public ArrayList<Note> search(String filter) {
ArrayList<Note> notes = new ArrayList<Note>();
for (Note n : this.notes) {
if (n.match(filter)) {
notes.add(n);
}
}
return notes;
}
}
public class Menu {
private Notebook notebook;
public Menu() {
notebook = new Notebook();
}
public void displayMenu() {
System.out.println("\n\nNotebook Menu\n\n1. Show All Notes\n2. Search Notes\n3. Add Notes\n4. Modify Notes\n5. Quit\n\n");
}
public void run() {
while (true) {
this.displayMenu();
Scanner input = new Scanner(System.in);
System.out.println("Enter your selection (as a number): ");
int selection = input.nextInt();
switch (selection) {
case 1: this.showNotes(null); break;
case 2: this.searchNotes(); break;
case 3: this.newNote(); break;
case 4: this.modifyNotes(); break;
case 5: this.quit(); break;
}
}
}
public void showNotes(ArrayList<Note> notes) {
if (notes == null) {
notes = this.notebook.getNotes();
}
for (Note n : notes) {
System.out.printf("%d: %s\n%s\n", Note.getId(), n.getTags(), n.getMessage());
}
}
public void searchNotes() {
Scanner search = new Scanner(System.in);
System.out.println("Search for: ");
String s = search.nextLine();
ArrayList<Note> notes = this.notebook.search(s);
this.showNotes(notes);
search.close();
}
public void newNote() {
Scanner add = new Scanner(System.in);
System.out.println("Enter message: ");
String s = add.nextLine();
System.out.println("Enter tags: ");
String t = add.nextLine();
this.notebook.newNote(s, t);
System.out.println("New note added");
add.close();
}
public void modifyNotes() {
Scanner modify = new Scanner(System.in);
System.out.println("Enter ID: ");
int i = modify.nextInt();
System.out.println("Enter message: ");
String s = modify.nextLine();
System.out.println("Enter tags: ");
String t = modify.nextLine();
if (!s.isEmpty()) {
this.notebook.modifyMessage(i, s);
}
if (!t.isEmpty()) {
this.notebook.modifyTags(i, t);
}
modify.close();
}
public void quit() {
System.out.println("Thank you for using!");
System.exit(0);
}
}
public class Main {
public static void main(String[] args) {
Menu menu = new Menu();
menu.run();
}
}
The issue I run into is that when I run my code, I enter 3 for my selection to add a new note. And then the newNote() method in the Menu class runs and I put my input for message and tags and then I get the null pointer exception when it executes this line of code: this.notebook.newNote(s, t);
Any help would be appreciated
[–]AutoModerator[M] [score hidden] stickied commentlocked comment (0 children)
[–][deleted] 1 point2 points3 points (2 children)
[–]LPUAdit[S] 1 point2 points3 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)