Results 1 to 13 of 13
Thread: Error!
- 12-02-2009, 05:10 PM #1
Member
- Join Date
- Dec 2009
- Posts
- 9
- Rep Power
- 0
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.
Java 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"); } }
- 12-02-2009, 05:19 PM #2
Post the complete stack trace.
Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
The Ubiquitous Newbie Tips
- 12-02-2009, 05:34 PM #3
Member
- Join Date
- Dec 2009
- Posts
- 9
- Rep Power
- 0
Hmm?? I don't understand
- 12-02-2009, 05:46 PM #4
Senior Member
- Join Date
- Sep 2009
- Location
- Sweden/Borås
- Posts
- 107
- Rep Power
- 0
What happens if you change
toJava Code:choice=in.next();
Java Code:choice ="this i think would work";
If that works you probly will understand were the problem is.
- 12-02-2009, 06:13 PM #5
Member
- Join Date
- Dec 2009
- Posts
- 9
- Rep Power
- 0
That does work but I don't understand :confused:
- 12-02-2009, 08:12 PM #6
Senior Member
- Join Date
- Sep 2009
- Location
- Sweden/Borås
- Posts
- 107
- Rep Power
- 0
The problem is that
Java Code:choice=in.next();
is empty. what happens if you move it down a bit. so you can read from keybord before you call it?
Java 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;
- 12-02-2009, 10:53 PM #7
Member
- Join Date
- Dec 2009
- Posts
- 9
- Rep Power
- 0
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.
Java 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"); } }
- 12-02-2009, 10:59 PM #8
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.
Liberty has never come from the government.
Liberty has always come from the subjects of government.
The history of liberty is the history of resistance.
The history of liberty is a history of the limitation of governmental power, not the increase of it.
- 12-02-2009, 11:23 PM #9
Member
- Join Date
- Dec 2009
- Posts
- 9
- Rep Power
- 0
Heres the load method and it works fine outside of the menu.
Java 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(); }
- 12-02-2009, 11:46 PM #10
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,561
- Rep Power
- 11
Java 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?
- 12-03-2009, 02:03 AM #11
Member
- Join Date
- Dec 2009
- Posts
- 9
- Rep Power
- 0
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?Last edited by AgentApe; 12-03-2009 at 02:53 AM.
- 12-03-2009, 06:22 AM #12
Senior Member
- Join Date
- Sep 2009
- Location
- Sweden/Borås
- Posts
- 107
- Rep Power
- 0
Morning.
Are you reading a file or keybord input with your
Java 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. :)
- 12-03-2009, 11:27 AM #13
Member
- Join Date
- Dec 2009
- Posts
- 9
- Rep Power
- 0
Similar Threads
-
java.lang.Error: Error opening DSound for capture
By NARs in forum NetworkingReplies: 1Last Post: 10-26-2009, 04:38 PM -
Diference Between compiler error Garbage collection and Runtime Error?
By makpandian in forum New To JavaReplies: 3Last Post: 01-23-2009, 08:53 AM -
error 530 error authentication required
By rgale in forum JavaServer Pages (JSP) and JSTLReplies: 0Last Post: 05-12-2008, 04:28 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks