我正在继续我的上学项目,似乎遇到了另一个错误。所以发生的事情基本上是我收到一个空指针异常,即使代码看起来很好。我相信我的阵列出了点问题,即使经过数小时的搜索,我似乎也找不到该错误。再一次,任何帮助/解决方案将不胜感激。
import java.util.*;
public class library {
private static students stu1 = new students(65435, "Bob", "Ted");
private static students stu2 = new students(45546, "Guy", "Sid");
private static students stu3 = new students(78688, "Tim", "Cas");
private static students stu4 = new students(45387, "Guy", "Jim");
private static students stu5 = new students(12367, "Dom", "Lam");
private static students stu6 = new students(45905, "Kid", "Loo");
private static students stu7 = new students(65484, "Mut", "Hum");
private static students stu8 = new students(34578, "Kim", "Hay");
private static students stu9 = new students(20457, "Roy", "Boy");
private static students stu0 = new students(15678, "Kil", "Bil");
private static students[] studentArray;
private static students[] stuArrayAlt;
private static boolean firstArrayStu = true;
// private static books bookName = new books("title",
// "author","category","isbn", cost, rating out of 10);
private static books book1 = new books(
"Harry Potter and the Deathly Hallows", "JK Rowling", "fantasy",
"9780739360385", 30.00, 9.0);
private static books book2 = new books("Angels and Demons", "Dan Brown",
"fiction", "9780828855495", 25.00, 8.5);
private static books book3 = new books("The Hunger Games",
"Suzanne Collins", "science fiction", "9780439023481", 20.00, 8.0);
private static books book4 = new books("A Game of Thrones",
"George R R Martin", "fantasy", "9780002245845", 54.50, 12.5);
private static books book5 = new books("title2", "author2", "category2",
"isbn2", 54.50, 12.5);
private static books book6 = new books("title2", "author2", "category2",
"isbn2", 54.50, 12.5);
private static books book7 = new books("title2", "author2", "category2",
"isbn2", 54.50, 12.5);
private static books book8 = new books("title2", "author2", "category2",
"isbn2", 54.50, 12.5);
private static books book9 = new books("title2", "author2", "category2",
"isbn2", 54.50, 12.5);
private static books book0 = new books("title2", "author2", "category2",
"isbn2", 54.50, 12.5);
private static books[] bookArray;
private static books[] bookArrayAlt;
private static boolean firstArrayBook;
private static int year1;
private static int month1;
private static int date1;
public library() {
bookArray = new books[] { book1, book2, book3, book4, book5, book6,
book7, book8, book9, book0 };
firstArrayBook = true;
studentArray = new students[] { stu1, stu2, stu3, stu4, stu5, stu6,
stu7, stu8, stu9, stu0 };
firstArrayStu = true;
}
public static void main(String[] args) {
// System.out.println(stu1.getStuNum() + " " + stu1.getFirstName() +
// " "+ stu1.getLastName());
// books[] bookReturn = stu1.insertBook(book1);
// System.out.println(book1.getCheckInLibrary());
// System.out.println(book1.getCheckInLibrary());
// System.out.println(bookReturn[0].getName());
// books[] bookReturn2 = stu1.insertBook(book2);
// System.out.println(book2.getCheckInLibrary());
// System.out.println(book2.getCheckInLibrary());
// stu1.insertBook(book1);
// checkOutBook(stu1,book1);
// System.out.println(stu1);
// stu1=null;
// System.out.println(stu1);
/*
* stu1.lostBookFine(book1); System.out.println(stu1.getFineBalance());
* stu1.lostBookFine(book2); System.out.println(stu1.getFineBalance());
*/
int[] a = new int[] { 1, 2, 3, 4, 5 };
int[] b = new int[(a.length) + 1];
System.arraycopy(a, 0, b, 0, a.length);
b[a.length] = 6;
// for(int i=0;i<b.length;i++)
// {
// System.out.println(b[i]);
// }
/*
* int[] c = new int[]{1,2,3,4,5,6}; b = new int[(c.length)+1];
*
* System.arraycopy( c, 0, b, 0, c.length ); b[c.length]=7;
*
* for(int i=0;i<b.length;i++) { System.out.println(b[i]); }
*/
// int[] date = enterDate();
// int[] date2 = book1.addTwoWeeks(date);
// int[]date2= book1.getDateDue();
System.out.println(studentArray[0].getFirstName());
//boolean array=checkStuNum(45456);
//System.out.println(array);
//students[]array=lookUpLastName("sid");
//array[0].getFirstName();
}
public static void returnBook(students borrower, books bookReturn) {
}
public static books[] lookUpTitleBooks(String title)// //////////////interchange
// the array-boolean
// firstArrayBook--two
// if statements
{
int counter = 0;
for (int i = 0; i < bookArray.length; i++) {
if (title.equalsIgnoreCase(bookArray[i].getTitle())
|| ((bookArray[i].getTitle().toLowerCase())).contains(title
.toLowerCase())) {
counter++;
}
}
books[] booksLookUp = new books[counter];
int counterS = 0;
for (int i = 0; i < bookArray.length; i++) {
if (title.equalsIgnoreCase(bookArray[i].getTitle())
|| ((bookArray[i].getTitle().toLowerCase())).contains(title
.toLowerCase())) {
booksLookUp[counterS] = bookArray[i];
counterS++;
}
}
return booksLookUp;
}
// look up last name of student
public static students[] lookUpLastName(String lName) {
students[] studentlName = new students[1] ;
if (firstArrayStu == true) {
int counter = 0;
System.out.println("check");
for (int i = 0; i < studentArray.length; i++) {
if (lName.equalsIgnoreCase(studentArray[i].getLastName()) || ((studentArray[i].getLastName()).contains(lName.toLowerCase())) ){
counter++;
System.out.println("check if");
}
System.out.println("check for");
}
studentlName = new students[counter];
int counterS = 0;
for (int i = 0; i < studentArray.length; i++) {
if (lName.equalsIgnoreCase(studentArray[i].getFirstName())
|| ((studentArray[i].getFirstName().toLowerCase()))
.contains(lName.toLowerCase())) {
studentlName[counterS] = studentArray[i];
counterS++;
}
}
}
if (firstArrayStu == false) {
int counter = 0;
for (int i = 0; i < stuArrayAlt.length; i++) {
if (lName.equalsIgnoreCase(stuArrayAlt[i].getFirstName())
|| ((stuArrayAlt[i].getFirstName().toLowerCase()))
.contains(lName.toLowerCase())) {
counter++;
}
}
studentlName = new students[counter];
int counterS = 0;
for (int i = 0; i < stuArrayAlt.length; i++) {
if (lName.equalsIgnoreCase(stuArrayAlt[i].getFirstName())
|| ((stuArrayAlt[i].getFirstName().toLowerCase()))
.contains(lName.toLowerCase())) {
studentlName[counterS] = stuArrayAlt[i];
counterS++;
}
}
}
return studentlName;
}
public static void checkOutBook(students borrower, books bookBorrow) {
boolean canBorrow1 = checkFine(borrower);
boolean canBorrow2 = checkBorrowedBooks(borrower);
boolean canBorrow3 = checkBorrowedBooks(bookBorrow);
if (canBorrow1 == false) {
System.out.println("Your fine is too damn high");// alert window and
// redirect to
// main menu-so
// he/she can
// pay it if he
// wants to
}
if (canBorrow2 == false) {
System.out.println("Your already have 3 books checked out");// alert
// window
// and
// redirect
// to
// main
// menu-
// so
// he/she
// can
// return
// a
// book
// if he
// wants
// to
}
if (canBorrow1 == false) {
System.out.println("This book has been checkd out");// alert window
// and redirect
// to main menu-
// so he/she can
// look for
// another book
// and check it
// out
}
if (canBorrow1 && canBorrow2 && canBorrow3) {
borrower.insertBook(bookBorrow);
bookBorrow.checkOutStatusChange();
// alert window to show successful check out and redirect to main
// menu
}
}
public static boolean checkFine(students borrower) {
boolean canBorrow1 = borrower.checkBorrowedBooks(borrower
.getBookArray());
return canBorrow1;
}
public static boolean checkBorrowedBooks(students borrower) {
boolean canBorrow2 = borrower.checkFine(borrower);
return canBorrow2;
}
public static boolean checkBorrowedBooks(books bookBorrow) {
boolean canBorrow3 = bookBorrow.getCheckInLibrary();
return canBorrow3;
}
public static int[] enterDate() {
Scanner input = new Scanner(System.in);
boolean dateTrue = false;
int year, month, date;
while (dateTrue == false) {
System.out.println("Enter year");
year = input.nextInt();
System.out.println("Enter month");
month = input.nextInt();
System.out.println("Enter date");
date = input.nextInt();
// checking first date----------------------------------------
year1 = ((year >= 2010) ? year : 0);
month1 = ((month >= 1 && month <= 12) ? month : 0);
if (month1 == 1) {
date1 = ((date >= 1 && date <= 31) ? date : 0);
}
if (month1 == 2 && (year1 % 4) != 0) {
date1 = ((date >= 1 && date <= 28) ? date : 0);
}
if (month1 == 2 && (year1 % 4) == 0) {
date1 = ((date >= 1 && date <= 29) ? date : 0);
}
if (month1 == 3) {
date1 = ((date >= 1 && date <= 31) ? date : 0);
}
if (month1 == 4) {
date1 = ((date >= 1 && date <= 30) ? date : 0);
}
if (month1 == 5) {
date1 = ((date >= 1 && date <= 31) ? date : 0);
}
if (month1 == 6) {
date1 = ((date >= 1 && date <= 30) ? date : 0);
}
if (month1 == 7) {
date1 = ((date >= 1 && date <= 31) ? date : 0);
}
if (month1 == 8) {
date1 = ((date >= 1 && date <= 31) ? date : 0);
}
if (month1 == 9) {
date1 = ((date >= 1 && date <= 30) ? date : 0);
}
if (month1 == 10) {
date1 = ((date >= 1 && date <= 31) ? date : 0);
}
if (month1 == 11) {
date1 = ((date >= 1 && date <= 30) ? date : 0);
}
if (month1 == 12) {
date1 = ((date >= 1 && date <= 31) ? date : 0);
}
if (month1 == 0 || date1 == 0 || year1 == 0) {
// do nothing boolean remains false
// put alert window here
} else {
dateTrue = true;
}
}
int[] dates = { year1, month1, date1 };
return dates;
}
public void createStudent(int stuNum, String fName, String lName) {
if (firstArrayStu == true) {
for (int i = 0; i < studentArray.length; i++) {
if (studentArray[i] == null) {
studentArray[i] = new students(stuNum, fName, lName);
} else if (i == (studentArray.length - 1)) {
stuArrayAlt = new students[studentArray.length + 1];
System.arraycopy(studentArray, 0, stuArrayAlt, 0,
studentArray.length);
stuArrayAlt[studentArray.length] = new students(stuNum,
fName, lName);
firstArrayStu = false;
}
}
} else if (firstArrayStu == false) {
for (int i = 0; i < stuArrayAlt.length; i++) {
if (stuArrayAlt[i] == null) {
stuArrayAlt[i] = new students(stuNum, fName, lName);
} else if (i == (stuArrayAlt.length - 1)) {
studentArray = new students[stuArrayAlt.length + 1];
System.arraycopy(stuArrayAlt, 0, stuArrayAlt, 0,
stuArrayAlt.length);
studentArray[stuArrayAlt.length] = new students(stuNum,
fName, lName);
firstArrayStu = true;
}
}
}
}
public static void createBook(String name, String author, String category,
String isbn, double cost, double sRating) {
if (firstArrayBook == true) {
for (int i = 0; i < bookArray.length; i++) {
if (bookArray[i] == null) {
bookArray[i] = new books(name, author, category, isbn,
cost, sRating);
} else if (i == (bookArray.length - 1)) {
bookArrayAlt = new books[bookArray.length + 1];
System.arraycopy(bookArray, 0, bookArrayAlt, 0,
bookArray.length + 1);
bookArrayAlt[bookArray.length] = new books(name, author,
category, isbn, cost, sRating);
firstArrayBook = false;
}
}
}
else if (firstArrayBook == false) {
for (int i = 0; i < bookArrayAlt.length; i++) {
if (bookArrayAlt[i] == null) {
bookArrayAlt[i] = new books(name, author, category, isbn,
cost, sRating);
} else if (i == (bookArrayAlt.length - 1)) {
bookArray = new books[bookArrayAlt.length + 1];
System.arraycopy(bookArrayAlt, 0, bookArray, 0,
bookArrayAlt.length + 1);
bookArray[bookArrayAlt.length] = new books(name, author,
category, isbn, cost, sRating);
firstArrayBook = false;
}
}
}
}
public static boolean deleteStudent(String lName, int stuNum) {
students[] arrayLookedUp = lookUpLastName(lName);
boolean deleted = false;
for (int i = 0; i < arrayLookedUp.length; i++) {
if ((arrayLookedUp[i].getStuNum()) == stuNum) {
System.out.println("checker");
if (firstArrayStu == true) {
for (int j = 0; j < studentArray.length; j++) {
if (arrayLookedUp[i] == studentArray[j]) {
studentArray[j] = null;
deleted = true;
break;
}
}
break;
} else if (firstArrayStu == false) {
for (int j = 0; j < stuArrayAlt.length; j++) {
if (arrayLookedUp[i] == stuArrayAlt[j]) {
stuArrayAlt[j] = null;
deleted = true;
break;
}
}
}
} else if (i == (arrayLookedUp.length - 1)) {
deleted = false;
}
}
return deleted;
}
public static boolean checkStuNum(int stuNum) {
// private static students[] studentArray;
// private static students[] stuArrayAlt;
// private static boolean firstArrayStu=true;
boolean stuNumNew = false;
System.out.println("test");
if (firstArrayStu == true) {
for (int i = 0; i < studentArray.length; i++) {
System.out.println("false");
if ((studentArray[i].getStuNum()) == stuNum) {
System.out.println("true");
stuNumNew = true;
break;
}
}
}
else if (firstArrayStu == false) {
for (int i = 0; i < stuArrayAlt.length; i++) {
if ((stuArrayAlt[i].getStuNum()) == stuNum) {
stuNumNew = true;
break;
}
}
}
return stuNumNew;
}
}
我有一个带有构造函数的学生班,并且有一个说明
public String getFirstName() {
return fName;
}
但仍然出现错误。我知道要处理的代码很多,因此再次感谢任何帮助。
由于您的代码是当前编写的,
System.out.println(studentArray[0].getFirstName());
由于您永远不会初始化阵列,因此将抛出NPE。
private static students[] studentArray;
只声明它,不初始化它。
也许您应该按照以下方式重构代码
private static ArrayList<students> studentArray = new ArrayList<students>();
// Inside the constructor or some method called before you access studentArray:
studentArray.add(stu1);
studentArray.add(stu2);
//...
// or, more concisely, since you're not using the temporary `students` variables:
studentArray.add(new students(65435, "Bob", "Ted"));
studentArray.add(new students(45546, "Guy", "Sid"));
//...
如果您要使用数组而不是列表,
private static students[] studentArray;
需要用类似的东西初始化
private static students[] studentArray = new students[10];
然后您需要分配其元素
studentArray[0] = stu1;
要么
studentArray[0] = new students(65435, "Bob", "Ted");
问题内容: 有可能这可能是一个双重问题。我将String变量初始化为null。我可能会或可能不会使用一个值更新它。现在我想检查此变量是否不等于null以及我尝试执行的操作是否会得到null指针异常。空指针异常,因为它代价高昂。是否有任何有效的解决方法.TIA 问题答案: 如果您使用 你 不会 得到。 我怀疑你在做什么: 这是因为null 而引发,而不是因为null。 如果仍然无法解释,请发布您用于
我已经更新了我的项目中的一些依赖关系之后,我的Hibernate配置类显示Nullpointerx的。 我将SpringDataJPA存储库与hibernate一起使用,已经超过24小时了,仍然没有找到任何关于小问题的适当解决方案。 我已经尝试过的一些解决方案:- 使用@bean(name=“entityManagerFactory”)提供bean名称 我面临的问题 波姆。xml文件 配置类 db
问题内容: 我最近在学习Java,并尝试使用类。但是我无法初始化数组对象 谁能告诉我我要去哪里错了? 问题答案: 当你做 您正在创建 引用数组(即引用变量数组) ,这些 引用数组 未指向其他任何地方,即它们为null。您需要将对象分配给数组中上面创建的引用变量。
当我试图从DB查询结果时,我得到一个空指针异常。以下是错误: 这是错误产生的函数。 如果我启动应用程序信息服务(=new ApplicationInfoService()),我会得到一个指向我的服务类的空指针错误,在这里进行查询 错误转到createQuery行,如果我打印出EntityManager,它将返回“null”。为什么我的Entitymanager不能初始化。我在我的登录系统中使用了几
我在当前版本的Java8中发现了一个奇怪的行为。在我看来,下面的代码应该可以,但是JVM抛出了一个: 这并不重要,它是什么样的lambda表达式(与相同)或使用了什么泛型类型。也可以用更有意义的表达式来代替。上面的例子很短。这里有一个更丰富多彩的例子: 然而,这些代码段运行良好: 和 或与功能: 我测试了两个Java版本: openjdk版本“1.8.0_66-internal”openjdk运行
问题内容: 嗨,我们一直在使用apache编写我们的Java程序来读取xls和xlsx文件,问题是我们由于两个原因而得到了空指针异常。第一个是已经解决的空白单元格,另一个是当我们选择一个没有任何记录的列。 我们的程序要求输入excel文件的路径,然后输入要读取的文件的特定工作表号和特定的工作表的列号。这是读取xls文件的代码 变量工作表名称用于excel文件的工作表编号 变量cols用于您要阅读的