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

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

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++)
				result = i;
				result = -1;
		return result;

	public boolean addOwner (String stringToParse)
		 Owner owner = OwnerParser.parseStringToOwner(stringToParse); 
			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();

     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];

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


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

     //get a last name
     if (tokens2[0].length() > 0)
       temp1 = tokens2[0];

     //get a first name
     if (tokens2.length == 2 && tokens2[1].length() > 0)
       temp2 = tokens2[1];
     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?

This article has been dead for over six months. Start a new discussion instead.