java poker game nullpointer [duplicate] - java

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 6 years ago.
I'm getting a null pointer exception for this and I'm not sure why
public static boolean hasPair(Card[] cards) {
int k=0;
cards = new Card[5];
for (int atPos = 0; atPos<5; atPos++){
for (int atPos2 = atPos+1; atPos2<5; atPos2++){
if(cards[atPos].getValue() == cards[atPos2].getValue()){
k++;
}
if (atPos2 == (cards.length-1) && k!=1){
k=0;
}
else if (atPos2 == (cards.length-1) && k>=2){
return true;
}
}
}
return false;
}
My method is testing whether or not my hand of cards has two cards that hold the same value and the nul pointer says it's within this line
if(cards[atPos].getValue() == cards[atPos2].getValue()){
I also have this method...could i use it as a helper?
public Card[] deal(int numCards) {
Card[] newArray;
newArray = new Card[numCards];
for (int index=0; index<numCards; index++){
newArray[index] = cards.get(0);
cards.remove(0);
}
return newArray;
}

In second line you create a new array of objects Card. Every object in that array is null, so you need to fill the array first.

Related

Using public boolean add(Object new) { [duplicate]

This question already has answers here:
Method for adding Objects into a fixed collection(Array) in Java
(3 answers)
Closed 10 months ago.
In my program I have two private variable
private Object[] array;
private int place;
I have initialized these variables in the constructor
public Arrays() {
array = new Object[10];
place = 0;
}
I am trying to properly implement the following method, this is what I have so far
public boolean add(Object new) {
for(int j = place+1; j <= 0; j++){
array[j] = new;
}
place++;
return true;
}
I am having trouble with placing the object 'new' into the next unoccupied cell
Just assign to the next free slot. new is a reserved keyword.
public boolean add(Object newItem) {
vals[place++]=newItem;
return true;
}

Generic Array List [duplicate]

This question already has answers here:
What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?
(26 answers)
Closed 6 years ago.
I need to create an array list using generics. My add method seems to work sometimes, however my get method appears to have a good amount of problems and i don't receive a compile error. However when i try to get an object from the Array list using my get method it throws a java out of bounds exception. here i what i have so far, and i am using BlueJ. Also, the instructions were to set the initial "illusion" length to zero.
public class AL <X> {
private X[]data;
private int count;
public AL() {
count = 0;
data = (X[]) new Object[0];
}
public void add (X v) {
if (data.length != count) {
data[count] = v;
count++;
} else {
X [] newdata = (X[]) new Object[data.length * 2];
for (int i = 0; i < data.length; i++) {
newdata[i] = data [i];
}
count++;
data = newdata;
}
}
public X get(int index) {
if (index >= count || index < 0) {
throw new ICantEven();
} else {
return data[index];
}
}
}
Your add method doesn't work, since the initial backing array you are using has a 0 length, which remains 0 even when you try to double it (since 0*2==0).
You also forgot to actually add the new element when you resize the backing array. If you hadn't forgot that, you'd get the exception in add.
First of all, change the initial size of the array created by your constructor to be positive :
data = (X[]) new Object[10];
Then add
data[count] = v;
to the else clause of your add method (just before count++;).
Your add method can be further simplified :
public AL()
{
count = 0;
data = (X[]) new Object[10];
}
public void add (X v)
{
// resize backing array if necessary
if (data.length == count)
{
X [] newdata = (X[]) new Object[data.length * 2];
for (int i = 0; i < data.length;i++ )
{
newdata[i] = data [i];
}
data = newdata;
}
// add new element
data[count] = v;
count++;
}

Setting a value in java classes. Null Pointer Exception [duplicate]

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 5 years ago.
I am currently trying to make a java card game but am having trouble setting the card. I am taking in values such as 2H 3D 4S 5C 6H in the main function. I am trying to put these values into my Card class but when I try to set my rank I get a nullpointerexception error.
I am new to java programing and can not figure out why this is happening. Any suggestions? Am I not allowed to make an array of Cards?
public class Game {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int T = sc.nextInt();
String[] player1arr = new String[5];
String[] player2arr = new String[5];
Card[] player1 = new Card[5];
Card[] player2 = new Card[5];
for(int i = 0; i < 5; i++){
player1arr[i] = sc.next();
char first = player1arr[i].charAt(0);
int rank = Character.getNumericValue(first); //error
player1[i].setRank(rank);
}
for(int i = 0; i < 5; i++){
player2arr[i] = sc.next();
System.out.println(player2arr[i]);
}
}
}
class Card{
private int rank;
private char suit;
public int getRank(){
return rank;
}
public void setRank(int r){
rank = r;
}
}
When you create an array of objects, the array is initially filled with the default value of null.
Call
cards[i] = new Card();
to initialize all the objects within the array :)
You need to instantiate Cards in your main method.
for(int i = 0; i < 5; i++){
player1arr[i] = sc.next();
char first = player1arr[i].charAt(0);
int rank = Character.getNumericValue(first); //error
player1[i] = new Card();
player1[i].setRank(rank);
}

Filling an array using a method [duplicate]

This question already has answers here:
What's the simplest way to print a Java array?
(37 answers)
Closed 7 years ago.
I'm stuck with a revision question from a lab. The question is:
Write a static method to initialize an array of integers called numList. The size of the array should be passed as an int to the method and the array should be returned. Each odd index position should contain the value -1 and each even numbered position should contain the index value. Thus, such an array might contain {0,-1,2,-1,4,-1,6,-1}.
My code is currently :
public class initializeArray{
public static void main (String [] args) {
int [] numList = new int [6];
alterArray(numList);
}
public static void alterArray (int [] numList)
{
for( int i = 0; i<numList.length; i++)
{
if (i == 0)
{
numList[i] = i;
} else{
numList[i] = -1;
}
}
System.out.println( "The array is: " + numList);}
}
The return that I'm getting is :
"The array is: [I#1ef856c"
Thanks.
Updated to:
System.out.println(Arrays.toString(numList));
Error now occuring:
"Cannot find symbol - variable Arrays"
Try this:
System.out.println(Arrays.toString(numList));

Null Pointer Exception with a 2d array [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
objects classes and arrays - why is it returning ‘null’ ? [java]
The other questions with similar titles all have the answer that their data needs to be initialized which I have done but I'm still getting a null pointer exception. Could anyone tell me why?
public class grid{
private Node [][] board = new Node [9][9];
public boolean add(int x, int y, char label) {
boolean valid=true;
System.out.println("enter add");
if(label==' '){
System.out.println("enter if 1");
board[x][y].setValue('0');
}
else if(label<='9'&&label>'0'){
System.out.println("enter if 2");
board[x][y].setValue(label);
}
else{
valid=false;
}
if(valid)
System.out.println("valid");
return valid;
}
I'm getting the error on the setValue Lines (10 and 14)
public class Node{
public char value;
public char [] possibleValues = {1,2,3,4,5,6,7,8,9};
public boolean correct=false;
}
Edit: I figured it out, if anyone else has the same problem, this seems to fix it.
if(label==' '){
System.out.println("enter if 1");
board[x][y]= new Node(' ');
}
else if(label<='9'&&label>'0'){
System.out.println("enter if 2");
board[x][y]= new Node(label);
}
An array does not initialize the elements of the array. So, each board[x][y] will be initially null.

Categories

Resources