Results 1 to 13 of 13

Thread: Error!

  1. #1
    AgentApe is offline Member
    Join Date
    Dec 2009
    Posts
    9
    Rep Power
    0

    Default 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");
            }                
    }

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,429
    Rep Power
    7

    Default

    Post the complete stack trace.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  3. #3
    AgentApe is offline Member
    Join Date
    Dec 2009
    Posts
    9
    Rep Power
    0

    Default

    Hmm?? I don't understand

  4. #4
    ocean is offline Senior Member
    Join Date
    Sep 2009
    Location
    Sweden/Borås
    Posts
    107
    Rep Power
    0

    Default

    What happens if you change
    Java Code:
     choice=in.next();
    to
    Java Code:
     choice ="this i think would work";

    If that works you probly will understand were the problem is.

  5. #5
    AgentApe is offline Member
    Join Date
    Dec 2009
    Posts
    9
    Rep Power
    0

    Default

    That does work but I don't understand :confused:

  6. #6
    ocean is offline Senior Member
    Join Date
    Sep 2009
    Location
    Sweden/Borås
    Posts
    107
    Rep Power
    0

    Default

    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;

  7. #7
    AgentApe is offline Member
    Join Date
    Dec 2009
    Posts
    9
    Rep Power
    0

    Default

    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");
            }                
    }

  8. #8
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    7

    Default

    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.

  9. #9
    AgentApe is offline Member
    Join Date
    Dec 2009
    Posts
    9
    Rep Power
    0

    Default

    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();
    }

  10. #10
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    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?

  11. #11
    AgentApe is offline Member
    Join Date
    Dec 2009
    Posts
    9
    Rep Power
    0

    Default

    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 03:53 AM.

  12. #12
    ocean is offline Senior Member
    Join Date
    Sep 2009
    Location
    Sweden/Borås
    Posts
    107
    Rep Power
    0

    Default

    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. :)

  13. #13
    AgentApe is offline Member
    Join Date
    Dec 2009
    Posts
    9
    Rep Power
    0

    Default

    The method works fine its calling it into the menu thats causing a nullpointerexception. I still do not see how to correct this.
    Last edited by AgentApe; 12-03-2009 at 01:14 PM.

Similar Threads

  1. Replies: 1
    Last Post: 10-26-2009, 05:38 PM
  2. Replies: 3
    Last Post: 01-23-2009, 09:53 AM
  3. error 530 error authentication required
    By rgale in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 05-12-2008, 05:28 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •