-
Error!
I'm currently constructing a menu and during the running of said menu I appear to be running into a null pointer exception. Can someone take a look at my code please and figure out why this is.
Code:
import java.util.*;
import java.io.*;
public class Application {
private Dictionary dictionary;
private Scanner in;
public Application() {
System.out.println("Welcome to the Dictionary");
}
public void runApp()throws IOException {
String choice;
char ch;
do{
this.printMenu();
choice=in.next(); NullPointerException!
ch=choice.charAt(0);
switch (ch) {
case 'P': case 'p':
System.out.println("Add a new phrase");
Phrases p=new Phrases();
p.readKeyboard();
dictionary.add(p);
System.out.println("New phrase added to dictionary");
break;
case 'L': case 'l':
System.out.println("Loading Dictionary");
dictionary.load("dic.txt");
System.out.println("Loaded Dictionary");
break;
case 'S': case 's':
System.out.println("Saving Dictionary");
dictionary.save("dic.txt");
System.out.println("Saved Dictionary");
break;
case 'C': case 'c':
dictionary.printout();
break;
case 'Q': case 'q':
break;
default:
System.out.println("Not an option");
}
} while ((ch !='Q')&(ch !='q'));
}
public void printMenu() {
System.out.println("These are the options avaliable to you: ");
System.out.println("P- Adds new phrase to dictionary");
System.out.println("L- Loads dictionary");
System.out.println("S- Saves Dictionary");
System.out.println("C- Printout contents of the dictionary");
System.out.println("Q- Quit dictionary");
}
public static void main(String args[]) throws IOException{
Application app=new Application();
app.runApp();
System.out.println("End of program");
}
}
-
Post the complete stack trace.
-
-
What happens if you change
to
Code:
choice ="this i think would work";
If that works you probly will understand were the problem is.
-
That does work but I don't understand :confused:
-
The problem is that
is empty. what happens if you move it down a bit. so you can read from keybord before you call it?
Code:
switch (ch) {
case 'P': case 'p':
System.out.println("Add a new phrase");
Phrases p=new Phrases();
p.readKeyboard();
choice=in.next(); <------- like this
ch=choice.charAt(0);
dictionary.add(p);
System.out.println("New phrase added to dictionary");
break;
-
Ok that error is fixed but another has popped up. When I try to run my load dictionary method fromt he menu it provides a NullPointerException in reference to the "pirate.txt" in the method. Can someone take another look over the code and explain why please.
Code:
import java.util.*;
import java.io.*;
public class Application {
private Dictionary dictionary;
private Scanner in;
public Application() {
System.out.println("Welcome to the Pirate Dictionary");
in=new Scanner (System.in);
}
public void runApp()throws IOException {
char ch;
do{
this.printMenu();
String choice=in.next();
ch=choice.charAt(0);
switch (ch) {
case 'P': case 'p':
System.out.println("Add a new phrase");
Phrases p=new Phrases();
p.readKeyboard();
dictionary.add(p);
System.out.println("New phrase added to dictionary");
break;
case 'L': case 'l':
System.out.println("Loading Dictionary");
dictionary.load("pirate.txt"); NullPointerException
System.out.println("Loaded Dictionary");
break;
case 'S': case 's':
System.out.println("Saving Dictionary");
dictionary.save("pirate.txt"); NullPointerException
System.out.println("Saved Dictionary");
break;
case 'C': case 'c':
dictionary.printout();
break;
case 'E': case 'e':
System.out.println("Enter English phrase to translate");
String english=in.next();
dictionary.searchE(english);
break;
case 'K': case 'k':
System.out.println("Enter Pirate phrase to translate");
String pirate=in.next();
dictionary.searchP(pirate);
break;
case 'Q': case 'q':
break;
default:
System.out.println("Not an option");
}
} while ((ch !='Q')&(ch !='q'));
}
public void printMenu() {
System.out.println("These are the options avaliable to you: ");
System.out.println("P- Adds new phrase to dictionary");
System.out.println("L- Loads dictionary");
System.out.println("S- Saves Dictionary");
System.out.println("C- Printout contents of the dictionary");
System.out.println("E- Translates english phrases to pirate");
System.out.println("K- Translates pirate phrases to english");
System.out.println("Q- Quit dictionary");
}
public static void main(String args[]) throws IOException{
Application app=new Application();
app.runApp();
System.out.println("End of program");
}
}
-
we need to see how your load method is set up to determine what's causing that. I'm assuming though you are just trying to pull pirates.txt not the complete file location name which could be giving the error.
-
Heres the load method and it works fine outside of the menu.
Code:
public void load(String pirateTxt) throws IOException{
Scanner infile=new Scanner(new InputStreamReader(
new FileInputStream(pirateTxt)));
int num=infile.nextInt();
infile.nextLine();
for(int i=0;i<num; i++){
String pi=infile.nextLine();
String e=infile.nextLine();
Phrases p=new Phrases(pi,e);
phrase.add(p);
}
infile.close();
}
-
Code:
dictionary.load("pirate.txt"); NullPointerException
The NullPointerException has nothing to do with the load() method. And this would have bee clear had you followed the advice in the very first reply and posted the complete stack trace.
The stack trace indicates on which line the NPE occurred. In the original problem in.next() caused an NPE because in was null.
How did you fix that?
What variable is the stack trace indicating is null now? How might you fix that?
-
What is and how do I use the stack trace to check which variable in the line is null?
Edit: Hmm it turns out all the parts of the menu that call a method from dictionary encite a NullPointerException, this may be very telling of what my problem exactly is?
-
Morning.
Are you reading a file or keybord input with your
Code:
public void load(String pirateTxt) throws IOException{
Why are you throwing IOException, and not catching em. Mabey my brain just still is in sleeps mode, need more coffe to wake up. :)
-
The method works fine its calling it into the menu thats causing a nullpointerexception. I still do not see how to correct this.