[英]Java Arrays: Added object to array but is null. How to modify?
為此,我正在運行BlueJ做面向對象的Java,並且我有JVM 7.1活動。
好的,所以我必須為我的學位提出申請,而我遇到了麻煩。 我很感謝“老師”讓我使用我的三陣列系統,因為迭代器弄亂了我的單陣列系統。
遺憾的是,我無法在互聯網上找到任何東西來解決我的問題。
基本上,當我去向數組添加“ Student”時,該對象在所有字段中均為null。 而且我不知道如何修改它們。
我的數組使用菱形符號。 類似於以下內容:
private ArrayList<Student> students;
這是我當前使用的數組形式,還有2個以上的數組。
private ArrayList<CollegeEmployee> staff;
private ArrayList<Faculty> members;
建議我使用單個陣列,但是如上所述,迭代器將其弄亂了,只要我能修復它,我就不在乎我有多少個陣列。
該程序在while循環中運行,並提示用戶為某個命令輸入字母。 當用戶輸入“ s”時,然后將一個學生添加到數組中,這就是問題所在。該對象被賦予空值,我不知道如何修改它們...以下是調用該方法的方法對象構造函數
private void addStudent()
{
Student b = new Student(firstName, lastName, address, postCode, phoneNumber, gradePointAvg, selectedCourse);
students.add(b);
}
當用戶退出程序時打印數組時,我從終端窗口中獲得以下輸入:
null,null,null,0、0 0 null
我的問題是:如何給對象b值覆蓋空值。 順便說一句,這些值必須由用戶輸入,而不是在數組的聲明中預先設置。
感謝您的時間 :)
這些東西使我討厭代碼xD
編輯:
這是學生類的構造函數和字段:
private String title;
public double gradePointAvg;
public String selectedCourse;
/**
* Constructor for objects of class Student
*/
public Student(String firstName, String lastName, String address, int postCode, int phoneNumber, double gradePointAvg, String selectedCourse)
{
super(firstName, lastName, address, postCode, phoneNumber);
this.title = title;
this.gradePointAvg = gradePointAvg;
this.selectedCourse = selectedCourse;
}
人超類構造函數:
public String firstName;
public String lastName;
public String address;
public int postCode;
public int phoneNumber;
/**
* Constructor for objects of class Person
*/
public Person(String firstName, String lastName, String address, int postCode, int phoneNumber)
{
// initialise fields
this.firstName = firstName;
this.lastName = lastName;
this.address = address;
this.postCode = postCode;
this.phoneNumber = phoneNumber;
}
上面的方法addStudent調用了Student構造函數,然后又調用了person構造函數。 是的,它確實必須像這樣,因為它是項目的限制。
我將我的字段設為公開,因為它們被設置為受保護字段,因為私有是本地范圍。
非常感謝你們檢查我的代碼。.我希望我的老師更好>。<
您是否初始化了ArrayList?
private ArrayList<Student> students;
students = new ArrayList<Student>();
其他
private ArrayList<Student> students = new ArrayList<Student>();
如果未初始化,它將返回null值,並且您將獲得null指針異常。
在你的學生類firstName, lastName, address,selectedCourse
大概是String
和Java將初始化實例變量,是對象(字符串你的情況),以null
,您的其他領域即postCode, phoneNumber
,我認為是int
默認值為0。
當您在addStudent
方法中時,檢查用於構造該對象的值,似乎所有的Strings為null且int尚未初始化。
Student b = new Student(firstName, lastName, address, postCode, phoneNumber, gradePointAvg, selectedCourse);
當執行上述代碼時,我認為firstName,lastName等參數都沒有任何值,這就是為什么它向您顯示null的原因。
在將新的Student
對象添加到列表名單中的students
,提示您的用戶提供firstName, lastName, address
等的值,然后使用這些值構造您的Student
對象。
更新示例代碼:
學生班:
package com.mumz.test.scanner;
public class Student {
private String firstName = null;
private String lastName = null;
/**
* @param firstName
* @param lastName
*/
public Student(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
/**
* @return the firstName
*/
public String getFirstName() {
return firstName;
}
/**
* @param firstName the firstName to set
*/
public void setFirstName(String firstName) {
this.firstName = firstName;
}
/**
* @return the lastName
*/
public String getLastName() {
return lastName;
}
/**
* @param lastName the lastName to set
*/
public void setLastName(String lastName) {
this.lastName = lastName;
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return String.format("Student [firstName=%s, lastName=%s]", firstName, lastName);
}
}
主班
package com.mumz.test.scanner;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class StudentScannerApp {
private List<Student> students = new ArrayList<Student>();
public static void main(String[] args) {
StudentScannerApp studentScannerApp = new StudentScannerApp();
//This scanner we will use for reading data from input stream
Scanner inputStream = new Scanner(System.in);
System.out.println("Enter First Name");
String firstName = inputStream.nextLine();
System.out.println("Enter Last Name");
String lastName = inputStream.nextLine();
studentScannerApp.addStudent(firstName, lastName);
System.out.println("Enter Another student details");
System.out.println("Enter First Name");
firstName = inputStream.nextLine();
System.out.println("Enter Last Name");
lastName = inputStream.nextLine();
studentScannerApp.addStudent(firstName, lastName);
System.out.println(studentScannerApp.students);
}
private void addStudent(String firstName, String lastName){
Student student = new Student(firstName, lastName);
students.add(student);
}
}
我想學生擴大人,不是嗎?
然后您的構造函數應如下所示
public Student(String firstName, String lastName, String address, int postCode, int phoneNumber){
super(firstName, lastName, address, postCode, phoneNumber);
//Do whatever stuff Students do and normal Persons do not do.
}
並且您的addStudent方法應通過以下方式添加學生:
private void addStudent(Student student){
students.add(student);
}
(無論如何,將其私有化並沒有多大意義,但我想您還在學習中)
因此,當您要創建和添加新學生時,只需執行此操作(當然,您可以這樣做,但是如果您不希望在數組中容納大量名為John Smith的學生,那么您確實應該使用變量。 )
Student student = new Student("John", "Smith", "Fake Street", 123, 12345, 5555555);
students.add(student);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.