Results 1 to 9 of 9
  1. #1
    PRW56 is offline Member
    Join Date
    Mar 2012
    Posts
    75
    Rep Power
    0

    Unhappy Unusual null pointer error

    im relatively new to java and understand what a null pointer exception is, but cannot figure out what is causing it, error is caused by line of code after comment.

    here is code:

    Java Code:
    import java.util.Random;
    import java.util.Scanner;
    public class CardGame21
    {
        public static void main(String[] args)
        {
            Scanner scan = new Scanner(System.in);
            Random r = new Random();
            boolean game=true,a1,a2;
            char blah;
            int players=1;
            int[] card = new
            while(game==true)
            {
                System.out.println("Welcome to the game of blackjack!");
                a1=true;
                a2=true;
                while(a1==true)
                {
                    System.out.println("Select number of computer players players.");
                    players=scan.nextInt();
                    if(players<1)
                    {
                        System.out.print("!INVALID SELECTION!");
                        System.out.println("\n");
                    }
                    else
                    {
                        a1=false;
                    }
                }
                /*System.out.println("Would you like to go first(Y) or it to be random(N)?");
                blah=System.in.read(); System.in.read();
                */
                Player[] player = new Player[players];
                while(a2==true)
                {
                    System.out.println("Cards have been dealt!");
                    System.out.print("\n");
                    for(int x=0;x<players;++x)
                    {
    // next line is cause of error
                        player[x].setCard(0,(r.nextInt(13)+1));
                        player[x].setCard(1,(r.nextInt(13)+1));
                    }
                    System.out.println("You recieved a "+player[0].getCard(0)+" and a(n) "+player[0].getCard(1));
                    a2=false;
                    game=false;
                }
            }
        }
    }
    
    
    public class Player
    {
        private int[] card = new int[10];
        public String getCard(int a)
        {
            String b="No card Assigned";
            if(card[a]==1)
            {
                b = ("Ace");
            }
            if(card[a]==11)
            {
                b = ("Jack");
            }
            if(card[a]==12)
            {
                b = ("Queen");
            }
            if(card[a]==13)
            {
                b = ("King");
            }
            if(card[a]>1&&card[a]<11)
            {
                b = (card[a]+"");
    = ;        }
            return b;
        }
        public void setCard(int a,int b)
        {
            card[a]=b;
        }
    }
    any help would be greatly appreciated :)

    //p.s. sorry about lack of indentation, when i copied and posted it went away
    Last edited by sunde887; 03-21-2012 at 04:39 AM. Reason: Added code tags [code][/code]

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default Re: Unusual null pointer error

    Directly after
    Java Code:
    Player[] player = new Player[players];
    Add this
    Java Code:
    for(int i = 0; i < players; ++i){
      System.out.println(player[i]);
    }

  3. #3
    PRW56 is offline Member
    Join Date
    Mar 2012
    Posts
    75
    Rep Power
    0

    Default Re: Unusual null pointer error

    sorry for the delay, wouldnt that just print the address of the object or is it existing the thing were checking for...?
    also since the error was there wouldn't i be unable to enter/ test the code

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default Re: Unusual null pointer error

    I realized what your error was I just didn't want to give you the answer directly. Try adding that code and the problem should become quite clear.

  5. #5
    PRW56 is offline Member
    Join Date
    Mar 2012
    Posts
    75
    Rep Power
    0

    Default Re: Unusual null pointer error

    i know how that would be benificial to me but... i did something stupid, when i had this error i ended up using a multidimensional array for the players instead, it works fine, but i would still like to know what was the problem here, because i will have classes in the future where i will not be able to get around this, i.e. this code only exists on this site, when i try to copy it from here it all appears on one line, making it impossible to use, so pls just tell me the error... or how to copy it from this site...

  6. #6
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default Re: Unusual null pointer error

    This snippet:

    Java Code:
    public class TestArray{
    	public static void main(String[] args){
    		String[] x = new String[5];
    		for(int i = 0; i < x.length; ++i){
    			System.out.println(x[i]);
    		}
    	}
    }
    produces an output of:

    Java Code:
    null
    null
    null
    null
    null
    What do you think will happen if I try performing an operation on some element of this array? Your problem is the same as this one.

  7. #7
    PRW56 is offline Member
    Join Date
    Mar 2012
    Posts
    75
    Rep Power
    0

    Default Re: Unusual null pointer error

    kinda thought it would do that, but why? i had delcared.........OH SMACK! i was only declaring the array not the individual objects, thats why...

    p.s. this is what i said as i read this just now.

  8. #8
    PRW56 is offline Member
    Join Date
    Mar 2012
    Posts
    75
    Rep Power
    0

    Default Re: Unusual null pointer error

    p.s.s. ty very much

  9. #9
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default Re: Unusual null pointer error

    No problem. Ya, it was a small mistake, but it's always important to remember this.

Similar Threads

  1. Replies: 3
    Last Post: 12-04-2011, 03:29 PM
  2. null pointer error help!!
    By fakepics500 in forum New To Java
    Replies: 1
    Last Post: 07-16-2011, 03:57 PM
  3. Error = Null pointer with inner class
    By jonytek in forum New To Java
    Replies: 3
    Last Post: 05-11-2011, 05:10 AM
  4. Null Pointer Exception error
    By tfitz666 in forum New To Java
    Replies: 3
    Last Post: 03-28-2010, 08:20 PM
  5. Null pointer exception error
    By brownie_jedi in forum New To Java
    Replies: 3
    Last Post: 03-15-2008, 07:27 AM

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
  •