   Java
  i am trying to make a program that takes student names and scores for tests then prints them out.problem is, i am getting a nullpointerexception whenever i type in the students name. Anyway. here is the code:import java.lang.String;import java.io.*;public class Main extends java.lang.Object{    protected class student    {   private String name;   private int score; public String getName()   {  return name;   }   public int getScore()   {  return score;   }    };    public static void main(String[] args) throws IOException    {   BufferedReader console = new BufferedReader(new InputStreamReader(System.in)); System.out.println("How many students took part in the test or assignment?: ");   int num = Integer.parseInt(console.readLine()); student[] pupil = new student[num]; boolean enteringData = true;   while(enteringData)   {for(int i = 0; i <= num; i++)  { System.out.println("\n\nWhat is the students name?: "); pupil.name = console.readLine();   System.out.println("\nAnd what was the score?: "); pupil.score = Integer.parseInt(console.readLine());  }  System.out.println("Printing results...");  for(int i = 0; i <= num; i++)  { System.out.println("\n\n" + pupil.getName()); System.out.println("\n" + pupil.getScore());  }  System.out.println("Is this correct (y/n)");  String answer = console.readLine();if(answer.equals("y"))  { enteringData = false;  }  else if(answer.equals("n"))  {  }  else  { System.out.println("You really are stupid aren't you?!");  }   }   //printing code will go here    }}pls help if you can
  Posting the stack trace is always a good idea, since that tells you the exact line where things went wrong.Your problem is here:  student[] pupil = new student[num];New'ing an array only creates the array, pupil[0], pupil[1] etc. are all still null pointers. You need something like this:for (int i=0; i<pupil.length; i++)  pupil = new student();Or better yet, use your existing loop and pass all the pupil data into it in the constructor. You should also read the Java style Guide - the convention is that classes start with a capital (ie. Student instead of student).   ;  Please use the source tag:import java.lang.String;import java.io.*;public class Main extends java.lang.Object{  protected class student  {    private String name;    private int score;    public String getName()    { return name;    }    public int getScore()    { return score;    }  }  public static void main(String[] args) throws IOException  {    BufferedReader console = new BufferedReader(new InputStreamReader(System.in));    System.out.println("How many students took part in the test or assignment?: ");    int num = Integer.parseInt(console.readLine());    student[] pupil = new student[num];    boolean enteringData = true;    while(enteringData)    { for(int i = 0; i <= num; i++) {   System.out.println("\n\nWhat is the students name?: ");   pupil.name = console.readLine();   System.out.println("\nAnd what was the score?: ");   pupil.score = Integer.parseInt(console.readLine()); } System.out.println("Printing results..."); for(int i = 0; i <= num; i++) {   System.out.println("\n\n" + pupil.getName());   System.out.println("\n" + pupil.getScore()); } System.out.println("Is this correct (y&#47;n)"); String answer = console.readLine(); if(answer.equals("y")) {   enteringData = false; } else if(answer.equals("n")) { } else {   System.out.println("You really are stupid aren't you?!"); }    }    //printing code will go here  }};  what is the source tag?   ;In the FAQ ;-)   ;  [source]your code here[/source]See the FAQ;  does this work?import java.lang.String;import java.io.*;public class Main extends java.lang.Object{    protected class Student    {   private String name;   private int score; public String getName()   {  return name;   }   public int getScore()   {  return score;   }    };    public static void main(String[] args) throws IOException    {   BufferedReader console = new BufferedReader(new InputStreamReader(System.in)); System.out.println("How many students took part in the test or assignment?: ");   int num = Integer.parseInt(console.readLine()); Student[] pupil = new Student[num];for(int i = 0; i <= pupil.length; i++)   {  pupil = new Student();   } boolean enteringData = true;   while(enteringData)   {for(int i = 0; i <= num; i++)  { System.out.println("\n\nWhat is the students name?: "); pupil.name = console.readLine();   System.out.println("\nAnd what was the score?: "); pupil.score = Integer.parseInt(console.readLine());  }  System.out.println("Printing results...");  for(int i = 0; i <= num; i++)  { System.out.println("\n\n" + pupil.getName()); System.out.println("\n" + pupil.getScore());  }  System.out.println("Is this correct (y/n)");  String answer = console.readLine();if(answer.equals("y"))  { enteringData = false;  }  else if(answer.equals("n"))  {  }  else  { System.out.println("You really are stupid aren't you?!");  }   }   //printing code will go here    }}problem is, now it says"non-static variablethis cannot be referenced from a static context"this comes up infor(int i = 0; i <= pupil.length; i++)   {  pupil = new Student(); //here is the problem   };  does this work?[source lang = "java"]import java.lang.String;import java.io.*;public class Main extends java.lang.Object{    protected class Student    {   private String name;   private int score; public String getName()   {  return name;   }   public int getScore()   {  return score;   }    };    public static void main(String[] args) throws IOException    {   BufferedReader console = new BufferedReader(new InputStreamReader(System.in)); System.out.println("How many students took part in the test or assignment?: ");   int num = Integer.parseInt(console.readLine()); Student[] pupil = new Student[num];for(int i = 0; i &lt;= pupil.length; i++)   {  pupil = new Student();   } boolean enteringData = true;   while(enteringData)   {for(int i = 0; i &lt;= num; i++)  { System.out.println("\n\nWhat is the students name?: "); pupil.name = console.readLine();   System.out.println("\nAnd what was the score?: "); pupil.score = Integer.parseInt(console.readLine());  }  System.out.println("Printing results...");  for(int i = 0; i &lt;= num; i++)  { System.out.println("\n\n" + pupil.getName()); System.out.println("\n" + pupil.getScore());  }  System.out.println("Is this correct (y/n)");  String answer = console.readLine();if(answer.equals("y"))  { enteringData = false;  }  else if(answer.equals("n"))  {  }  else  { System.out.println("You really are stupid aren't you?!");  }   }   //printing code will go here    }}problem is, now it says"non-static variablethis cannot be referenced from a static context"this comes up infor(int i = 0; i <= pupil.length; i++)   {  pupil = new Student(); //here is the problem   }sorry. i know this must sound pretty stupid;  Your student class should be marked static, since it's an inner class of the Main class. Non-static inner classes can access the member variables of the outer class, and thus non-static Student needs an instance of Main to make sense. Just mark it static.   ;  i got it!!!!thanks heaps guysfinal code is:import java.lang.String;import java.io.*;public class Main extends java.lang.Object{    protected static class Student    {   private String name;   private int score; public String getName()   {  return name;   }   public int getScore()   {  return score;   }    };    public static void main(String[] args) throws IOException    {   BufferedReader console = new BufferedReader(new InputStreamReader(System.in)); System.out.println("How many students took part in the test or assignment?: ");   int num = Integer.parseInt(console.readLine()); Student[] pupil = new Student[num];for(int i = 0; i < pupil.length; i++)   {  pupil = new Student();   } boolean enteringData = true;   while(enteringData)   {for(int i = 0; i < num; i++)  { System.out.println("\n\nWhat is the students name?: "); pupil.name = console.readLine();   System.out.println("\nAnd what was the score?: "); pupil.score = Integer.parseInt(console.readLine());  }  System.out.println("Printing results...");  for(int i = 0; i < num; i++)  { System.out.println("\n\n" + pupil.getName()); System.out.println("\n" + pupil.getScore());  }  System.out.println("Is this correct (y/n)");  String answer = console.readLine();if(answer.equals("y"))  { enteringData = false;  }  else if(answer.equals("n"))  {  }  else  { System.out.println("You really are stupid aren't you?!");  }   }   //printing code will go here    }}