0

これが私たちの仕事です。問題を解決するためにコードを実行してください..

タスク 1. Slumbook_.java Slumbook エントリを含むクラスを作成します。スラムブック エントリには、次の仕様が必要です。- atleast (2) コンストラクター - リストしたすべての属性に必要なアクセサー メソッドとミューテーター メソッドを提供します - atleast (1) ヘルパー メソッド 追加の制約 以下は一般的すぎるため、属性として使用しないでください。名前、年齢、住所。

タスク 2. SlumbookDemo_.java Slumbook エントリ オブジェクトの最小 10 および最大 20 エントリを含むクラス SlumbookDemo を作成します (最初のタスクで作成したクラスを使用します)。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

4

2 に答える 2

1

You're dereferencing a null pointer on line 31. This is because you're initializing your array in a really weird way, with p1[c1++] = new slumbook_gamoranao();. Either initialize them all at once, or initialize them on demand, now you're just begging for problems.

于 2013-08-02T08:47:15.953 に答える