- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我正在继续我的学校项目,但似乎遇到了另一个错误。所以发生的事情基本上是我收到一个空指针异常,即使代码看起来不错。我相信我的阵列有问题,即使经过数小时的搜索,我似乎也找不到错误。再一次,任何帮助/解决方案将不胜感激。
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");
关于java - 数组空指针异常错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15747516/
问题很简单:我正在寻找一种优雅的使用方式 CompletableFuture#exceptionally与 CompletableFuture#supplyAsync 一起.这是行不通的: priva
对于 Web 服务,我们通常使用 maven-jaxb2-plugin 生成 java bean,并在 Spring 中使用 JAXB2 编码。我想知道如何处理 WSDL/XSD 中声明的(SOAP-
这个问题已经有答案了: Array index out of bound behavior (10 个回答) 已关闭 8 年前。 我对下面的 C 代码感到好奇 int main(){
当在类的开头使用上下文和资源初始化 MediaPlayer 对象时,它会抛出 NullPointer 异常,但是当在类的开头声明它时(因此它是 null),然后以相同的方式初始化它在onCreate方
嘿 我尝试将 java 程序连接到 REST API。 使用相同的代码部分,我在 Java 6 中遇到了 Java 异常,并且在 Java 8 中运行良好。 环境相同: 信任 机器 unix 用户 代
我正在尝试使用 Flume 和 Hive 进行 Twitter 分析。为了从 twitter 获取推文,我在 flume.conf 文件中设置了所有必需的参数(consumerKey、consumer
我在 JavaFX 异常方面遇到一些问题。我的项目在我的 Eclipse 中运行,但现在我的 friend 也尝试访问该项目。我们已共享并直接保存到保管箱文件夹中。但他根本无法让它发挥作用。他在控制台
假设我使用 blur() 事件验证了电子邮件 ID,我正在这样做: $('#email').blur(function(){ //make ajax call , check if dupli
我这样做是为了从 C 代码调用非托管函数。 pCallback 是一个函数指针,因此在托管端是一个委托(delegate)。 [DllImport("MyDLL.dll")] public stati
为什么这段代码是正确的: try { } catch(ArrayOutOfBoundsException e) {} 这是错误的: try { } catch(IOException e) {} 这段
我遇到了以下问题:有导出函数的DLL。 代码示例如下:[动态链接库] __declspec(dllexport) int openDevice(int,void**) [应用] 开发者.h: __de
从其他线程,我知道我们不应该在析构函数中抛出异常!但是对于下面的例子,它确实有效。这是否意味着我们只能在一个实例的析构函数中抛出异常?我们应该如何理解这个代码示例! #include using n
为什么需要异常 引出 public static void main(String[
1. Java的异常机制 Throwable类是Java异常类型的顶层父类,一个对象只有是 Throwable 类的(直接或者间接)实例,他才是一个异常对象,才能被异常处理机制识别。JDK中内
我是 Python 的新手,我对某种异常方法的实现有疑问。这是代码(缩写): class OurException(Exception): """User defined Exception"
我已经创建了以下模式来表示用户和一组线程之间的关联,这些线程按他们的最后一条消息排序(用户已经阅读了哪些线程,哪些没有): CREATE TABLE table(user_id bigint, mes
我正在使用 Python 编写一个简单的自动化脚本,它可能会在多个位置引发异常。在他们每个人中,我都想记录一条特定的消息并退出程序。为此,我在捕获异常并处理它(执行特定的日志记录操作等)后引发 Sys
谁能解释一下为什么这会导致错误: let xs = [| "Mary"; "Mungo"; "Midge" |] Array.iter printfn xs 虽然不是这样: Array.iter pr
在我使用 Play! 的网站上,我有一个管理部分。所有 Admin Controller 都有一个 @With 和一个 @Check 注释。 断开连接后,一切正常。连接后,每次加载页面(任何页面,无论
我尝试连接到 azure 表存储并添加一个对象。它在本地主机上工作得很好,但是在我使用的服务器上我得到以下异常及其内部异常: Exception of type 'Microsoft.Wind
我是一名优秀的程序员,十分优秀!