这是我们的任务。请运行代码为您解决问题..
任务 1. Slumbook_.java 创建一个包含 slumbook 条目的类。您的 slumbook 条目应具有以下规范: - 最少 (5) 个通常在 Slumbook 中找到的属性。- 至少 (2) 构造函数 - 为您列出的所有属性提供必要的访问器和修改器方法 - 至少 (1) 辅助方法 附加约束 不要再使用以下作为属性,因为它们太常见了:名字、中间名、姓氏姓名、年龄和地址。
任务 2. SlumbookDemo_.java 创建一个类 SlumbookDemo,它可以包含最少 10 个和最多 20 个 Slumbook 条目对象的条目(使用您在第一个任务中创建的类)。您的 slumbook 演示/驱动程序应为 slumbook 提供以下方法。- 添加条目 - 删除条目 - 查看所有条目 - 更新条目 - 退出/退出程序 以上是我们的机器问题.. 我创建了这个,但我堆叠了,,
这是我的课:
public class slumbook_gamoranao {
//attributes
private String fn=""; // entries name
private String fs=""; // fav sport
private String fc=""; // fav color
private String fsin=""; // fav singer
private String fp=""; // fav pet
//constructors
public slumbook_gamoranao() {
fn = "";
fs = "";
fc = "";
fsin ="";
fp = "";
}
public slumbook_gamoranao(String fn, String fs, String fc, String fsin, String fp) {
this.fn = fn;
this.fs = fs;
this.fc = fc;
this.fsin = fsin;
this.fp = fp;
}
//accessors or getters
public String getFn() { return fn; }
public String getFs() { return fs; }
public String getFc() { return fc; }
public String getFsin() { return fsin; }
public String getFp() { return fp; }
//mutators or setters
public void setFn(String x) { this.fn = x; }
public void setFs(String y) { this.fs = y; }
public void setFc(String z) { this.fc = z; }
public void setFsin(String xx) { this.fsin = xx; }
public void setFp(String yy) { this.fp = yy; }
//helpers (you can modify this)
public String helper() {
String info1 =
"Favorite book:" + getFn() +
"\nFavorite sport: " + getFs() +
"\nFavorite color: " + getFc() +
"\nFavorite singer: " + getFsin() +
"\nAddress: " + getFp();
return info1;
}
}
这是我的司机:
import java.util.*;
public class slumbookdemo_gamoranao{
public static void main(String args[]) {
int q=0;// for switch case;
int e=0;// for do while;
int r;// for For loop;
int c1=0; // couting of entries
int c2=0;
Scanner sc = new Scanner(System.in);
slumbook_gamoranao[] p1 = new slumbook_gamoranao [20];
p1[c1++] = new slumbook_gamoranao();
do {
p1[c1++] = new slumbook_gamoranao();
System.out.println("Select one!");
System.out.println("(1) Add entry");
System.out.println("(2) Delete entries");
System.out.println("(3) View entries");
System.out.println("(4) Update an entries");
System.out.println("(5) Quit the program");
q = sc.nextInt();
switch(q)
{
case 1: // add entry
p1[c1].setFn(sc.nextLine()); // I add this bcos in the book part, dont ask the input, So I tried to input this then the book part is working..
System.out.println("Input your name!");
p1[c1].setFn(sc.nextLine());
System.out.println("Input favorite sport");
p1[c1].setFs(sc.nextLine());
System.out.println("Input favorite color");
p1[c1].setFc(sc.nextLine());
System.out.println("Input favorite singer");
p1[c1].setFsin(sc.nextLine());
System.out.println("Input favorite pet");
p1[c1].setFp(sc.nextLine());
c1++;
break;
case 2:// delete entry
break;
case 3: // view entry
for (r=0;r<=p1.length; r++ ){
System.out.println("Favorite book:" + p1[r].getFn() +
"\nFavorite sport: " + p1[r].getFs() +
"\nFavorite color: " + p1[r].getFc() +
"\nFavorite singer: " + p1[r].getFsin() +
"\nAddress: " + p1[r].getFp());
}
break;
case 4:// update entries
case 5: // quit or exit
System.exit(0);
}
System.out.println("press 1 to go back to menu");
e = sc.nextInt();
while(e!=1)
{
System.out.println("Try again! Please press #1 !!");
e = sc.nextInt();
}
}while(e==1);
}
}
..问题是,我运行后,菜单会提示然后我选择1后,这个错误
Exception in thread "main" java.lang.NullPointerException at slumbookdemo_gamoranao.main(slumbookdemo_gamoranao.java:31)
但是在我运行之后,它只会显示一个信息..
抱歉英语不好,TT