Results 1 to 3 of 3
Thread: Exception Error need help fixing
- 12-03-2007, 06:42 PM #1
Member
- Join Date
- Dec 2007
- Posts
- 2
- Rep Power
- 0
- 12-03-2007, 07:09 PM #2
I put both classes in the same file, changed their names, compiled, ran and got this in the console:
Count down to line 69 to findJava Code:C:\jexp>javac playrx.java C:\jexp>java PlayRx Exception in thread "main" java.lang.NullPointerException at ShowRx.guess(playrx.java:69) at PlayRx.main(playrx.java:20)
Something in this line was null. It is a member variable which was hidden/shadowed by a local variable.Java Code:if ( !guess.equals(strings[currentString]) && strings[currentString].contains(guess) )
How to fix this?Java Code:class ShowRx { // Member variable declaration. // Member variables have class scope. private String[] strings; ... public ShowRx() { // Local variable of same name is declared and instantiated. // This hides/shadows the member variable with same name and // its (the member variable) value remains null. The scope of // this local variable is within the curley braces of this // constructor, ie, no one outside the constructor can see // this local variable. String[] strings = { "cat", "dog", "mouse", "bear", "moose", "bird", "frog", "fish", "slug", "emu" };
Now everyone in ShowRx can see "strings" and it has been instantiated and its value is therefore non–null.Java Code:private String[] strings; ... public ShowRx() { // Instantiate the member variable. strings = new String[] { "cat", "dog", "mouse", "bear", "moose", "bird", "frog", "fish", "slug", "emu" };
- 12-03-2007, 07:14 PM #3
Member
- Join Date
- Dec 2007
- Posts
- 2
- Rep Power
- 0
Similar Threads
-
Null pointer exception error
By brownie_jedi in forum New To JavaReplies: 3Last Post: 03-15-2008, 06:27 AM -
Error: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException
By romina in forum New To JavaReplies: 1Last Post: 07-25-2007, 10:55 PM -
JSF error+exception
By Peter in forum SWT / JFaceReplies: 1Last Post: 07-04-2007, 06:29 AM -
Difference between error and exception
By harinath chakrapani in forum New To JavaReplies: 1Last Post: 06-19-2007, 08:49 AM -
tomcat exception-error
By Nick15 in forum EclipseReplies: 2Last Post: 05-11-2007, 01:32 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks