Note that there are some explanatory texts on larger screens.

plurals
  1. POI get a nullPointerException when I'm trying to add some values to a referenced class from another class
    text
    copied!<p>I am c# programmer and I am used to c#'s syntaxes for encapsulation and other stuff.But now,due to some reasons,I should write something with java and I am practicing java for a day now!I am going to create a dummy project for my self in order to make myself more comfortable with java's oop concepts</p> <p>What I am trying to do is that I want a class named 'Employee' which has three properties(fields):<code>firstName</code>,<code>lastName</code>,and <code>id</code>.And then I want to create another class named <code>EmployeeArray</code> which will construct an array of <code>Employee</code>s within itself and can perform some operations on it(for some reasons I want this project this way!!) Now I want to add some values to the <code>Employee</code>s within the <code>EmployeeArray</code> class.here's my work so far:</p> <pre><code>//this is the class for Employee public class Employee { private String firstName; private String lastName; private int id; public void SetValues(String firstName, String lastName, int id) { this.firstName = firstName; this.lastName = lastName; this.id = id; } //this is the EmployeeArray class public class EmployeeArray { private int numOfItems; private Employee[] employee; public EmployeeArray(int maxNumOfEmployees) { employee = new Employee[maxNumOfEmployees]; numOfItems = 0; } public void InsertNewEmployee(String firstName, String lastName, int id){ try { employee[numOfItems].SetValues(firstName, lastName, id); numOfItems++; } catch (Exception e) { System.out.println(e.toString()); } } //and this is the app's main method Scanner input = new Scanner(System.in); EmployeeArray employeeArray; employeeArray = new EmployeeArray(input.nextInt()); String firstName; String lastName; int id; firstName = input.nextLine(); lastName = input.nextLine(); id = input.nextInt(); employeeArray.InsertNewEmployee(firstName, lastName, id); </code></pre> <p>the problem is I get a nullPointerException when the app wants to set the values,and it is happening in the <code>employeeArray</code> reference.I don't know what I am missing.Any suggestions?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload