Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 12-03-2007, 07:42 PM
Member
 
Join Date: Dec 2007
Posts: 2
skinnybones is on a distinguished road
Exception Error need help fixing
adfdasfdasfdsa

Last edited by skinnybones : 12-10-2007 at 05:02 AM.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 12-03-2007, 08:09 PM
Senior Member
 
Join Date: Jul 2007
Posts: 1,191
hardwired is on a distinguished road
I put both classes in the same file, changed their names, compiled, ran and got this in the console:
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)
Count down to line 69 to find
Code:
if ( !guess.equals(strings[currentString]) && strings[currentString].contains(guess) )
Something in this line was null. It is a member variable which was hidden/shadowed by a local variable.
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" };
How to fix this?
Code:
private String[] strings; ... public ShowRx() { // Instantiate the member variable. strings = new String[] { "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.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 12-03-2007, 08:14 PM
Member
 
Join Date: Dec 2007
Posts: 2
skinnybones is on a distinguished road
Thanks for the help
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Null pointer exception error brownie_jedi New To Java 3 03-15-2008 07:27 AM
Error: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException romina New To Java 1 07-25-2007 11:55 PM
JSF error+exception Peter SWT / JFace 1 07-04-2007 07:29 AM
Difference between error and exception harinath chakrapani New To Java 1 06-19-2007 09:49 AM
tomcat exception-error Nick15 Eclipse 2 05-11-2007 02:32 AM


All times are GMT +3. The time now is 07:00 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org