Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Re: null pointer exception problem

  1. #1
    Junior Member
    Join Date
    Apr 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: null pointer exception problem

    hey im having the same problem with my code im a beginner in a programing class and have been trying to figure it out for days im trying to build a game of hangman.
    class TestGuessList
    {
      private char guessList;
      public static void main (String []args)
      {
        GetInput input = new GetInput();
        GuessList list = new GuessList();
        char guessList = ' ';
        int turns=5;
     
        for (int f=0;f<5;f++)
        {
          boolean done = false;
          System.out.println("You have " + turns + " turns remaining ");
          while (done == false)
          {
            input.setUserInput();
            guessList=input.getUserInput();
            while (list.inGuessList(guessList)==true)
            {
            System.out.println("This letter has already been guessed");
            }
            done = false;
          }
           turns--;      
          System.out.print("the letters that you have already guessed and added to you ignore list are: ");
          list.print();
          System.out.println("\n");
        }
      }
    }
     
    the method class is this
     
    public class GuessList// Class is responsible for insuring the letter guess is valid.
    {
      private char[] list; //Creates a private array character variable called list.
        public GuessList()// Creates a public method called GuessList.
        {
          char[] list = new char [0];//Creates an array called list.
        }
       public boolean inGuessList(char c)//Creates a public boolean method called inGuessList.
        {
         boolean duplicate=false;//Creates a boolean called duplicate and sets it to false.
          for(int x=0; x<list.length;x++)/*Creates a for loop with int x and sets it less then the length of 
           * list and not equal to duplicate.*/
         {
          if(list[x] == c )// Ifs statement for if list is equal to c.
          {
            duplicate = true;//Makes duplicate true.
            return duplicate;//Returns duplicate.
          }
          }
          return false;
        }
     
        public void print()//Print method.
        {
          System.out.println("The letters you have guessed are ");//Prints the letters guessed to the user.
          for(int i = 0;i<list.length;i++)//Creates a for loop for int i.
          {
            System.out.println(list[i] + " ");//Prints list[i].
          }
        }
        char[] getGuessList()//Creates an array method.
        {
          return list;//Returns the list.
        }
        public void addToGuessList(char guesses)//Creates a method called addToGuessList.
        {
          char[] list2 = new char [list.length +1];//Creates a second longer array.
          for(int t = 0; t<list.length; t++)//Creates a for loop less the the size of list.
          {
            list2[t]=list[t];//Makes list2[t] equal to list[t].
          }
          list2[list.length] = guesses;//Makes list2[list.length] equal to guesses.
          list=list2;//Makes list equal to list2.
        }    
        }
    please help..
    Last edited by copeg; April 2nd, 2011 at 08:55 PM.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: null pointer exception problem

    First, please don't hijack someone else's post - I've moved this to its own thread. Second, in the future please use the code tags - makes the source code readable. Lastly, you've posted a bit of code - I recommend posting the exception stack trace which helps pinpoint the problem.

Similar Threads

  1. null pointer exception problem
    By vandrop in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 2nd, 2011, 01:45 PM
  2. Null Pointer Exception Help !!
    By AlterEgo1234 in forum Member Introductions
    Replies: 1
    Last Post: March 27th, 2011, 10:07 AM
  3. Null Pointer Exception Help!!
    By puzzledstudent in forum What's Wrong With My Code?
    Replies: 5
    Last Post: November 11th, 2010, 06:46 PM
  4. [SOLVED] Fixed Null Pointer Exception but still have another problem
    By javapenguin in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 29th, 2010, 10:16 PM
  5. [SOLVED] Null Pointer Exception
    By musasabi in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 11th, 2010, 09:25 PM