1

I would like to ask help for my project. I am a newbie programmer and I don't get this error "exception in thread main java.lang.nullpointerexception"

this is my code:

 public class Slumbook{

    public String codeName;

    public Slumbook(){
        //
    }

    public Slumbook(String codeName){
        this.codeName = codeName;
    }

    public String getCodeName(){return codeName;}

    public void setCodeName(String codeName){this.codeName = codeName;}
   }

And the Driver Program code is:-

import java.util.*;

public class SlumbookD{
    public static void main(String[] args){

        Scanner sc = new Scanner(System.in);
        Slumbook[] slum = new Slumbook[20];

        for(int n=0; n<10; n++){
            slum[n].setCodeName(sc.nextLine());
        }
    }
}
St. John Johnson
  • 6,590
  • 7
  • 35
  • 56
  • 2
    You create an array, but never intialize the each array slot. You should look into what a `NullPointerException` is, because this question gets asked ALL THE TIME. – Vince Dec 27 '14 at 05:10
  • 1
    By the time you get to the loop, `slum[n]` has not been instantiated; that is, it is a null reference/pointer. To fix, instantiate each element in `slum` before attempting to set its code name. – apnorton Dec 27 '14 at 05:12

3 Answers3

2

You allocated the array ... but you failed to allocate any objects contained in the array.

SUGGESTION:

import java.util.*;

public class SlumbookD { 

    public static void main(String[] args){
      Scanner sc = new Scanner(System.in);
      Slumbook[] slum = new Slumbook[20];
      for(int n=0; n<slum.length; n++){
          slum[n] = new Slumbook(sc.nextLine);
      }
    }

}

... OR BETTER ...

// You only need one class - with it's own main().  Not two classes...
public class Slumbook{

  public String codeName;

  public Slumbook(String codeName){
    this.codeName = codeName;
  }

  public String getCodeName(){
    return codeName;
  }

  public void setCodeName(String codeName){
    this.codeName = codeName;
  }

  public static void main(String[] args){
    Scanner sc = new Scanner(System.in);
    Slumbook[] slum = new Slumbook[20];
    for(int n=0; n<slum.length; n++){
        slum[n] = new Slumbook(sc.nextLine);
    }
  }

}

FoggyDay
  • 11,962
  • 4
  • 34
  • 48
1

You have created blank array but not initialize it, you have to associate object reference at your array index and then you can call any number of setter methods on that index.

 Slumbook[] slum = new Slumbook[20];

    for(int n=0; n<10; n++){
        slum[n] = new Slumbook(); //  Instantiate Slumbook class and assign reference
        slum[n].setCodeName(sc.nextLine());
    }
atish shimpi
  • 4,873
  • 2
  • 32
  • 50
0

You need to initialize the Array elements before using them.

Slumbook[] slum = new Slumbook[20];

for(int n=0; n<10; n++){
    slum[n] = new Slumbook();
    slum[n].setCodeName(sc.nextLine());
}

You can initialize all the elements of array at once to avoid any future Exceptions

Slumbook[] slum = new Slumbook[20];

for(int i = 0 ; i < slum.length ; i++)  //initializing all the elements
    slum[i] = new Slumbook();

for(int n=0; n<10; n++){
    slum[n].setCodeName(sc.nextLine());
}
varun
  • 1,473
  • 1
  • 9
  • 15