I am working on may code and I received a null pointer error , how do I fix this? all I wanted to do was remove the selected array here's my code

import java.util.Scanner;

public class Game {
	
	public static void main(String args[]){
		
		Scanner input = new Scanner( System.in );
		
		Briefcase briefcase[] = new Briefcase[11];
		
		double amounts[] = {0,100,200,300,400,500,600,700,800,900,1000,25000};
		
		for(int i = 0; i<11 ; i++){
			briefcase[i] = new Briefcase();
			double value = amounts[i];
			briefcase[i].setAmount(value);
			briefcase[i].setFace(i);
		}
		
		
		
		
		
		for(int a = 1 ;a<11; a++){
			System.out.print("\t "+briefcase[a].getFace()+" ");
			if(a%5==0){
				System.out.println();
			}
		}
		
		System.out.print("\tPlease Chose your Case! : ");
		
		int nUser = input.nextInt();
		int myCase = briefcase[nUser].getFace();
		briefcase[nUser].getAmount();
		briefcase[nUser] = null;
		
		System.out.println("\tYou've chosen BriefCase number! "+myCase);
		
		for(int z = 0 ; z<=11;z++)
		{
			
			if(briefcase[z] == null){
				System.out.println("Blarg");
			}
			System.out.println(" "+briefcase[z].getFace());
			
		}
			
		
		
	}

}

and here is the other class

public class Briefcase {

	double amount;
	int face;
	
	public void setAmount(double amount){
		this.amount = amount;
	}
	
	public double getAmount(){
		
		return this.amount;
	}
	
	public void setFace(int face){
		this.face =  face;
	}
	
	public int getFace(){
		return face;
	}
	
	
}

Instead of double amounts[] = {0,100,200,300,400,500,600,700,800,900,1000,25000};

Try this,

double amounts[] = new double[30];
amounts[] = {0,100,200,300,400,500,600,700,800,900,1000,25000};

Good Luck.

check at line 24 , 25

for(int a = 1 ;a<11; a++){
System.out.print("\t "+briefcase[a].getFace()+" ");

why are u leaving location 0
Also u cant use 11 th position , it should be less then 11, even u want to show something , dont code like this.

for(int z = 0 ; z<=11;z++)
{
 
if(briefcase[z] == null){
System.out.println("Blarg");
}
System.out.println(" "+briefcase[z].getFace());
}

The thing is, I am setting an object to null so that it can be removed from the array that's why I am setting it to null

I've fixed it my error is this "Exception in thread "main" java.lang.NullPointerException
at Testing.main(Testing.java:46)", I am guessing that it's because of that object that referenced to null?
how do I fix this?

Here's my code with the edits you guys Suggested

import java.util.Scanner;
 
public class Testing {
 
	public static void main(String args[]){
 
		Scanner input = new Scanner( System.in );
 
		Briefcase briefcase[] = new Briefcase[11];
 
		double amounts[] = {0,100,200,300,400,500,600,700,800,900,1000,25000};
 
		for(int i = 0; i<11 ; i++){
			briefcase[i] = new Briefcase();
			double value = amounts[i];
			briefcase[i].setAmount(value);
			briefcase[i].setFace(i);
		}
 
 
 
 
 
		for(int a = 0 ;a<11; a++){
			System.out.print("\t "+briefcase[a].getFace()+" ");
			if(a%5==0){
				System.out.println();
			}
		}
 
		System.out.print("\tPlease Chose your Case! : ");
 
		int nUser = input.nextInt();
		int myCase = briefcase[nUser].getFace();
		briefcase[nUser].getAmount();
		briefcase[nUser] = null;
 
		System.out.println("\tYou've chosen BriefCase number! "+myCase);
 
		for(int z = 0 ; z<11;z++)
		{
 
			if(briefcase[z] == null){
				System.out.println("Blarg");
			}
			System.out.println(" "+briefcase[z].getFace());
 
		}
 
 
 
	}
 
}
briefcase[nUser] = null; /// Why this... Remove Null

Also as i stated make it less then 11 at line 40 else it will give index out of bound exception

Remove line 36. , also u are getting values at line 35 but you are not storing it???

briefcase[nUser] = null; /// Why this... Remove Null

Also as i stated make it less then 11 at line 40 else it will give index out of bound exception

Why null? because I want to remove that object, from my array collection

You Just need a else portion at line 46....

if(briefcase[z] == null){
System.out.println("Blarg");
}
else
System.out.println(" "+briefcase[z].getFace());
}

You are printing data even its class is null, that was creating error.

Also it seems a very bad programming practice.....

You Just need a else portion at line 46....

if(briefcase[z] == null){
System.out.println("Blarg");
}
else
System.out.println(" "+briefcase[z].getFace());
}

You are printing data even its class is null, that was creating error.

Just did that and it kept sayng "blarg" for a lot of times any help?

check my code , it only print blarg when it find null, and that only the selected value.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.