I need this program to take user entered information, check and see if the object exists and add it if it doesn't. I keep getting a nullpointer exception when trying to add an owner to an Arraylist. I've included the relevant part of the driver class, the OwnerParser class that takes the owner information and returns an object and the addOwner and ownerExists methods of the ownership class. I know its a lot to look at, but any help would be appreciated. Thank you for your time.

if (line.length() == 1)   //check if a user entered only one character
          {
           switch (input1)
            {
             case 'A':   //Add Owner
               System.out.print("Please enter the owner information to add:\n");
               inputInfo = stdin.readLine().trim();
/************************************************************************************
***  ADD your code to add an Owner object
***********************************************************************************/
             ownership1.addOwner(inputInfo);

               if (operation == true)
                System.out.print("owner added\n");
               else
                System.out.print("owner exists\n");
               break;




import java.io.Serializable;
import java.util.*;


public class Ownership implements Serializable
{
	private int result = 0;
	public ArrayList ownerList;
	private int i=0;
	public Ownership()
	{
		ArrayList ownerList = new ArrayList();
		ArrayList petList = new ArrayList();
		
	}
	
	public int ownerExists(String ownerID)
	{
		for(int i=0; i<ownerList.size(); i++)
			if(ownerID.equals(ownerList.get(i)))
				result = i;
			else 
				result = -1;
		
		return result;
	}
	

	public boolean addOwner (String stringToParse)
	{	
		
		 Owner owner = OwnerParser.parseStringToOwner(stringToParse); 
		
		if(ownerExists(owner.getOwnerID())==-1)
		{
			ownerList.add(owner);
			return true;
	            	
	            } 	
		else return false;
		
	}




//  Description: This is a utility class that takes a string containing
// an owner's information and parse it to create an owner object.

public class OwnerParser {

 public static Owner parseStringToOwner(String lineToParse)
  {
   String temp1 = new String();
   String temp2 = new String();
   String temp3 = new String();
   String temp4 = new String();
   String name = new String();

   Owner owner = new Owner();

   try
    {
     String[] tokens = lineToParse.split(":");


     //get a combination of first name and last name
     if (tokens[0].length() > 0)
      name = tokens[0];

     //get an owner ID
     if (tokens[1].length() > 0)
      {
       temp3 = tokens[1];
       owner.setOwnerID(temp3.trim());
      }

     //get a number of pets
     if (tokens.length == 3 && tokens[2].length() > 0)
      {
       temp4 = tokens[2];
       owner.setPetNum( Integer.parseInt(temp4.trim()));
      }

     name=name.trim();

     String[] tokens2 = name.split(",");

     //get a last name
     if (tokens2[0].length() > 0)
      {
       temp1 = tokens2[0];
       owner.setLastName(temp1.trim());
      }

     //get a first name
     if (tokens2.length == 2 && tokens2[1].length() > 0)
      {
       temp2 = tokens2[1];
       owner.setFirstName(temp2.trim());
      }
     return owner;
    }
   catch(NumberFormatException exception)
    {
     System.out.print("Invalid String\n");
     return owner;
    }
  }

}

can you show your entire code? it's possible you didn't initialize your ArrayList correctly or in the right scope, also there might be something wrong with the input.

can you also show the complete stacktrace you get when you get that nullpointer?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.