Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: What is Wrong When calling the function using arrayList.

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default What is Wrong When calling the function using arrayList.

    code for library.java

    import java.util.ArrayList;
    public class Library {
    private String libraryName,libAddress;
    Library(String libraryName,String libAddress)
    {
    this.libraryName = libraryName;
    this.libAddress =libAddress;

    }
    public String getLibraryName() {
    return libraryName;
    }
    public void setLibraryName(String libraryName) {
    this.libraryName = libraryName;
    }
    public String getLibAddress() {
    return libAddress;
    }
    public void setLibAddress(String libAddress) {
    this.libAddress = libAddress;
    }
    ArrayList<Book> bookList = new ArrayList<Book>();
    ArrayList<Member> memberList = new ArrayList<Member>();

    public void addBook(Book book)
    {
    bookList.add(book);

    }

    public void bookInfo()
    {
    for (int i=0; i< bookList.size(); i++)
    {
    System.out.println("Author of " +bookList.get(i).getName()+" is "+bookList.get(i).getAuthorName());
    }
    }
    public void getBookLend(String bookName) //function call to check the name of person to whom book is issued
    {

    System.out.println(bookName);

    for(Book boo : bookList)
    if(bookName.equals(boo.getName()))
    {
    System.out.println("In Loop "+boo.getName());
    System.out.println(boo.getNameOfPerson());
    System.out.println("Book Issued To " +boo.getNameOfPerson()); //ERROR HERE. GETTING NULL WHY???

    }
    }
    }


    code for member.java




    import java.util.ArrayList;

    public class Member {
    String memberName;
    int id;
    ArrayList<Member> memberList = new ArrayList<Member>();
    public Member(String memberName, int id) {
    this.memberName = memberName;
    this.id = id;
    }

    public String getMemberName() {
    return memberName;
    }
    public void setMemberName(String memberName) {
    this.memberName = memberName;
    }
    public int getId() {
    return id;
    }
    public void setId(int id) {
    this.id = id;
    }
    public ArrayList<Member> getMemberList() {
    return memberList;
    }
    public void setMemberList(ArrayList<Member> memberList) {
    this.memberList = memberList;
    }
    public void addMember(Member member)
    {
    memberList.add(member);

    System.out.println(member.memberName +" added Successully as a member");
    }
    public void showMember()
    {
    for(Member member : memberList)
    System.out.println(member.getMemberName());
    }

    }



    Code for book.java






    public class Book {
    private String name,authorName,catagory;
    private int noOfCopies,bookId;
    private String nameOfPerson;
    Book(String name ,int bookId,String authorName,String catagory,int noOfCopies)
    {
    this.name =name;
    this.authorName=authorName;
    this.catagory =catagory;
    this.noOfCopies = noOfCopies;

    }
    public String getName() {
    return name;
    }
    public void setName(String name) {
    this.name = name;
    }
    public String getAuthorName() {
    System.out.println("nutan");
    return authorName;
    }
    public void setAuthorName(String authorName) {
    this.authorName = authorName;
    }
    public String getCatagory() {
    return catagory;
    }
    public void setCatagory(String catagory) {
    this.catagory = catagory;
    }
    public int getNoOfCopies() {
    return noOfCopies;
    }
    public void setNoOfCopies(int noOfCopies) {
    this.noOfCopies = noOfCopies;
    }
    public String getNameOfPerson() {
    return nameOfPerson;
    }
    public void setNameOfPerson(String nameOfPerson) {
    this.nameOfPerson = nameOfPerson;
    }
    public void bookLend(int bookId,String nameOfPerson) //function called when a book is issued
    {

    if(noOfCopies == 0)
    System.out.println("Non Applicable");
    else
    {
    noOfCopies--;
    this.nameOfPerson = nameOfPerson;
    this.setNameOfPerson(this.nameOfPerson);
    System.out.println("book issued to \t"+getNameOfPerson() +"\t");
    System.out.println("Number Of Available copies \t" +noOfCopies);
    }
    }

    }

    Code for Execution.java



    public class Execution {
    public static void main(String[] args) {

    Library lib = new Library("Universal Library","MUMBAI"); //Library Constructor
    Book book1 = new Book("Book Java",1,"Scmidt","computers",1);
    Book book2 = new Book("C++",2,"Bjarne","computers",6); //Book Objects
    Book book3 = new Book("C",3,"Yaswant","computers",4);
    Book book4 = new Book("Adobe Basics",4,"Mr.","basics",3);
    Book book5 = new Book("Learning",5,"Paul","english",2);
    Book book6 = new Book("How To?",6,"Ravi","vhut",1);
    Member member1 = new Member("nn", 01);
    Member member2 = new Member("ri", 02); //Members objects
    lib.addBook(book1);
    lib.addBook(book2);
    lib.addBook(book3); //calling addBook function to add books in the list
    lib.addBook(book4);
    lib.addBook(book5);
    lib.addBook(book6);
    member1.addMember(member1);
    member2.addMember(member2);
    book6.bookLend(6,"nu"); //function to issue books
    lib.getBookLend("C++"); // function call to find out whom book is issued
    }
    }


  2. #2
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    609
    My Mood
    Sleepy
    Thanks
    10
    Thanked 93 Times in 86 Posts

    Default Re: What is Wrong When calling the function using arrayList.

    I don't think that anybody will be bothered to compile and run all that stuff. What compiler errors do you get? What runtime Exceptions do you get? Post the full error messages.

  3. #3
    Junior Member
    Join Date
    Mar 2013
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: What is Wrong When calling the function using arrayList.

    Quote Originally Posted by PhHein View Post
    I don't think that anybody will be bothered to compile and run all that stuff. What compiler errors do you get? What runtime Exceptions do you get? Post the full error messages.
    Ya i know... there are no compiling errors... the output which i m getting of System.out.println("Book Issued To " +boo.getNameOfPerson()); in library.java is null... Just call the getBookLend() method in Execution.java .... please help me.

  4. #4
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    609
    My Mood
    Sleepy
    Thanks
    10
    Thanked 93 Times in 86 Posts

    Default Re: What is Wrong When calling the function using arrayList.

    Ok. You have six different books. You have two members.
    book6.bookLend(6,"nu"). Book "How to?" is lend to a person "nu" , who isn't even a member.
    lib.getBookLend("C++"); -> NULL, because no member has that book.

    Everything is good. Your code works as expected.

    --- Update ---

    Another one: do the books lend themselves (book6.bookLend) , or rather the library (lib.bookLend(bookName, memberName))?

  5. #5
    Junior Member
    Join Date
    Mar 2013
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: What is Wrong When calling the function using arrayList.

    means i have to create bookLend method inside the Library..............

    and thanks for your help..

  6. #6
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    609
    My Mood
    Sleepy
    Thanks
    10
    Thanked 93 Times in 86 Posts

    Default Re: What is Wrong When calling the function using arrayList.

    It would be more intuitive to me.

Similar Threads

  1. what's wrong with this function?
    By victorsaur in forum Other Programming Languages
    Replies: 3
    Last Post: July 10th, 2012, 12:58 PM
  2. user input going to wrong ArrayList
    By havinFun in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 15th, 2012, 02:58 PM
  3. Ordering ArrayList by 3 conditions as you add to ArrayList
    By aussiemcgr in forum Collections and Generics
    Replies: 4
    Last Post: July 13th, 2010, 02:08 PM
  4. [SOLVED] Extracting an How to ArrayList from an ArrayList and convert to int??
    By igniteflow in forum Collections and Generics
    Replies: 2
    Last Post: August 16th, 2009, 01:11 PM
  5. How to use an ArrayList and what is its advantage over array?
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 1
    Last Post: May 17th, 2009, 01:12 PM