SlideShare a Scribd company logo
How do I fix this error "
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "String.hashCode()"
because "action" is null
at AddressBook.main(AddressBook.java:25)"
import java.util.Scanner;
public class AddressBook {
public static void main(String[] args) {
Tablea addressBook = new Tablea();
Scanner scanner = new Scanner(System.in);
String name, address;
String action = null;
boolean moreEntries = true;
while (moreEntries) {
System.out.println("/nMenu");
System.out.println("n.Add Entry");
System.out.println("d. Delete Entry");
System.out.println("u. Update Entry");
System.out.println("l. Search");
System.out.println("a. View All");
System.out.println("q. Exit");
// Insert a new entry into the address book
switch (action) {
case "n":
System.out.print("Enter a name: ");
name = scanner.nextLine();
System.out.print("Enter an address: ");
address = scanner.nextLine();
addressBook.insert(name, address);
System.out.println("New Contact has been added,");
System.out.print("Add another entry? (y/n): ");
String answer = scanner.nextLine().toLowerCase();
moreEntries = answer.equals("y") || answer.equals("yes");
break;
// Delete an entry from the address book
case "d":
System.out.println("Enter a name to delete: ");
name = scanner.nextLine();
boolean deleted = addressBook.delete(name);
if (deleted) {
System.out.println("Address deleted");
} else {
System.out.println("Name not found");
}
break;
// Update an entry in the address book
case "u":
System.out.print("Enter a name to update: ");
name = scanner.nextLine();
System.out.print("Enter a new address: ");
address = scanner.nextLine();
boolean updated = addressBook.update(name, address);
if (updated) {
System.out.println("Address updated");
} else {
System.out.println("Name not found");
}
break;
// Lookup an entry in the address book
case "l":
System.out.print("Enter a name to look up: ");
name = scanner.nextLine();
String result = addressBook.lookUp(name);
if (result != null) {
System.out.println("Address: " + result);
} else {
System.out.println("Name not found");
}
break;
// Display all entries in the address book
case "a":
System.out.println("All entries:");
addressBook.displayAll();
break;
// Quit the program
case"q":
System.out.println("Quitting...");
}
}
}
}
Other files that go with it
public class Node {
private String name;
private String address;
private Node next;
Node() {
// add here ..
}
Node(String name, String address) {
// add here ..
this.name = name;
this.address = address;
this.next = null;
}
public String getKey() {
// add here ..
return this.name;
}
public void setKey(String name) {
// add here ..
this.name = name;
}
public String getValue() {
// add here ..
return this.address;
}
public void setValue(String address) {
// add here ..
this.address = address;
}
public Node getNext() {
// add here ..
return this.next;
}
public void setNext(Node next) {
// add here ..
this.next = next;
}
}
import java.util.Scanner;
public class Tablea {
private Node mark;
public Node getMark() {
return this.mark;
}
public void setMark(Node mark) {
this.mark = mark;
}
public boolean insert(String name, String address) {
Node newNode= new Node(name,address);
if (this.mark== null) {
this.mark = newNode;
}else {
newNode.setNext(this.mark.getNext());
this.mark.setNext(newNode);
}
return true;
}
public String lookUp(String name) {
Node current = this.mark;
while(current!=null) {
if (current.getKey().equals(name)) {
return current.getValue();
}
current = current.getNext();
}
return null;
}
public boolean delete(String name) {
Node current = this.mark;
Node prev = null;
while (current!= null) {
if (current.getKey().equals(name)) {
if (prev == null) {
this.mark = current.getNext();
}else {
prev.setNext(current.getNext());
}
return true;
}
prev = current;
current = current.getNext();
}
return false;
}
public boolean update(String name, String newValue) {
Node current = this.mark;
while (current != null) {
if (current.getKey().equals(name)) {
current.setValue(newValue);
return true;
}
current = current.getNext();
}
return false;
}
public boolean markToStart() {
if (this.mark== null) {
return false;
}
this.mark = null;
return true;
}
public boolean advanceMark() {
if (this.mark== null) {
return false;
}
this.mark = this.mark.getNext();
return true;
}
public String keyAtMark() {
if (this.mark== null) {
return null;
}
return this.mark.getKey();
}
public String valueAtMark() {
if (this.mark== null) {
return null;
}
return this.mark.getValue();
}
public int displayAll() {
Node current = this.mark;
int count = 0;
while (current != null) {
System.out.println(current.getKey()+":"+ current);
}
return count;
}
}
How do I fix this error - Exception in thread -main- java-lang-NullPoi.pdf

More Related Content

