Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java

Null pointer Exception!!!

Hey guys I am getting a Null pointer Exception in my program can someone please explain to me what exactly that is.. and how do i resolve it??

The Code:

The error has occurred in line 31 on Prompter class and line 13 in Example class

public class Example {

    public static void main(String[] args) {
        // Your amazing code goes here...
      Student[] studentArray;
      studentArray = new Student[10];
      Prompter prompter = new Prompter();
      int n;

      prompter.askNumber();

      for(int i=0;i<prompter.getNumber();i++){
        prompter.getAllNames(studentArray[i]);
      }

      for(int j=0;j<prompter.getNumber();j++){
        prompter.display(studentArray[j]);
      }

    }
}

public class Student{

  private String mName;
  private String mBranch;
  private int mSem;

  public void setName(String name){
    mName = name;
  }

  public void setBranch(String branch){
    mBranch = branch;
  }

  public void setSem(int sem){
    mSem = sem;
  }

  public String getName(){
    return mName;
  }

  public String getBranch(){
    return mBranch;
  }

  public int getSem(){
    return mSem;
  }
}

import java.util.Scanner;
public class Prompter{

  Scanner input = new Scanner(System.in);
  private int mNumber;

  public Prompter(){
    mNumber = 0;
  }

  public void setNumber(int num){
    mNumber = num;
  }

  public int getNumber(){
    return mNumber;
  }

  public void askNumber(){
    int n;
    System.out.println("Enter the number of Students");
    n = input.nextInt();
    setNumber(n);
  }

  public void getAllNames(Student stud){
    String str1 = null;
    String str2 =null;
    int sem;
    System.out.println("Enter the Student name");
    str1 = input.nextLine();
    stud.setName(str1);
    System.out.println("Enter the Student Branch");
    str2 = input.nextLine();
    stud.setBranch(str2);
    System.out.println("Enter the Student Semester");
    sem = input.nextInt();
    stud.setSem(sem);
  }

  public void display(Student stud){
    System.out.printf("%s\t\t%s\t\t%d\n",stud.getName(),stud.getBranch(),stud.getSem());
  }
}

3 Answers

Hey there, I'll give a hint, you are setting up student to be accepted as a parameter but unfortunately it's not set up anywhere as an object. So when you try to call things like .setName() on your student object, java is looking for a student object in your class and not finding any, so it comes back and shrugs at you.

Let me know if this doesn't help.

Hey Rob,

I didnt quite understand. I thought I was creating a Array of objects of the same class. I am using the prompter to accept the details by passing the objects through getAllNames() method.

Hey Abhishek,

Your array of objects is just an array holding your student objects. You are getting the null pointer exception because there is never a student object declared, though you try to call methods on it. So Java is noticing that you're trying to call methods on an object that hasn't been created yet. That is why it is giving you the null pointer exception.

the line stud.setName(str1);

Is throwing you your error because it can't find a student object.

A simple fix

Student stud = new Student();

would fix this.

There still needs to be the student object created to call methods on it, and to put into the array. You are correct in saying that you have an array ready to accept students. though.

Thank you Rob :)

I got that error out of the way..

But my Program isnt Accepting the name, it directly goes to accepting the branch name. The output also showing null and 0.

Is this cause of the new Objects Created??? I mean are they Re-initialized??

Hey Abhishek,

I looked into this, and I was getting odd feedback from the input checkers as well. I think it might be because your array of students is empty. It might be best to declare an arrayList and add the students to there. Maybe in the constructor of the student?

Even if you want to use an array, which is probably do able, we'll need to create a method where we set up the array that we're feeding in, because you're right now it's just taking blank information for student and not allowing it to be set.

My advice would be if possible, in your student class (Or another class on it's own) you might want to make methods that set up your array. Like for every student variable you create you add that spot in your array List.

So studentArray(0) = new Student();

except we set this up for the array size that we want it to be.

It would probably be better to have this declared before we pass in the array, and not try to create the array on the fly now that I think about it.

thats different function, I am using prompter.getNumber() it returns an int value right??

Why would it not work?

Sorry Abdhishek,

I wasn´t right.

askNumber sets the int in mNumber. And you get this int through getNumber. You are totally right!

Grigorij

Hi Adhishek,

this line looks wrong for me:

for(int i=0;i<prompter.getNumber();i++)

you use the getNumber method here that gives you the number of mNumber. But this number is 0 ...

public Prompter(){
    mNumber = 0;
  }

So in the for loop i is 0 and not < 0 and the condition is false. I think something wrong is with the user input.

Let us know if it works

Grigorij

Hey Girgorij,

The value of mNumber will be zero initially. But the actually value will be assigned to mNumber when prompter.AskNumber() is called.

At that instance I'm setting the value of mNumber depending on the user input.

I did try by replacing the code by a value 5. Still getting the same error.

Can store the prompter.askNumber() in a int variable like "numerAsked" and use it in the for loop?

for(int i=0;i<numberAsked;i++)

I think that the for loop can´t see the number from the call:

prompter.askNumber();

Grigorij