Abdul Gamoranao
Abdul Gamoranao

Reputation: 7

Basic Class and driver program [Java].. T.T i'm stack

This is our task. please run the code for you to figure it out the problem..

Task 1. Slumbook_.java Create a class that contains a slumbook entry. Your slumbook entry should have the following specifications: - a minimum of (5) attributes typically found in a Slumbook. - atleast (2) constructors - provide the necessary accessor and mutator methods for all the attributes you listed - atleast (1) helper method Additional constraints Do not use the following as attributes anymore because they are too common: first name, middle name, last name, age, and address.

Task 2. SlumbookDemo_.java Create a class SlumbookDemo that can contain a minimum of 10 and a maximum of 20 entries of your Slumbook entry objects (use the class you created in the first task). Your slumbook demo/driver program should provide the following methods for the slumbook. - Add entry - Delete entry - View all entries - Update an entry - Quit/Exit the Program the above is our machine problem.. I create this but I stacked,,

Here's my class:

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;
}

}

and here's my driver:

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);

    }  
}

.. The problem is, after I run, the menu will prompt then after I select 1, this error

Exception in thread "main" java.lang.NullPointerException at  slumbookdemo_gamoranao.main(slumbookdemo_gamoranao.java:31)

but after I run, it will show a only one info..

Sorry for bad english, T.T

Upvotes: 0

Views: 148

Answers (2)

user2553971
user2553971

Reputation: 16

when you do p1[c1++] = new slumbook_gamoranao(); (in the loop), you have p1[1] = something and c1=2.

so when you call p1[c1].setFn(..); you call it on null.

A quick and dirty solution is to set c1 = -1;, remove line 9 (p1[c1++] = new slumbook_gamoranao();) and loop with p1[++c1] = new slumbook_gamoranao();

Upvotes: 0

Kayaman
Kayaman

Reputation: 73578

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.

Upvotes: 1

Related Questions