SlideShare a Scribd company logo
1 of 7
Download to read offline
How to fix this error:
Exception in thread "main" q. Exit
java.lang.NullPointerException: Cannot invoke "String.hashCode()" because "action" is null
at AddressBook.main(AddressBook.java:23)
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 THIS
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 to fix this error-   Exception in thread -main- q- Exit java-lang-.pdf

More Related Content

Similar to How to fix this error- Exception in thread -main- q- Exit java-lang-.pdf

How do I fix it in LinkedList.javaLabProgram.javaLinkedList.jav.pdf
How do I fix it in LinkedList.javaLabProgram.javaLinkedList.jav.pdfHow do I fix it in LinkedList.javaLabProgram.javaLinkedList.jav.pdf
How do I fix it in LinkedList.javaLabProgram.javaLinkedList.jav.pdf
mail931892
 
import java-util--- public class MyLinkedList{ public static void.pdf
import java-util---  public class MyLinkedList{    public static void.pdfimport java-util---  public class MyLinkedList{    public static void.pdf
import java-util--- public class MyLinkedList{ public static void.pdf
asarudheen07
 
Note             Given Code modified as required and required met.pdf
Note             Given Code modified as required and required met.pdfNote             Given Code modified as required and required met.pdf
Note             Given Code modified as required and required met.pdf
Ankitchhabra28
 
Here is the editable codeSolutionimport java.util.NoSuchEleme.pdf
Here is the editable codeSolutionimport java.util.NoSuchEleme.pdfHere is the editable codeSolutionimport java.util.NoSuchEleme.pdf
Here is the editable codeSolutionimport java.util.NoSuchEleme.pdf
arrowmobile
 
Hi, Please find my code.I have correted all of your classes.Plea.pdf
Hi, Please find my code.I have correted all of your classes.Plea.pdfHi, Please find my code.I have correted all of your classes.Plea.pdf
Hi, Please find my code.I have correted all of your classes.Plea.pdf
pritikulkarni20
 
How do I fix it in LinkedList.javathis is what i didLabProgra.pdf
How do I fix it in LinkedList.javathis is what i didLabProgra.pdfHow do I fix it in LinkedList.javathis is what i didLabProgra.pdf
How do I fix it in LinkedList.javathis is what i didLabProgra.pdf
mail931892
 
please help with java questionsJAVA CODEplease check my code and.pdf
please help with java questionsJAVA CODEplease check my code and.pdfplease help with java questionsJAVA CODEplease check my code and.pdf
please help with java questionsJAVA CODEplease check my code and.pdf
arishmarketing21
 
in this assignment you are asked to write a simple driver program an.pdf
in this assignment you are asked to write a simple driver program an.pdfin this assignment you are asked to write a simple driver program an.pdf
in this assignment you are asked to write a simple driver program an.pdf
michardsonkhaicarr37
 
So I have this code(StackInAllSocks) and I implemented the method but.pdf
So I have this code(StackInAllSocks) and I implemented the method but.pdfSo I have this code(StackInAllSocks) and I implemented the method but.pdf
So I have this code(StackInAllSocks) and I implemented the method but.pdf
aksahnan
 
Using NetBeansImplement a queue named QueueLL using a Linked List .pdf
Using NetBeansImplement a queue named QueueLL using a Linked List .pdfUsing NetBeansImplement a queue named QueueLL using a Linked List .pdf
Using NetBeansImplement a queue named QueueLL using a Linked List .pdf
siennatimbok52331
 
could you implement this function please, im having issues with it..pdf
could you implement this function please, im having issues with it..pdfcould you implement this function please, im having issues with it..pdf
could you implement this function please, im having issues with it..pdf
feroz544
 
Sorted number list implementation with linked listsStep 1 Inspec.pdf
 Sorted number list implementation with linked listsStep 1 Inspec.pdf Sorted number list implementation with linked listsStep 1 Inspec.pdf
Sorted number list implementation with linked listsStep 1 Inspec.pdf
almaniaeyewear
 
Having a problem figuring out where my errors are- The code is not run.pdf
Having a problem figuring out where my errors are- The code is not run.pdfHaving a problem figuring out where my errors are- The code is not run.pdf
Having a problem figuring out where my errors are- The code is not run.pdf
NicholasflqStewartl
 
public class CircularDoublyLinkedList-E- implements List-E- { privat.pdf
public class CircularDoublyLinkedList-E- implements List-E- {   privat.pdfpublic class CircularDoublyLinkedList-E- implements List-E- {   privat.pdf
public class CircularDoublyLinkedList-E- implements List-E- { privat.pdf
ChristopherkUzHunter
 
Implement a queue using a linkedlist (java)SolutionLinkedQueue.pdf
Implement a queue using a linkedlist (java)SolutionLinkedQueue.pdfImplement a queue using a linkedlist (java)SolutionLinkedQueue.pdf
Implement a queue using a linkedlist (java)SolutionLinkedQueue.pdf
kostikjaylonshaewe47
 
mainpublic class AssignmentThree {    public static void ma.pdf
mainpublic class AssignmentThree {    public static void ma.pdfmainpublic class AssignmentThree {    public static void ma.pdf
mainpublic class AssignmentThree {    public static void ma.pdf
fathimafancyjeweller
 
PLEASE MAKE SURE THE PROGRAM IS ASKING FOR INPUT FROM USER TO ADD OR.pdf
PLEASE MAKE SURE THE PROGRAM IS ASKING FOR INPUT FROM USER TO ADD OR.pdfPLEASE MAKE SURE THE PROGRAM IS ASKING FOR INPUT FROM USER TO ADD OR.pdf
PLEASE MAKE SURE THE PROGRAM IS ASKING FOR INPUT FROM USER TO ADD OR.pdf
mallik3000
 
I have created a class hasdhedDictionary that implements the Diction.pdf
I have created a class hasdhedDictionary that implements the Diction.pdfI have created a class hasdhedDictionary that implements the Diction.pdf
I have created a class hasdhedDictionary that implements the Diction.pdf
allystraders
 
Modify HuffmanTree.java and HuffmanNode.java to allow the user to se.pdf
Modify HuffmanTree.java and HuffmanNode.java to allow the user to se.pdfModify HuffmanTree.java and HuffmanNode.java to allow the user to se.pdf
Modify HuffmanTree.java and HuffmanNode.java to allow the user to se.pdf
arjuncorner565
 

Similar to How to fix this error- Exception in thread -main- q- Exit java-lang-.pdf (20)

How do I fix it in LinkedList.javaLabProgram.javaLinkedList.jav.pdf
How do I fix it in LinkedList.javaLabProgram.javaLinkedList.jav.pdfHow do I fix it in LinkedList.javaLabProgram.javaLinkedList.jav.pdf
How do I fix it in LinkedList.javaLabProgram.javaLinkedList.jav.pdf
 
import java-util--- public class MyLinkedList{ public static void.pdf
import java-util---  public class MyLinkedList{    public static void.pdfimport java-util---  public class MyLinkedList{    public static void.pdf
import java-util--- public class MyLinkedList{ public static void.pdf
 
Note             Given Code modified as required and required met.pdf
Note             Given Code modified as required and required met.pdfNote             Given Code modified as required and required met.pdf
Note             Given Code modified as required and required met.pdf
 
Here is the editable codeSolutionimport java.util.NoSuchEleme.pdf
Here is the editable codeSolutionimport java.util.NoSuchEleme.pdfHere is the editable codeSolutionimport java.util.NoSuchEleme.pdf
Here is the editable codeSolutionimport java.util.NoSuchEleme.pdf
 
Hi, Please find my code.I have correted all of your classes.Plea.pdf
Hi, Please find my code.I have correted all of your classes.Plea.pdfHi, Please find my code.I have correted all of your classes.Plea.pdf
Hi, Please find my code.I have correted all of your classes.Plea.pdf
 
How do I fix it in LinkedList.javathis is what i didLabProgra.pdf
How do I fix it in LinkedList.javathis is what i didLabProgra.pdfHow do I fix it in LinkedList.javathis is what i didLabProgra.pdf
How do I fix it in LinkedList.javathis is what i didLabProgra.pdf
 
please help with java questionsJAVA CODEplease check my code and.pdf
please help with java questionsJAVA CODEplease check my code and.pdfplease help with java questionsJAVA CODEplease check my code and.pdf
please help with java questionsJAVA CODEplease check my code and.pdf
 
in this assignment you are asked to write a simple driver program an.pdf
in this assignment you are asked to write a simple driver program an.pdfin this assignment you are asked to write a simple driver program an.pdf
in this assignment you are asked to write a simple driver program an.pdf
 
So I have this code(StackInAllSocks) and I implemented the method but.pdf
So I have this code(StackInAllSocks) and I implemented the method but.pdfSo I have this code(StackInAllSocks) and I implemented the method but.pdf
So I have this code(StackInAllSocks) and I implemented the method but.pdf
 
Using NetBeansImplement a queue named QueueLL using a Linked List .pdf
Using NetBeansImplement a queue named QueueLL using a Linked List .pdfUsing NetBeansImplement a queue named QueueLL using a Linked List .pdf
Using NetBeansImplement a queue named QueueLL using a Linked List .pdf
 
could you implement this function please, im having issues with it..pdf
could you implement this function please, im having issues with it..pdfcould you implement this function please, im having issues with it..pdf
could you implement this function please, im having issues with it..pdf
 
Sorted number list implementation with linked listsStep 1 Inspec.pdf
 Sorted number list implementation with linked listsStep 1 Inspec.pdf Sorted number list implementation with linked listsStep 1 Inspec.pdf
Sorted number list implementation with linked listsStep 1 Inspec.pdf
 
Having a problem figuring out where my errors are- The code is not run.pdf
Having a problem figuring out where my errors are- The code is not run.pdfHaving a problem figuring out where my errors are- The code is not run.pdf
Having a problem figuring out where my errors are- The code is not run.pdf
 
public class CircularDoublyLinkedList-E- implements List-E- { privat.pdf
public class CircularDoublyLinkedList-E- implements List-E- {   privat.pdfpublic class CircularDoublyLinkedList-E- implements List-E- {   privat.pdf
public class CircularDoublyLinkedList-E- implements List-E- { privat.pdf
 
Implement a queue using a linkedlist (java)SolutionLinkedQueue.pdf
Implement a queue using a linkedlist (java)SolutionLinkedQueue.pdfImplement a queue using a linkedlist (java)SolutionLinkedQueue.pdf
Implement a queue using a linkedlist (java)SolutionLinkedQueue.pdf
 
mainpublic class AssignmentThree {    public static void ma.pdf
mainpublic class AssignmentThree {    public static void ma.pdfmainpublic class AssignmentThree {    public static void ma.pdf
mainpublic class AssignmentThree {    public static void ma.pdf
 
PLEASE MAKE SURE THE PROGRAM IS ASKING FOR INPUT FROM USER TO ADD OR.pdf
PLEASE MAKE SURE THE PROGRAM IS ASKING FOR INPUT FROM USER TO ADD OR.pdfPLEASE MAKE SURE THE PROGRAM IS ASKING FOR INPUT FROM USER TO ADD OR.pdf
PLEASE MAKE SURE THE PROGRAM IS ASKING FOR INPUT FROM USER TO ADD OR.pdf
 
I have created a class hasdhedDictionary that implements the Diction.pdf
I have created a class hasdhedDictionary that implements the Diction.pdfI have created a class hasdhedDictionary that implements the Diction.pdf
I have created a class hasdhedDictionary that implements the Diction.pdf
 
TypeScript Introduction
TypeScript IntroductionTypeScript Introduction
TypeScript Introduction
 
Modify HuffmanTree.java and HuffmanNode.java to allow the user to se.pdf
Modify HuffmanTree.java and HuffmanNode.java to allow the user to se.pdfModify HuffmanTree.java and HuffmanNode.java to allow the user to se.pdf
Modify HuffmanTree.java and HuffmanNode.java to allow the user to se.pdf
 

More from aarokyaaqua

Quiz Question 1 (2 points) Which of the following is true about data.pdf
Quiz Question 1 (2 points)   Which of the following is true about data.pdfQuiz Question 1 (2 points)   Which of the following is true about data.pdf
Quiz Question 1 (2 points) Which of the following is true about data.pdf
aarokyaaqua
 
help with RR Hypothesis Experiment What is your independent variable (.pdf
help with RR Hypothesis Experiment What is your independent variable (.pdfhelp with RR Hypothesis Experiment What is your independent variable (.pdf
help with RR Hypothesis Experiment What is your independent variable (.pdf
aarokyaaqua
 
Anatomy and Physiology 1- The epidermis is the most superficial layer.pdf
Anatomy and Physiology 1- The epidermis is the most superficial layer.pdfAnatomy and Physiology 1- The epidermis is the most superficial layer.pdf
Anatomy and Physiology 1- The epidermis is the most superficial layer.pdf
aarokyaaqua
 

More from aarokyaaqua (20)

Quiz Question 1 (2 points) Which of the following is true about data.pdf
Quiz Question 1 (2 points)   Which of the following is true about data.pdfQuiz Question 1 (2 points)   Which of the following is true about data.pdf
Quiz Question 1 (2 points) Which of the following is true about data.pdf
 
Refer to Question 10 in Chapter 17 of the textbook- How is the use of.pdf
Refer to Question 10 in Chapter 17 of the textbook- How is the use of.pdfRefer to Question 10 in Chapter 17 of the textbook- How is the use of.pdf
Refer to Question 10 in Chapter 17 of the textbook- How is the use of.pdf
 
Problem 1- NYC Restaurants Dataset In Problem 1- you will be working w.pdf
Problem 1- NYC Restaurants Dataset In Problem 1- you will be working w.pdfProblem 1- NYC Restaurants Dataset In Problem 1- you will be working w.pdf
Problem 1- NYC Restaurants Dataset In Problem 1- you will be working w.pdf
 
Q3-2) Pick all true statements- We can easily detect metamorphic worm.pdf
Q3-2) Pick all true statements- We can easily detect metamorphic worm.pdfQ3-2) Pick all true statements- We can easily detect metamorphic worm.pdf
Q3-2) Pick all true statements- We can easily detect metamorphic worm.pdf
 
only these choices are available Here are comparative financial state.pdf
only these choices are available  Here are comparative financial state.pdfonly these choices are available  Here are comparative financial state.pdf
only these choices are available Here are comparative financial state.pdf
 
Look up the Exxon Valdez scandal on the Internet- then answer the foll.pdf
Look up the Exxon Valdez scandal on the Internet- then answer the foll.pdfLook up the Exxon Valdez scandal on the Internet- then answer the foll.pdf
Look up the Exxon Valdez scandal on the Internet- then answer the foll.pdf
 
Matching I A- epiphysis B- medullary cavity C- carpal G- vertebra D- a.pdf
Matching I A- epiphysis B- medullary cavity C- carpal G- vertebra D- a.pdfMatching I A- epiphysis B- medullary cavity C- carpal G- vertebra D- a.pdf
Matching I A- epiphysis B- medullary cavity C- carpal G- vertebra D- a.pdf
 
help with RR Hypothesis Experiment What is your independent variable (.pdf
help with RR Hypothesis Experiment What is your independent variable (.pdfhelp with RR Hypothesis Experiment What is your independent variable (.pdf
help with RR Hypothesis Experiment What is your independent variable (.pdf
 
Anatomy and Physiology 1- The epidermis is the most superficial layer.pdf
Anatomy and Physiology 1- The epidermis is the most superficial layer.pdfAnatomy and Physiology 1- The epidermis is the most superficial layer.pdf
Anatomy and Physiology 1- The epidermis is the most superficial layer.pdf
 
A- Collect Bitcoin price data (monthly)- calculate monthly returns- Co.pdf
A- Collect Bitcoin price data (monthly)- calculate monthly returns- Co.pdfA- Collect Bitcoin price data (monthly)- calculate monthly returns- Co.pdf
A- Collect Bitcoin price data (monthly)- calculate monthly returns- Co.pdf
 
e geyser Old Faithful in Yellowstone National Park alternates periods.pdf
e geyser Old Faithful in Yellowstone National Park alternates periods.pdfe geyser Old Faithful in Yellowstone National Park alternates periods.pdf
e geyser Old Faithful in Yellowstone National Park alternates periods.pdf
 
A financial analyst is interested in estimating the proportion of publ.pdf
A financial analyst is interested in estimating the proportion of publ.pdfA financial analyst is interested in estimating the proportion of publ.pdf
A financial analyst is interested in estimating the proportion of publ.pdf
 
8- In a study of helicopter usage and patient survival- among the 42-3.pdf
8- In a study of helicopter usage and patient survival- among the 42-3.pdf8- In a study of helicopter usage and patient survival- among the 42-3.pdf
8- In a study of helicopter usage and patient survival- among the 42-3.pdf
 
Define the following terms- and explain it in your own words with exam.pdf
Define the following terms- and explain it in your own words with exam.pdfDefine the following terms- and explain it in your own words with exam.pdf
Define the following terms- and explain it in your own words with exam.pdf
 
Complete the following tables- UnitAbbreviationdpsdpmcurie millicu.pdf
Complete the following tables-   UnitAbbreviationdpsdpmcurie   millicu.pdfComplete the following tables-   UnitAbbreviationdpsdpmcurie   millicu.pdf
Complete the following tables- UnitAbbreviationdpsdpmcurie millicu.pdf
 
2- Identify in the statement of position the assets and liabilities se (1).pdf
2- Identify in the statement of position the assets and liabilities se (1).pdf2- Identify in the statement of position the assets and liabilities se (1).pdf
2- Identify in the statement of position the assets and liabilities se (1).pdf
 
1-3 (4 points) L is the language over the alphabet {a-b-c} given by L-.pdf
1-3 (4 points) L is the language over the alphabet {a-b-c} given by L-.pdf1-3 (4 points) L is the language over the alphabet {a-b-c} given by L-.pdf
1-3 (4 points) L is the language over the alphabet {a-b-c} given by L-.pdf
 
vijay ananth has just been hired as the cio at wilshire fina.pdf
vijay ananth has just been hired as the cio at wilshire fina.pdfvijay ananth has just been hired as the cio at wilshire fina.pdf
vijay ananth has just been hired as the cio at wilshire fina.pdf
 
Which of the Android lifecycle methods is called when the ac.pdf
Which of the Android lifecycle methods is called when the ac.pdfWhich of the Android lifecycle methods is called when the ac.pdf
Which of the Android lifecycle methods is called when the ac.pdf
 
Using the original tables below if a DBMS is using an DELET.pdf
Using the original tables below if a DBMS is using an DELET.pdfUsing the original tables below if a DBMS is using an DELET.pdf
Using the original tables below if a DBMS is using an DELET.pdf
 

Recently uploaded

QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lessonQUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
httgc7rh9c
 
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
AnaAcapella
 

Recently uploaded (20)

Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 
Simple, Complex, and Compound Sentences Exercises.pdf
Simple, Complex, and Compound Sentences Exercises.pdfSimple, Complex, and Compound Sentences Exercises.pdf
Simple, Complex, and Compound Sentences Exercises.pdf
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
Model Attribute _rec_name in the Odoo 17
Model Attribute _rec_name in the Odoo 17Model Attribute _rec_name in the Odoo 17
Model Attribute _rec_name in the Odoo 17
 
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lessonQUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
 
OS-operating systems- ch05 (CPU Scheduling) ...
OS-operating systems- ch05 (CPU Scheduling) ...OS-operating systems- ch05 (CPU Scheduling) ...
OS-operating systems- ch05 (CPU Scheduling) ...
 
Introduction to TechSoup’s Digital Marketing Services and Use Cases
Introduction to TechSoup’s Digital Marketing  Services and Use CasesIntroduction to TechSoup’s Digital Marketing  Services and Use Cases
Introduction to TechSoup’s Digital Marketing Services and Use Cases
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
Economic Importance Of Fungi In Food Additives
Economic Importance Of Fungi In Food AdditivesEconomic Importance Of Fungi In Food Additives
Economic Importance Of Fungi In Food Additives
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdfFICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
 
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxExploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
 
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
 
dusjagr & nano talk on open tools for agriculture research and learning
dusjagr & nano talk on open tools for agriculture research and learningdusjagr & nano talk on open tools for agriculture research and learning
dusjagr & nano talk on open tools for agriculture research and learning
 
How to Add a Tool Tip to a Field in Odoo 17
How to Add a Tool Tip to a Field in Odoo 17How to Add a Tool Tip to a Field in Odoo 17
How to Add a Tool Tip to a Field in Odoo 17
 

How to fix this error- Exception in thread -main- q- Exit java-lang-.pdf

  • 1. How to fix this error: Exception in thread "main" q. Exit java.lang.NullPointerException: Cannot invoke "String.hashCode()" because "action" is null at AddressBook.main(AddressBook.java:23) 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 THIS 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; } }