Results 1 to 7 of 7
  1. #1
    wdh321 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    116
    Rep Power
    0

    Default Another Exception in thread "main" java.lang.NullPointerException problem

    Sorry for another question on this, a different issue this time. I have a class RegistryInterface which is the menu interface for a Registry, and i also have Registry app which has a main method and calls the RegistryInterface methods. When i run the RegistryApp program below i get to the addStudent menu, i can enter the names but when it gets to storing the names in the registry i get the message below:

    Exception in thread "main" java.lang.NullPointerException
    at RegistryInterface.doAddStudent(RegistryInterface.j ava:49)
    at RegistryInterface.doMenu(RegistryInterface.java:23 )
    at RegistryApp.main(RegistryApp.java:11)
    Java Result: 1

    I think the problem is to do with creating and/or updating the Registry but im not sure why and how to fix it . . Thanks for any help

    RegistryInterface
    Java Code:
    import java.util.Scanner;
    public class RegistryInterface 
    {
        private Registry aRegistry = null;
        Scanner in = new Scanner(System.in);
        
        public RegistryInterface(Registry aRegistry)
        {
           aRegistry = new Registry();
        }
        
        public void doMenu(Registry aRegistry)
        {
            System.out.println("Registry Main Menu");
            System.out.println("******************");
            System.out.println("1. Add a Student");
            System.out.println("2. Delete a Student");
            System.out.println("3. Print Registry");
            System.out.println("4. Quit");
            System.out.print("\nSelect option [1, 2, 3, 4] :> ");
            int option = in.nextInt();
            if(option == 1)
                doAddStudent();
            else if(option == 2)
                doDeleteStudent();
            else if(option == 3)
                doPrintRegistry();
            else if(option == 4)
                doQuit();
            else
                System.out.println("Incorrect option selected");
            
    
            
        }
        
        private void doAddStudent()
        {
            boolean add = true;
            int studentID = 0;
            while(add)
            {
                System.out.println("Add New Student");
                System.out.println("***************");
                System.out.print("\nEnter forename :> ");
                String foreName = in.next();
                System.out.print("\nEnter surname :>");
                String surName = in.next();
                aRegistry.addStudent(new Student(foreName,surName,studentID));
                studentID = studentID +1;
                System.out.print("\nEnter another (y/n");
                String another = in.next();
                if(another.equals("n"))
                    add = false;
            }
                
        }
    }
    RegistryApp
    Java Code:
    public class RegistryApp 
    {
        public static void main(String[] args)
        {
            Registry aRegistry = new Registry();
            
            RegistryInterface aRegistryInterface = new RegistryInterface(aRegistry);
            
            aRegistryInterface.doMenu(aRegistry);
            
        }
    }
    Last edited by wdh321; 04-19-2012 at 03:15 PM.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,691
    Rep Power
    8

    Default Re: Another Exception in thread "main" java.lang.NullPointerException problem

    It's a similar problem to your old problem- you're hiding class variables by local variables (in this case, method parameters). So when you make a change to the local variable, the class variable remains unchanged. Then when you try to use that class variable, it's the old value- in this case, null.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    wdh321 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    116
    Rep Power
    0

    Default Re: Another Exception in thread "main" java.lang.NullPointerException problem

    Ok, thanks again! Once again an easy fix, just changed the name of
    public RegistryInterface(Registry newRegistry)
    and all seems to be working now.

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,691
    Rep Power
    8

    Default Re: Another Exception in thread "main" java.lang.NullPointerException problem

    That would get rid of the compiler error, but I'm not sure your logic makes sense. Why bother creating a new instance of Registry and passing it into the constructor if you aren't going to use it at all?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    wdh321 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    116
    Rep Power
    0

    Default Re: Another Exception in thread "main" java.lang.NullPointerException problem

    The registryInterface requires reference to a registry object, thats why ive created one in the registryApp if thats what you are mentioning.

  6. #6
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,691
    Rep Power
    8

    Default Re: Another Exception in thread "main" java.lang.NullPointerException problem

    You create a Registry instance in your main thread. You pass that instance into your RegistryInterface (which is a class, not an interface). Then what do you do with that instance? Judging from what you've said, you simply ignore it and create a new instance.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    wdh321 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    116
    Rep Power
    0

    Default Re: Another Exception in thread "main" java.lang.NullPointerException problem

    I see what you mean now, no there is no need for it. I think the original idea was to use the registry instance from the main thread but the way i ended up making it it's completely redundant. I will take it out and change the RegistryInterface to suit. Also, i know its not an interface, its just the name ive given to the class :)

Similar Threads

  1. Replies: 5
    Last Post: 12-21-2011, 06:44 PM
  2. Replies: 5
    Last Post: 10-11-2011, 12:16 AM
  3. Replies: 6
    Last Post: 07-16-2009, 03:30 PM
  4. Replies: 1
    Last Post: 07-16-2009, 10:35 AM
  5. Replies: 1
    Last Post: 07-16-2007, 06:32 AM

Posting Permissions

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