Results 1 to 6 of 6
Thread: A few exceptions being thrown
- 04-18-2012, 06:41 PM #1
Senior Member
- Join Date
- Aug 2011
- Posts
- 116
- Rep Power
- 0
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".
The main problem is whilst trying to run the Registry Tester. When i run it i get this exception.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); }
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("*************************"); } }
- 04-18-2012, 06:43 PM #2
Senior Member
- Join Date
- Apr 2012
- Location
- New York State of Confusion, USA
- Posts
- 137
- Blog Entries
- 1
- Rep Power
- 0
Re: A few exceptions being thrown
Withdrawn, I was incorrect.
- 04-18-2012, 06:53 PM #3
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!
- 04-18-2012, 07:11 PM #4
Senior Member
- Join Date
- Aug 2011
- Posts
- 116
- Rep Power
- 0
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
- 04-18-2012, 07:18 PM #5
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!
- 04-18-2012, 07:33 PM #6
Senior Member
- Join Date
- Aug 2011
- Posts
- 116
- Rep Power
- 0
Similar Threads
-
Mismatch error thrown?
By Teclis in forum New To JavaReplies: 16Last Post: 04-19-2011, 02:58 AM -
After catching the exception thrown
By scoobyrox in forum New To JavaReplies: 2Last Post: 09-05-2010, 02:29 PM -
How many no. of exceptions can be thrown????
By Stephen Douglas in forum New To JavaReplies: 8Last Post: 04-30-2010, 05:12 PM -
Problem .... sockettimeoutException not thrown
By Shiv in forum NetworkingReplies: 0Last Post: 06-08-2009, 09:59 PM -
Which exception is thrown.....
By money123 in forum New To JavaReplies: 1Last Post: 07-30-2007, 03:41 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks