Page 1 of 2 12 LastLast
Results 1 to 20 of 22
  1. #1
    harshit shah is offline Member
    Join Date
    Jan 2013
    Location
    Kolkata,India
    Posts
    86
    Rep Power
    0

    Angry null pointer exception

    This error
    Java Code:
    java.lang.NullPointerException
    	at vsbot.check_present(vsbot.java:107)
    	at vsbot.input_letter(vsbot.java:70)
    	at vsbot.main(vsbot.java:22)
    is arriving from my code which I've given below..
    Description of my program-- This a hangman game in progress the display() method is still in progress.So leave that as it is.Now if you guys can help me :)
    Java Code:
    import java.io.*;
    public class vsbot
    {
        BufferedReader br =new BufferedReader(new InputStreamReader(System.in));
        final static String words_bot[] ={"cat","heat","death","sdad","sdad","sdad",};
        char input[]= new char[6];
        char correct_input[];
        static int tries=0;
        static int level =0;
        static int l;// length of word
        int i,c,c1;
        String concat_check = "";
        public  void main() throws IOException
        {
            System.out.println("You are fighting against the computer !");
            System.out.println("Let's BEGIN !!");
            System.out.println();
            System.out.println("This is level " +(level+1));
            for (;level < 6;)
            {
                l= words_bot[level].length();
                input_letter();
                if (level==0)
                {
                    correct_input = new char[] {' ', ' ', ' ' };
                }
                else if (level == 1)
                {
                    correct_input = new char[] {' ', ' ', ' ',' ' };
                }
                else if (level == 2)
                {
                    correct_input = new char[] {' ', ' ', ' ' };
                }
                else if (level == 3)
                {
                    correct_input = new char[] {' ', ' ', ' ' };
                }
                else if (level == 4)
                {
                    correct_input = new char[] {' ', ' ', ' ' };
                }
                else if (level == 5)
                {
                    correct_input = new char[] {' ', ' ', ' ' };
                }
                else if (level == 6)
                {
                    correct_input = new char[] {' ', ' ', ' ' };
                }
    
            }
        }
    
        public  void display()
        {
            System.out.println("-");
        }
    
        public  void input_letter() throws IOException
        {
            for (;tries < 6;)
            {
                System.out.print("Guess ");
                input[tries]=(char)br.read();
                while (input[tries]=='\n')
                    input[tries] = ((char)br.read());
    
                check_repitition();
                check_present();
            }
    
        }
    
        public  void check_repitition() throws IOException
        {
            for (i=0;i<6;i++)
            {
                if (i==tries)
                    continue;
                else
                {
                    if (input[tries]==input[i])
                    {
                        System.out.println("Already entered that letter.Please enter a new one");
                        input_letter();
                    }
                    else 
                        continue;
                }
            }
        }
    
        public  void check_present()
        {
            c= words_bot[level].indexOf(input[tries]);
            for (;c<=l;c++)
            {
    
                if (c== -1)
                {
                    tries++;
                    break;
                }
                else
                {
                    correct_input[c] = input[tries];
                    c1 = c++;
                    c=words_bot[level].indexOf(input[tries],c1);
                    if (c== -1)
                        continue;
                    else
                        correct_input[c] = input[tries];
                }
            }
            for (i=0;i<correct_input.length;i++)
            {
                concat_check = concat_check + correct_input[i];
            }
            if (concat_check.equalsIgnoreCase( words_bot[level] ))
            {
                System.out.println("level passed");
                level++;
            }
        }
    
    }

  2. #2
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    356
    Rep Power
    5

    Default Re: null pointer exception

    Put a debug statement at the end of your main() method to see if correct_input is still null.

  3. #3
    harshit shah is offline Member
    Join Date
    Jan 2013
    Location
    Kolkata,India
    Posts
    86
    Rep Power
    0

    Default Re: null pointer exception

    please elaborate or link me how to it please

  4. #4
    noobplus's Avatar
    noobplus is offline Senior Member
    Join Date
    Feb 2012
    Location
    chennai,south of India(south east of asia)
    Posts
    232
    Rep Power
    3

    Default Re: null pointer exception

    Hi,
    line no.7:
    Java Code:
    char correct_input[];
    arrays should be initialised before allocating in Java
    example:
    Java Code:
    char correct_input[]=new char[6];
    Last edited by noobplus; 07-18-2013 at 08:22 PM. Reason: *allocating, i wrote "reference"

  5. #5
    superhaNds is offline Senior Member
    Join Date
    Apr 2013
    Location
    Sweden
    Posts
    264
    Rep Power
    2

    Default Re: null pointer exception

    At the end of main() check if correct_input is null, that would mean it was not initialized.

    add a print for example

    Edit: you call input_letter() and inside check_present() that uses correct_input while it is not initialized until afterwards.
    so you reach this: correct_input[c] = input[tries]; and correct_input is not initialized.

    Java Code:
    for (;level < 6;)
    how does this work?
    Last edited by superhaNds; 07-18-2013 at 08:50 PM.

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,232
    Rep Power
    19

    Default Re: null pointer exception

    Quote Originally Posted by superhaNds View Post
    Java Code:
    for (;level < 6;)
    how does this work?
    An empty statement is allowed there.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  7. #7
    harshit shah is offline Member
    Join Date
    Jan 2013
    Location
    Kolkata,India
    Posts
    86
    Rep Power
    0

    Default Re: null pointer exception

    Quote Originally Posted by noobplus View Post
    Hi,
    line no.7:
    Java Code:
    char correct_input[];
    arrays should be initialised before allocating in Java
    example:
    Java Code:
    char correct_input[]=new char[6];
    As you say I have allocated the correct_input in my main() method where the level decides the allocation/ initialization of correct_input.

  8. #8
    harshit shah is offline Member
    Join Date
    Jan 2013
    Location
    Kolkata,India
    Posts
    86
    Rep Power
    0

    Default Re: null pointer exception

    A little bump ^^

    Java Code:
    import java.io.*;
    public class vsbot
    {
        BufferedReader br =new BufferedReader(new InputStreamReader(System.in));
        final static String words_bot[] ={"cat","heat","death","sdad","sdad","sdad",};
        char input[]= new char[6];
        char correct_input[];
        static int tries=0;
        static int level =0;
        static int l;// length of word
        int i,c,c1;
        String concat_check = "";
        public  void main() throws IOException
        {
            System.out.println("You are fighting against the computer !");
            System.out.println("Let's BEGIN !!");
            System.out.println();
            System.out.println("This is level " +(level+1));
            for (;level < 6;)
            {
                l= words_bot[level].length();
                if (level==0)
                {
                    correct_input = new char[] {' ', ' ', ' ' };
                }
                else if (level == 1)
                {
                    correct_input = new char[] {' ', ' ', ' ',' ' };
                }
                else if (level == 2)
                {
                    correct_input = new char[] {' ', ' ', ' ' };
                }
                else if (level == 3)
                {
                    correct_input = new char[] {' ', ' ', ' ' };
                }
                else if (level == 4)
                {
                    correct_input = new char[] {' ', ' ', ' ' };
                }
                else if (level == 5)
                {
                    correct_input = new char[] {' ', ' ', ' ' };
                }
                else if (level == 6)
                {
                    correct_input = new char[] {' ', ' ', ' ' };
                }
                if (correct_input == null)
                    System.out.println("Correct input is null " );
                input_letter();
            }
        }
    
        public  void display()
        {
            System.out.println("-");
        }
    
        public  void input_letter() throws IOException
        {
            if (correct_input == null)
                    System.out.println("Correct input is null " );
            for (;tries < 6;)
            {
                System.out.print("Guess ");
                input[tries]=(char)br.read();
                while (input[tries]=='\n')
                    input[tries] = ((char)br.read());
    
                check_repitition();
                check_present();
            }
    
        }
    
        public  void check_repitition() throws IOException
        {
            if (correct_input == null)
                    System.out.println("Correct input is null " );
            for (i=0;i<6;i++)
            {
                if (i==tries)
                    continue;
                else
                {
                    if (input[tries]==input[i])
                    {
                        System.out.println("Already entered that letter.Please enter a new one");
                        input_letter();
                    }
                    else 
                        continue;
                }
            }
        }
    
        public  void check_present()
        {
            if (correct_input == null)
                    System.out.println("Correct input is null " );
            c= words_bot[level].indexOf(input[tries]);
            for (;c<=l;c++)
            {
    
                if (c== -1)
                {
                    tries++;
                    break;
                }
                else
                {
                    correct_input[c] = input[tries];
                    c1 = c++;
                    c=words_bot[level].indexOf(input[tries],c1);
                    if (c== -1)
                        continue;
                    else
                        correct_input[c] = input[tries];
                }
            }
            if (correct_input == null)
                    System.out.println("Correct input is null " );
            for (i=0;i<correct_input.length;i++)
            {
                concat_check = concat_check + correct_input[i];
            }
            if (concat_check.equalsIgnoreCase( words_bot[level] ))
            {
                System.out.println("level passed");
                level++;
            }
        }
    
    }
    As stated earlier I am unable to resolve where is the null pointer problem arising.Please help,advise and criticize.

  9. #9
    harshit shah is offline Member
    Join Date
    Jan 2013
    Location
    Kolkata,India
    Posts
    86
    Rep Power
    0

    Default Re: null pointer exception

    Bump ^^^^^

  10. #10
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,001
    Rep Power
    20

    Default Re: null pointer exception

    Since you've changed your code it might be an idea to post the full stack trace again, and highlight the line that is causing the exception.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  11. #11
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,232
    Rep Power
    19

    Default Re: null pointer exception

    Quote Originally Posted by harshit shah View Post
    Bump ^^^^^
    Don't show impatience. This was posted less than an hour after the precious post.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  12. #12
    harshit shah is offline Member
    Join Date
    Jan 2013
    Location
    Kolkata,India
    Posts
    86
    Rep Power
    0

    Default Re: null pointer exception

    Quote Originally Posted by Tolls View Post
    Since you've changed your code it might be an idea to post the full stack trace again, and highlight the line that is causing the exception.
    i would appreciate it if you could please teach me how to post a stack trace, as stacks is an advanced chapter, and not yet taught at my level.

  13. #13
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,976
    Rep Power
    5

    Default Re: null pointer exception

    Your first post, that very first thing you posted (the error); that is an exception stacktrace. Nothing to do with the Stack class.

  14. #14
    harshit shah is offline Member
    Join Date
    Jan 2013
    Location
    Kolkata,India
    Posts
    86
    Rep Power
    0

    Default Re: null pointer exception

    Quote Originally Posted by darrylburke View Post
    don't show impatience. This was posted less than an hour after the precious post.

    Db
    sorry

  15. #15
    harshit shah is offline Member
    Join Date
    Jan 2013
    Location
    Kolkata,India
    Posts
    86
    Rep Power
    0

    Default Re: null pointer exception

    Quote Originally Posted by gimbal2 View Post
    Your first post, that very first thing you posted (the error); that is an exception stacktrace. Nothing to do with the Stack class.
    ?????
    Please elaborate i am java noob.

  16. #16
    harshit shah is offline Member
    Join Date
    Jan 2013
    Location
    Kolkata,India
    Posts
    86
    Rep Power
    0

    Default Re: null pointer exception

    bump

  17. #17
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,976
    Rep Power
    5

    Default Re: null pointer exception

    Even "noobs" are allowed to think about it.

    Java Code:
    java.lang.NullPointerException
        at vsbot.check_present(vsbot.java:107)
        at vsbot.input_letter(vsbot.java:70)
        at vsbot.main(vsbot.java:22)
    This is an exception stacktrace.

  18. #18
    harshit shah is offline Member
    Join Date
    Jan 2013
    Location
    Kolkata,India
    Posts
    86
    Rep Power
    0

    Default Re: null pointer exception

    Now what to do if I /You figured out where the null pointer exception arises.

  19. #19
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,976
    Rep Power
    5

    Default Re: null pointer exception

    You get that exception because a variable contains a null value where it should be pointing to a valid object. Ex:

    Java Code:
    private Person somePerson = null;
    
    System.out.println("PERSON: " + somePerson.getName()); // NPE, somePerson is null
    And to fix it you have to make sure that the variable in question is actually pointing to a proper object.

    Java Code:
    private Person somePerson = new Person("harshit");
    System.out.println("PERSON: " + somePerson.getName()); // no more NPE since somePerson now points to an object
    In short: you learn the Java language, you understand the Java language and you write proper Java code based on your understanding of the Java language.

  20. #20
    harshit shah is offline Member
    Join Date
    Jan 2013
    Location
    Kolkata,India
    Posts
    86
    Rep Power
    0

    Default Re: null pointer exception

    I exactly know what a null pointer exception is gimbal but ,my problem is that even after initializing the correct_input the code still shows the exception.hence,I am unable to rectify the error.

Page 1 of 2 12 LastLast

Similar Threads

  1. Null pointer exception a
    By TaxpayersMoney in forum New To Java
    Replies: 5
    Last Post: 08-16-2011, 12:37 AM
  2. Null pointer exception
    By jessie in forum New To Java
    Replies: 5
    Last Post: 02-08-2011, 02:58 PM
  3. Help with Null Pointer Exception
    By Beginner in forum New To Java
    Replies: 2
    Last Post: 04-17-2010, 04:41 PM
  4. Null pointer exception
    By talha06 in forum JDBC
    Replies: 5
    Last Post: 07-14-2009, 01:12 AM
  5. Null Pointer Exception
    By demiser55 in forum New To Java
    Replies: 1
    Last Post: 09-22-2008, 06:33 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •