Click here to Skip to main content
15,881,559 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This simple programs shows NullPointerException in line { System.out.println("Id = " + staffs[j].getId());}, how can i solve this issue?

Thanks

import java.util.Scanner;
import java.util.Arrays;
public class Main {

	public static void main(String[] args) {
		int ID;
		String Name;
		String LastName;
		String FatherName;
		int Salary;
		String Position;

		String input;

		boolean repeat = true;

		Scanner scan = new Scanner(System.in);
		
		Staff[] staffs = new Staff[100];
		int i = 0;
		while (repeat) {

			String userInput;
			System.out.println("Please Enter Staff Information!");

			System.out.println("ID: ");
			ID = scan.nextInt();

			System.out.println("Name: ");
			Name = scan.next();

			System.out.println("Last Name: ");
			LastName = scan.next();

			System.out.println("Father Name: ");
			FatherName = scan.next();

			System.out.println("Salary : ");
			Salary = scan.nextInt();

			System.out.println("Position: ");
			Position = scan.next();

			scan.nextLine();

			System.out
					.println("Type 'yes' or 'no' if you want to continure or not ");
			input = scan.nextLine();

			if ("yes".equals(input))
				repeat = true;
			else
				repeat = false;

			staffs[i++] = new Staff(ID, Name, LastName, FatherName, Salary,Position);

		}
		System.out.println("Thank you!");
		
		for (int j = 0; j < staffs.length - 1; j++) {
			System.out.println("Id = " + staffs[j].getId());
			System.out.println("Name = " + staffs[j].getName());
			System.out.println("LastName = " + staffs[j].getLastName());
			System.out.println("FatherName = " + staffs[j].getFatherName());
			System.out.println("Salary = " + staffs[j].getSalary());
			System.out.println("Position = " + staffs[j].getPosition());
		
		}
		}
	}

public class Staff {
	
	public int ID;
	public  String Name;
	public String LastName;
	public	 String FatherName;
	public  int Salary;
	public  String Position;
	
	public Staff(int ID, String Name, String LastName, String FatherName, int Salary, String Position){
		
		this.ID = ID;
		this.Name = Name;
		this.LastName = LastName;
		this.FatherName = FatherName;
		this.Salary = Salary;
		this.Position = Position;
		}
			public  int getId(){
				return ID;
			}
			public String getName(){
				 return Name;
				 }
			 public String getLastName(){
				 return LastName;
			 }
			 public String	getFatherName(){
				 return FatherName;
			 }
			 public int getSalary(){
				 return Salary;
				 }
			 public String getPosition(){
				 return Position;
				 }
	}
Posted
Updated 21-Jun-15 20:53pm
v2
Comments
Kornfeld Eliyahu Peter 22-Jun-15 2:56am    
At some point staffs[j] is null - run a debugger to find out why!

1 solution

Though I have not tested the code but it should give you the hint to move on.

Java
import java.util.Scanner;
import java.util.Arrays;
import java.util.ArrayList;

public class Main {
 
	public static void main(String[] args) {
		int ID;
		String Name;
		String LastName;
		String FatherName;
		int Salary;
		String Position;
 
		String input;
 
		boolean repeat = true;
 
		Scanner scan = new Scanner(System.in);
		
		ArrayList ArrayList<staff> staffList= new ArrayList<staff>();
		
		int i = 0;
		while (repeat) {
 
			String userInput;
			System.out.println("Please Enter Staff Information!");
 
			System.out.println("ID: ");
			ID = scan.nextInt();
 
			System.out.println("Name: ");
			Name = scan.next();
 
			System.out.println("Last Name: ");
			LastName = scan.next();
 
			System.out.println("Father Name: ");
			FatherName = scan.next();
 
			System.out.println("Salary : ");
			Salary = scan.nextInt();
 
			System.out.println("Position: ");
			Position = scan.next();
 
			scan.nextLine();
 
			System.out
					.println("Type 'yes' or 'no' if you want to continure or not ");
			input = scan.nextLine();
 
			if ("yes".equals(input))
				repeat = true;
			else
				repeat = false;
 
			Staff tmpStaff = new Staff(ID, Name, LastName, FatherName, Salary,Position);
			staffList.add(tmpStaff);
 
		}
		System.out.println("Thank you!");
		
		for (Staff tmpStaff : staffList) {
			System.out.println("Id = " + tmpStaff.getId());
			System.out.println("Name = " + tmpStaff.getName());
			System.out.println("LastName = " + tmpStaff.getLastName());
			System.out.println("FatherName = " + tmpStaff.getFatherName());
			System.out.println("Salary = " + tmpStaff.getSalary());
			System.out.println("Position = " + tmpStaff.getPosition());
		
		}
		}
	}</staff></staff>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900