PDF
How to fix this error- Exception in thread -main- q- Exit java-lang-.pdf
PDF
There is something wrong with my program-- (once I do a for view all t.pdf
PDF
why will it not display all of the entries-- output -nMenu n-Add Entr.pdf
PDF
How can I add multiple names and addresses- To the following code- Tab.pdf
PDF
How do you update an address according to this code- Currently- I fig.pdf
DOCX
java experiments and programs
PDF
[10] Write a Java application which first reads data from the phoneb.pdf
PDF
Modify this code to change the underlying data structure to .pdf
How to fix this error- Exception in thread -main- q- Exit java-lang-.pdf
There is something wrong with my program-- (once I do a for view all t.pdf
why will it not display all of the entries-- output -nMenu n-Add Entr.pdf
How can I add multiple names and addresses- To the following code- Tab.pdf
How do you update an address according to this code- Currently- I fig.pdf
java experiments and programs
[10] Write a Java application which first reads data from the phoneb.pdf
Modify this code to change the underlying data structure to .pdf

More from pnaran46 (19)

PDF
John works as a goldsmith who often repairs jewellry- Maria brought in.pdf
PDF
L-{anbmcmdnn-m0} (1).pdf
PDF
I need help to modify my code according to the instructions- Modify th.pdf
PDF
Given that x has a Poitson destribition with -1-7- what is the poobati.pdf
PDF
Find the p-value using Excel (not Appendix D)- (Round your answers to.pdf
PDF
Apple Inc- had gone through a hard period in the early 90s and almost.pdf
PDF
6) Cytological mapping (4 points) i) Deletional-deficiency-ii) Duplica.pdf
PDF
a- Let- a host with IP address 10-0-0-10-3550 want to transmit 16 bit.pdf
PDF
57) All of the following are non-arguments except- a- Americans are ma.pdf
PDF
2- An instructor wishes to see whether the variation in scores of the.pdf
PDF
You are working as the chief economist for the Ministry of Economy in.pdf
PDF
The Taylors agreed to make monthly payments on a mortgage of $130-000.pdf
PDF
The supply of low-skilled workers in China is perfectly elastic- In 20.pdf
PDF
The Federal Reserve provides accounts to financial institutions only f.pdf
PDF
The following is the balance sheet numbers (in millions) reported by B.pdf
PDF
1) Ludwig von Mises (1871 - 1973) was an influential economist from Au.pdf
PDF
Singh Company reports a contribution margin of $717-000 and fixed cost.pdf
PDF
Provide an appropriate response- How many ways can five people- A- B-.pdf
PDF
QRT Soltware creates and distributes inventory control noftware- The h.pdf
John works as a goldsmith who often repairs jewellry- Maria brought in.pdf
L-{anbmcmdnn-m0} (1).pdf
I need help to modify my code according to the instructions- Modify th.pdf
Given that x has a Poitson destribition with -1-7- what is the poobati.pdf
Find the p-value using Excel (not Appendix D)- (Round your answers to.pdf
Apple Inc- had gone through a hard period in the early 90s and almost.pdf
6) Cytological mapping (4 points) i) Deletional-deficiency-ii) Duplica.pdf
a- Let- a host with IP address 10-0-0-10-3550 want to transmit 16 bit.pdf
57) All of the following are non-arguments except- a- Americans are ma.pdf
2- An instructor wishes to see whether the variation in scores of the.pdf
You are working as the chief economist for the Ministry of Economy in.pdf
The Taylors agreed to make monthly payments on a mortgage of $130-000.pdf
The supply of low-skilled workers in China is perfectly elastic- In 20.pdf
The Federal Reserve provides accounts to financial institutions only f.pdf
The following is the balance sheet numbers (in millions) reported by B.pdf
1) Ludwig von Mises (1871 - 1973) was an influential economist from Au.pdf
Singh Company reports a contribution margin of $717-000 and fixed cost.pdf
Provide an appropriate response- How many ways can five people- A- B-.pdf
QRT Soltware creates and distributes inventory control noftware- The h.pdf
Ad

Recently uploaded (20)

PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
Basic Mud Logging Guide for educational purpose
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
RMMM.pdf make it easy to upload and study
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
Pre independence Education in Inndia.pdf
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
01-Introduction-to-Information-Management.pdf
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PPTX
PPH.pptx obstetrics and gynecology in nursing
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Basic Mud Logging Guide for educational purpose
Microbial disease of the cardiovascular and lymphatic systems
STATICS OF THE RIGID BODIES Hibbelers.pdf
RMMM.pdf make it easy to upload and study
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
Supply Chain Operations Speaking Notes -ICLT Program
Pre independence Education in Inndia.pdf
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
FourierSeries-QuestionsWithAnswers(Part-A).pdf
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
human mycosis Human fungal infections are called human mycosis..pptx
01-Introduction-to-Information-Management.pdf
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
Abdominal Access Techniques with Prof. Dr. R K Mishra
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PPH.pptx obstetrics and gynecology in nursing
Ad

