java - NPE when trying to addElement() for a Vector -


here part of ownership class. gets no compile errors when attempt run other classes, , put in correct input, gives me npe @ addelement() line of addowner()...and have no idea why. ownerparser not problem, know sure, , neither input. tried in smaller project initialize vector list it's contents, , gave me npe well. need know how add new owner object ownerlist without getting nullpointerexception thanks.

    import java.util.*;     import java.io.*;      public class ownership implements serializable     {         private vector<owner> ownerlist;         private vector<pet> petlist;          public void ownership()         {             ownerlist = new vector<owner>();             petlist = new vector<pet>();         }       public boolean addowner(string b)        {        owner r = new owner();        r = ownerparser.parsestringtoowner(b);        ownerlist.addelement(r);                return true;        }    } 

your default "constructor" has void return value, , not constructor ordinary method, named constructor. consequently, "constructor" never called, , vector never initialized.

change public void ownership() {...} public ownership() {...} , work.


Comments