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());
}

Edited 5 Years Ago by Majestics: n/a

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?

Edited 5 Years Ago by chiiqui: n/a

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???

Edited 5 Years Ago by Majestics: n/a

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.....

Edited 5 Years Ago by Majestics: n/a

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?

This article has been dead for over six months. Start a new discussion instead.