Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There are a couple of problems with your code:</p> <ol> <li><p><code>main</code> is a <code>static</code> method, meaning that it executes outside the context of any instance of <code>Library</code>. However, <code>s</code> is an instance field of <code>Library</code>. You should either make <code>s</code> a <code>static</code> field or create an instance of <code>Library</code> and reference the field through that instance:</p> <pre><code>public static void main(String[] args) { Library lib = new Library(); . . . lib.addStudent(g); // etc. } </code></pre></li> <li><p><code>addStudent</code> is not a member function of <code>ArrayList</code>; it is a member function of <code>Library</code>. Thus, you should not be coding <code>s.addStudent(f);</code>, etc.</p></li> <li><p>You don't initialize <code>s</code>, so the first time your code tries to add an element, you will get a <code>NullPointerException</code>. You should initialize it either inline:</p> <pre><code>private ArrayList&lt;Student&gt; s = new ArrayList&lt;Student&gt;(); </code></pre> <p>or write a constructor for <code>Library</code> and initialize the field there.</p></li> <li><p>Your latest change&mdash;to add <code>private ArrayList&lt;Student&gt; s;</code> to the <code>Student</code> class&mdash;is on the wrong track. You will end up with a separate list of students for each student you create; surely not what you want! The list of students belongs with the <code>Library</code> where it was.</p></li> <li>Your constructor for <code>Student</code> looks like it has the assignments backwards.</li> </ol>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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