Results 1 to 9 of 9
Thread: Unusual null pointer error
- 03-21-2012, 02:41 AM #1
Member
- Join Date
- Mar 2012
- Posts
- 70
- Rep Power
- 0
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:
any help would be greatly appreciated :)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; } }
//p.s. sorry about lack of indentation, when i copied and posted it went awayLast edited by sunde887; 03-21-2012 at 03:39 AM. Reason: Added code tags [code][/code]
- 03-21-2012, 03:41 AM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Re: Unusual null pointer error
Directly after
Add thisJava Code:Player[] player = new Player[players];
Java Code:for(int i = 0; i < players; ++i){ System.out.println(player[i]); }
- 03-22-2012, 06:12 PM #3
Member
- Join Date
- Mar 2012
- Posts
- 70
- Rep Power
- 0
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
- 03-22-2012, 06:18 PM #4
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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.
- 03-22-2012, 06:23 PM #5
Member
- Join Date
- Mar 2012
- Posts
- 70
- Rep Power
- 0
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...
- 03-22-2012, 06:30 PM #6
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Re: Unusual null pointer error
This snippet:
produces an output of: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]); } } }
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.Java Code:null null null null null
- 03-22-2012, 06:36 PM #7
Member
- Join Date
- Mar 2012
- Posts
- 70
- Rep Power
- 0
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.
- 03-22-2012, 06:37 PM #8
Member
- Join Date
- Mar 2012
- Posts
- 70
- Rep Power
- 0
Re: Unusual null pointer error
p.s.s. ty very much
- 03-22-2012, 06:58 PM #9
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Re: Unusual null pointer error
No problem. Ya, it was a small mistake, but it's always important to remember this.
Similar Threads
-
Program to convert Speech to Text..pls getting error @ rec.allocate(NULL POINTER EXC)
By kaushik1234567890 in forum Advanced JavaReplies: 3Last Post: 12-04-2011, 02:29 PM -
null pointer error help!!
By fakepics500 in forum New To JavaReplies: 1Last Post: 07-16-2011, 02:57 PM -
Error = Null pointer with inner class
By jonytek in forum New To JavaReplies: 3Last Post: 05-11-2011, 04:10 AM -
Null Pointer Exception error
By tfitz666 in forum New To JavaReplies: 3Last Post: 03-28-2010, 07:20 PM -
Null pointer exception error
By brownie_jedi in forum New To JavaReplies: 3Last Post: 03-15-2008, 06:27 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks