Results 1 to 3 of 3
  1. #1
    skinnybones is offline Member
    Join Date
    Dec 2007
    Posts
    2
    Rep Power
    0

    Default Exception Error need help fixing

    adfdasfdasfdsa
    Last edited by skinnybones; 12-10-2007 at 04:02 AM.

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    I put both classes in the same file, changed their names, compiled, ran and got this in the console:
    Java 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
    Java 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.
    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"
            };
    How to fix this?
    Java 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.

  3. #3
    skinnybones is offline Member
    Join Date
    Dec 2007
    Posts
    2
    Rep Power
    0

Similar Threads

  1. Null pointer exception error
    By brownie_jedi in forum New To Java
    Replies: 3
    Last Post: 03-15-2008, 06:27 AM
  2. Replies: 1
    Last Post: 07-25-2007, 10:55 PM
  3. JSF error+exception
    By Peter in forum SWT / JFace
    Replies: 1
    Last Post: 07-04-2007, 06:29 AM
  4. Difference between error and exception
    By harinath chakrapani in forum New To Java
    Replies: 1
    Last Post: 06-19-2007, 08:49 AM
  5. tomcat exception-error
    By Nick15 in forum Eclipse
    Replies: 2
    Last Post: 05-11-2007, 01: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
  •