4

I want to add data to ArrayList object. In my code addBook() method will be show Input Dialogue Box and pass this string to isbn variable. Now there is need to add isbn variable data to ArrayList which is reside in BookInfoNew() constructor but list object not found in addBook() method. (list.add(isbn))

kindly help me.

import java.util.*;
import javax.swing.JOptionPane;

public class BookInfoNew {
    private static String isbn;
    private static String bookName;
    private static String authorName;
    public static int totalBooks = 0;

    //default constructor
    public BookInfoNew() {
        List<String> list = new ArrayList<String>(); //create ArrayList
    }

    //Parameterized constructor
    public void BookInfoNew(String x, String y, String z) {
        isbn = x;
        bookName = y;
        authorName = z;
    }

    //add book method
    public void addBook() {
        String isbn = JOptionPane.showInputDialog("Enter ISBN");

        //add books data to ArrayList
        list.add(isbn);
    }
}
3
  • 3
    because list is a local variable, which only exists in your non parameter constructor. Commented Nov 19, 2015 at 8:04
  • 1
    Why are the other fields static? Commented Nov 19, 2015 at 8:08
  • your question title is erroneous , since your addBook() doesn't even have access to list Commented Nov 19, 2015 at 8:10

2 Answers 2

8

This is an issue with scope. You cannot access your list object within the addBook() object. So, you have to either make list a parameter to addBook() or you can make it a global variable.

This code fixes it using a global variable:

import java.util.*;
import javax.swing.JOptionPane;

public class BookInfoNew {
    private String isbn;
    private String bookName;
    private String authorName;
    public int totalBooks = 0;

    // global list variable here which you can use in your methods
    private List<String> list;

    //default constructor
    public BookInfoNew() {
        list = new ArrayList<String>(); //create ArrayList
    }

    //Parameterized constructor - constructor has no return type
    public BookInfoNew(String x, String y, String z) {
        isbn = x;
        bookName = y;
        authorName = z;
    }

    //add book method
    public void addBook() {
        String isbn = JOptionPane.showInputDialog("Enter ISBN");

        //add books data to ArrayList
        list.add(isbn);
    }
}
Sign up to request clarification or add additional context in comments.

7 Comments

Your code has been edited. As @manu asked, why are your other variables static?
Now list.add(isbn) is not working. it says list.add(isbn) method is undefined.
its works well when list.add(isbn) will in BookInfoNew() constructor
If you want to run addBook() every time you create a new BookInfoNew object, you can just write addBook() in the constructor.
And maybe you should remove the String in front of isbn in the addBook() method. That way you would be updating the instance variable isbn and not a local variable.
|
1

You should rewrite your code a little bit like that:

...
List<String> list = null;
public BookInfoNew() {
    list = new ArrayList<String>(); //create ArrayList
}
...

and it should be ok.

1 Comment

No need for = null;

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.