Reputation: 63
I'm new to Java. Tried to do a simple library and I've encountered a problem.
"Exception in thread "main" java.lang.NullPointerException at Book.showData(Book.java:22) at Book.main(Book.java:28) Java Result: 1"
I know how to make this work with one method. But when I use two methods - one to read and the other one to show the data, I get an error. I think I'm not reffering to string matrix correctly but I don't really know how to fix it.
import java.util.Scanner;
public class Book {
int bookID;
String bookName;
String bookAuthor;
int publishDate;
public String readData(){
Scanner scanner = new Scanner(System.in);
System.out.println("Book's name: ");
this.bookName = scanner.nextLine();
System.out.println("\nAuthor's name: ");
this.bookAuthor = scanner.nextLine();
System.out.println("\nYear of publish: ");
this.publishDate = scanner.nextInt();
return bookAuthor;
}
public void showData(){
String[] names = bookAuthor.split(" ");
System.out.println(bookName+" author is "+String.format("%s %s", names[0], names[names.length-1]));
}
public static void main(String args[]){
new Book().readData();
new Book().showData();
}
}
Upvotes: 1
Views: 3518
Reputation: 267
You are creating two instance of class Book.Create a single instance and call readData() and showData() functions. Sample Code
public static void main(String args[]){
Book myBook = new Book();
myBook.readData();
myBook.showData();
}
All other function you can keep the same.
Nullpointer is thrown because there are no values in the string for showData() when new instance is created.
Upvotes: 1
Reputation: 93872
You're creating two instances of your Book
object. So when you call showData
on the second one, your Strings are not initialized (the default value for an object is null
) and hence the NPE is thrown when you try to split on the bookAuthor
variable.
You have to create one instance of Book
and then perform operations on it.
Book b = new Book();
b.readData(); //<-- here you initialize the value fields for this instance (the instance b)
b.showData(); //<-- it's ok to use them now, they have been initialized !
Upvotes: 4
Reputation: 3585
You created a new instance of Book 2 times in main method. As a result in the invocation of showData()
the instance variables were not initialized,
try
Book book = new Book();
book.readData();
book.showData();
also you can move readData()
into constructor
import java.util.Scanner;
public class Book {
int bookID;
String bookName;
String bookAuthor;
int publishDate;
public Book() {
Scanner scanner = new Scanner(System.in);
System.out.println("Book's name: ");
this.bookName = scanner.nextLine();
System.out.println("\nAuthor's name: ");
this.bookAuthor = scanner.nextLine();
System.out.println("\nYear of publish: ");
this.publishDate = scanner.nextInt();
return bookAuthor;
}
public void showData(){
String[] names = bookAuthor.split(" ");
System.out.println(bookName+" author is "+String.format("%s %s", names[0], names[names.length-1]));
}
public static void main(String args[]){
new Book().showData();
}
}
Upvotes: 1
Reputation: 9331
Problem is in your main() method :
public static void main(String args[]){
new Book().readData();
new Book().showData();
}
In this line new Book().readData();
you are creating new Book instance and inserting data into that instance. But when you tried yo show data you are creating another instance and trying to show data from there but it is empty because it is new new Book().showData();
To fix it you have to create 1 instance, insert data into it, and show data from that same instance of the class. For example like this :
public static void main(String args[]){
Book book = new Book(); //create book object
book.readData(); //insert data into it
book.showData(); //show data from the same object
}
Upvotes: 1
Reputation: 283
Because you created two instances of the same class instead of using the same. So the bookAuthor
variable that you initialized in readData()
not longer exists in showData()
.
Change your main()
function to
public static void main(String args[]){
Book foo = new Book();
foo.readData();
foo.showData();
}
Upvotes: 1
Reputation: 431
The problem is that you are working with two different instances of Book
. Create an instance of Book and work with that for both readData()
and showData()
.
Book book = new Book();
book.readData();
book.showData();
Upvotes: 1
Reputation: 2871
You are calling readData()
and showData()
on different instances of Book
. You need create one instance of Book
class and invoke your methods on that instance.
Change your main() to this :
public static void main(String args[]){
Book book = new Book();
book.readData();
book.showData();
}
Upvotes: 1