Java 我一直得到一个空指针异常
我正在制作一个图书馆程序,记录所有藏书以及每本书的数量。这是密码Java 我一直得到一个空指针异常,java,Java,我正在制作一个图书馆程序,记录所有藏书以及每本书的数量。这是密码 import java.util.Arrays; import java.util.Scanner; public class Library{ static String title; static String author; static int id; static int copies; static String date; static Book[] database = new Book[1
import java.util.Arrays;
import java.util.Scanner;
public class Library{
static String title;
static String author;
static int id;
static int copies;
static String date;
static Book[] database = new Book[100];
static int count=0;
public static void main(String[] args){
int i;
Scanner s = new Scanner(System.in);
do{
addBook();
System.out.println("would you like to add another book?");
i=s.nextInt();
}while(i == 0);
database[0].viewDetails();
database[1].viewDetails();
checkingOut();
}
public static void addBook(){
Scanner s = new Scanner(System.in);
System.out.println("Enter the title of the book you want to add to the collection");
title=s.nextLine();
System.out.println("Enter the author of the book you want to add to the collection");
author=s.nextLine();
System.out.println("Enter the publishing date of the book you want to add to the collection");
date=s.nextLine();
System.out.println("Enter the ID number of the book you want to add to the collection");
id=s.nextInt();
System.out.println("Enter the the number of copies that will be added into the collection");
copies=s.nextInt();
Book Book1 = new Book(date, author, copies, id, title);
database[count] = Book1;
count++;
}
public static void checkingOut(){
boolean found=false;
int idSearch;
int i=0;
Scanner s = new Scanner(System.in);
System.out.println("Enter the ID number of the book you want to check out");
idSearch=s.nextInt();
while(i<database.length && found!=true){
if(database[i].getIdentificationNumber() == idSearch){
found = true;
}
i++;
}
if(found==true){
database[i].checkOut();
System.out.println("There are "+database[i].getNumberCopies()+" copies left");
}
else{System.out.println("There is no book with that ID number!");}
}
}
导入java.util.array;
导入java.util.Scanner;
公共班级图书馆{
静态字符串标题;
静态字符串作者;
静态int-id;
静态整型拷贝;
静态字符串日期;
静态图书[]数据库=新书[100];
静态整数计数=0;
公共静态void main(字符串[]args){
int i;
扫描仪s=新的扫描仪(System.in);
做{
addBook();
System.out.println(“您想添加另一本书吗?”);
i=s.nextInt();
}而(i==0);
数据库[0]。viewDetails();
数据库[1]。viewDetails();
签出();
}
公共静态void addBook(){
扫描仪s=新的扫描仪(System.in);
System.out.println(“输入要添加到收藏中的书籍的标题”);
title=s.nextLine();
System.out.println(“输入要添加到收藏中的书籍的作者”);
作者=s.nextLine();
System.out.println(“输入要添加到收藏中的书籍的出版日期”);
日期=s.nextLine();
System.out.println(“输入要添加到收藏中的书籍的ID号”);
id=s.nextInt();
System.out.println(“输入将添加到集合中的副本数”);
拷贝数=s.nextInt();
Book Book1=新书(日期、作者、份数、id、标题);
数据库[计数]=Book1;
计数++;
}
公共静态无效签出(){
布尔值=false;
国际情报研究;
int i=0;
扫描仪s=新的扫描仪(System.in);
System.out.println(“输入要签出的图书的ID号”);
idSearch=s.nextInt();
而(ifound==true,not found=true)if(found=true)
将始终执行,因为赋值表达式返回赋值,这将导致数据库[i].checkOut();
在不应该执行的地方执行
你应该写:
if(找到)
这就是为什么我们在比较布尔值时避免写=
的原因。如果(someBoolean)
应该是if(found==true)
使用
=
而不是=
,因为=
将始终计算为true
哪一行抛出了NPE?第57行…但是我的程序有另一个问题。在我的add book方法中有方法生成新的book对象吗?我的意思是每次运行该方法生成book1,然后第二次生成book2,then book 3…等等。因为现在当我尝试添加一本新书时,它基本上只是重置我为上一本书所做的信息。我如何知道哪一行是第57行?你希望我数到57行代码吗?你能不能不放一些形式的标记或告诉我们在那一行上是什么?对不起,这基本上是w在checkingOut方法中循环时,有些东西不能正常工作。我不知道如何修复它。如果我创建例如3个book对象并将它们放置在一个数组中,我只能对数组中的第一个对象使用checkingOut方法,当我尝试访问其他对象时,我会收到一个空指针异常。感谢各位,这是一个愚蠢的错误,c我真不敢相信我发现不了。不过我的程序还有一个问题。在我的add book方法中,有没有办法生成新的book对象?我的意思是每次运行该方法生成book1,然后第二次生成book2,然后生成book3……等等。因为现在我尝试添加新书时,基本上只是重置信息这是我为上一本书做的。