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

    Default A few exceptions being thrown

    I have a few issues, i have a class Registry which is a linked list of Students. I also have a class Registry tester to test the methods.
    Both classes have no errors however on line 13, net beans is telling me to "Assign return value to New Variable".
    Java Code:
    import java.util.LinkedList;
    public class Registry 
    {
        LinkedList<Student> StudentList;
        
        public Registry()
        {
          LinkedList<Student> StudentList = new LinkedList<Student>();  
        }
        
        public void addStudent(Student aStudent )
        {
            StudentList.add(aStudent);
        }
    The main problem is whilst trying to run the Registry Tester. When i run it i get this exception.
    Exception in thread "main" java.lang.NullPointerException
    at Registry.addStudent(Registry.java:13)
    at RegistryTester.main(RegistryTester.java:6)
    Java Result: 1

    I have searched this but cant find an answer, i assume its related to line 13 in the Registry class?
    Java Code:
    public class RegistryTester
    {
        public static void main(String[] args)
        {
            Registry aRegistry = new Registry();
            aRegistry.addStudent(new Student("steve","davies",1006));
            System.out.println("Testing Registry Class");
            System.out.println("**********************\n");
            
            System.out.println("Test 1: toString() method");
            System.out.println("*************************");
            
        }
        
    }

  2. #2
    jlczuk is offline Senior Member
    Join Date
    Apr 2012
    Location
    New York State of Confusion, USA
    Posts
    137
    Blog Entries
    1
    Rep Power
    0

    Default Re: A few exceptions being thrown

    Withdrawn, I was incorrect.

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

    Default Re: A few exceptions being thrown

    In your Registry constructor, you create a new variable named StudentList (variables should start with a lower-case letter, by the way). That variable hides the class variable with the same name, which remains unchanged. You then try to use the class variable, but since you never assigned anything to it, you get an NPE.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

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

    Default Re: A few exceptions being thrown

    I see what your saying, im a bit confused as to what to change to get it working correctly. Its my first time using linked lists so i wasnt to sure how to initiate it in public Registry

  5. #5
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,017
    Rep Power
    10

    Default Re: A few exceptions being thrown

    This doesn't really have anything to do with LinkedLists. For example:

    Java Code:
    public class Test{
       String testString;
    
       public Test(){
          String testString = "test String";
       }
    
       public void printString(){
          System.out.println(testString);
       }
    
    }
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

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

    Default Re: A few exceptions being thrown

    Sorted :) thanks for steering me in the right direction.

Similar Threads

  1. Mismatch error thrown?
    By Teclis in forum New To Java
    Replies: 16
    Last Post: 04-19-2011, 03:58 AM
  2. After catching the exception thrown
    By scoobyrox in forum New To Java
    Replies: 2
    Last Post: 09-05-2010, 03:29 PM
  3. How many no. of exceptions can be thrown????
    By Stephen Douglas in forum New To Java
    Replies: 8
    Last Post: 04-30-2010, 06:12 PM
  4. Replies: 0
    Last Post: 06-08-2009, 10:59 PM
  5. Which exception is thrown.....
    By money123 in forum New To Java
    Replies: 1
    Last Post: 07-30-2007, 04:41 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
  •