How do I fix this error - Exception in thread -main- java-lang-NullPoi.pdf

  • 1. How do I fix this error " Exception in thread "main" java.lang.NullPointerException: Cannot invoke "String.hashCode()" because "action" is null at AddressBook.main(AddressBook.java:25)" import java.util.Scanner; public class AddressBook { public static void main(String[] args) { Tablea addressBook = new Tablea(); Scanner scanner = new Scanner(System.in); String name, address; String action = null; boolean moreEntries = true; while (moreEntries) { System.out.println("/nMenu"); System.out.println("n.Add Entry"); System.out.println("d. Delete Entry"); System.out.println("u. Update Entry"); System.out.println("l. Search"); System.out.println("a. View All"); System.out.println("q. Exit"); // Insert a new entry into the address book switch (action) { case "n": System.out.print("Enter a name: "); name = scanner.nextLine(); System.out.print("Enter an address: "); address = scanner.nextLine(); addressBook.insert(name, address); System.out.println("New Contact has been added,"); System.out.print("Add another entry? (y/n): "); String answer = scanner.nextLine().toLowerCase(); moreEntries = answer.equals("y") || answer.equals("yes");
  • 2. break; // Delete an entry from the address book case "d": System.out.println("Enter a name to delete: "); name = scanner.nextLine(); boolean deleted = addressBook.delete(name); if (deleted) { System.out.println("Address deleted"); } else { System.out.println("Name not found"); } break; // Update an entry in the address book case "u": System.out.print("Enter a name to update: "); name = scanner.nextLine(); System.out.print("Enter a new address: "); address = scanner.nextLine(); boolean updated = addressBook.update(name, address); if (updated) { System.out.println("Address updated"); } else { System.out.println("Name not found"); } break; // Lookup an entry in the address book case "l": System.out.print("Enter a name to look up: "); name = scanner.nextLine(); String result = addressBook.lookUp(name); if (result != null) { System.out.println("Address: " + result); } else {
  • 3. System.out.println("Name not found"); } break; // Display all entries in the address book case "a": System.out.println("All entries:"); addressBook.displayAll(); break; // Quit the program case"q": System.out.println("Quitting..."); } } } } Other files that go with it public class Node { private String name; private String address; private Node next; Node() { // add here .. } Node(String name, String address) { // add here .. this.name = name; this.address = address; this.next = null; } public String getKey() { // add here .. return this.name; }
  • 4. public void setKey(String name) { // add here .. this.name = name; } public String getValue() { // add here .. return this.address; } public void setValue(String address) { // add here .. this.address = address; } public Node getNext() { // add here .. return this.next; } public void setNext(Node next) { // add here .. this.next = next; } } import java.util.Scanner; public class Tablea { private Node mark; public Node getMark() { return this.mark; } public void setMark(Node mark) { this.mark = mark; } public boolean insert(String name, String address) { Node newNode= new Node(name,address); if (this.mark== null) { this.mark = newNode; }else {
  • 5. newNode.setNext(this.mark.getNext()); this.mark.setNext(newNode); } return true; } public String lookUp(String name) { Node current = this.mark; while(current!=null) { if (current.getKey().equals(name)) { return current.getValue(); } current = current.getNext(); } return null; } public boolean delete(String name) { Node current = this.mark; Node prev = null; while (current!= null) { if (current.getKey().equals(name)) { if (prev == null) { this.mark = current.getNext(); }else { prev.setNext(current.getNext()); } return true; } prev = current; current = current.getNext(); } return false; } public boolean update(String name, String newValue) { Node current = this.mark; while (current != null) { if (current.getKey().equals(name)) { current.setValue(newValue); return true; } current = current.getNext(); } return false; }
  • 6. public boolean markToStart() { if (this.mark== null) { return false; } this.mark = null; return true; } public boolean advanceMark() { if (this.mark== null) { return false; } this.mark = this.mark.getNext(); return true; } public String keyAtMark() { if (this.mark== null) { return null; } return this.mark.getKey(); } public String valueAtMark() { if (this.mark== null) { return null; } return this.mark.getValue(); } public int displayAll() { Node current = this.mark; int count = 0; while (current != null) { System.out.println(current.getKey()+":"+ current); } return count; } }