Hello guys, i seem to have a problem. For some reason I can not pass an array of object to a class. Passing a single object works just fine but when i try to pass in an array i get a Null Pointer Exception.
Here is the scenario.
I have a person class that stores the first middle and last name.
i have a book class that stores the following
Author Name - Person object(s)
Book title
Book pages
Book year
Book isbn
I have 3 constructors set up.
1 -default constructor with default values
2 -parametrized constructor that accepts a single person object
3 -parametrized constructor that accepts a array of person objects
Here is my Book Class
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Book {
//begin Book Class
//private Person authors []; //store author objects as an array
private Person author; // store author object
private String title; // store book title
private int pages; // store book pages
private String isbn; // store book isbn
private int year; // store book year
public Book()
{// Book default constructor
//authors[0] = new Person();
setTitle("Default Book Title"); // Set default book title
setIsbn("0000000000"); // set default book isbn
setPages(1); // set default book pages
setYear(2009); // set default book year
}
public Book(Person userAuthor, String userTitle, String userIsbn, int userPages, int userYear)
{
this(); // call default constructor if test return false.
setAuthor(userAuthor); //set authers name to user author
userTitle.trim(); //trim the book title
if(userTitle.equals("")==false)
{// check to see if the title is empty
setTitle(userTitle);//set title to userTitle
}
if(isbnValidator(userIsbn)==true)
{// check to see if isbn is 10 digits long
String sequence1 = userIsbn.substring(0, 3);// extract first 3 numbers from isbn number
String sequence2 = userIsbn.substring(3, 10);// extract final 7 digits from isbn number
String newIsbn = sequence1+"-"+sequence2; // compile new string consisting of 3digits - 7digits
setIsbn(newIsbn); // assign newIsbn to isbn
}
if(userPages>1)
{// check to see if user pages is greater than 1
setPages(userPages);// set pages to userPages
}
if((userYear>1900)&&(userYear<=2010))
{// check to see if year is greater than 1900 and less than 2010
setYear(userYear); // set year to userYear
}
}
public Book(Person userAuthors[], String userTitle, String userIsbn, int userPages, int userYear)
{
this();
setAuthors(userAuthors);
userTitle.trim();
if(userTitle.equals("")==false)
{
setTitle(userTitle);
}
if(isbnValidator(userIsbn)==true)
{
String sequence1 = userIsbn.substring(0, 3);
String sequence2 = userIsbn.substring(3, 10);
String newIsbn = sequence1+"-"+sequence2;
setIsbn(newIsbn);
}
if(userPages>1)
{
setPages(userPages);
}
if((userYear>1900)&&(userYear<=2010))
{
setYear(userYear);
}
}
//mutators
public void setAuthor(Person userAuthor)
{
//authors[0] = userAuthor;
author = userAuthor;
}
public void setAuthors(Person userAuthors[])
{
for(int i = 0; i > userAuthors.length; i++)
{
// authors* = userAuthors*;
}
}
public void setTitle(String userTitle) {
title = userTitle;
}
public void setIsbn(String userIsbn) {
isbn = userIsbn;
}
public void setPages(int userPages) {
pages = userPages;
}
public void setYear(int year) {
this.year = year;
}
//acessors
/*
public Person [] getAuthors() {
return authors;
}
*/
public String getTitle() {
return title;
}
public String getIsbn() {
return isbn;
}
public int getPages() {
return pages;
}
public int getYear() {
return year;
}
//isbn validator
private static boolean isbnValidator(String userIsbn)
{
Pattern p = Pattern.compile("^[0-9]{10}+$");
Matcher matcher = p.matcher(userIsbn);
return matcher.matches();
}
//toString method
public String toString()
{
return getTitle() + " " + "( " + getYear() + " ). " + author + ". " + getPages() +" pages. " + "ISBN " + getIsbn() + ".";
}
}
I commented out all the blocks that call the author array
any one wana give it a whirl?