Help me in Java!

embargiel

Newcomer
Ok, this is the partial of my code:

Code:
   import javax.swing.JOptionPane;
   import java.io.*;

           
   public class Problem
           
   {
      private static final int START=0, MAXIMUMSTUDENTS=25;
      private static int getOption,numOfStudents;
      private static Student students[];
              
      public static void main(String[] args)                           
              
      {
         Student[] students = new Student[MAXIMUMSTUDENTS];
         numOfStudents=0;
         insert();  
      }
   
              
      public static void insert() 
              
      {
         int x,ID,age;
         double GPA;
         char gender;
         String name;
         ID = Integer.parseInt(JOptionPane.showInputDialog("Enter the studentID: "));
         x = ID;
      
         name = JOptionPane.showInputDialog("Name: ");
         gender = JOptionPane.showInputDialog("Gender: ").charAt(0);
         age = Integer.parseInt(JOptionPane.showInputDialog("Age: "));
         GPA = Double.parseDouble(JOptionPane.showInputDialog("GPA: "));
                     students[numOfStudents] = new Student(name, gender,age, ID, GPA);
      
         numOfStudents++;
      
      }
   }

At the main function, I've declared the student array. When I actually try to initialize that object (as an element of an array), it gives me the nullpointer exception. Can anybody help me?

This is the error message:
Exception in thread "main" java.lang.NullPointerException
at Problem.insert(Problem.java:36)
at Problem.main(Problem.java:18)

Just to add something, student class works just fine and the program compiles gracefully.
 
embargiel said:
Ok, this is the partial of my code:

Code:
   import javax.swing.JOptionPane;
   import java.io.*;

           
   public class Problem
           
   {
      private static final int START=0, MAXIMUMSTUDENTS=25;
      private static int getOption,numOfStudents;
      private static Student students[];
              
      public static void main(String[] args)                           
              
      {
         Student[] students = new Student[MAXIMUMSTUDENTS];
         numOfStudents=0;
         insert();  
      }
   
              
      public static void insert() 
              
      {
         int x,ID,age;
         double GPA;
         char gender;
         String name;
         ID = Integer.parseInt(JOptionPane.showInputDialog("Enter the studentID: "));
         x = ID;
      
         name = JOptionPane.showInputDialog("Name: ");
         gender = JOptionPane.showInputDialog("Gender: ").charAt(0);
         age = Integer.parseInt(JOptionPane.showInputDialog("Age: "));
         GPA = Double.parseDouble(JOptionPane.showInputDialog("GPA: "));
                     students[numOfStudents] = new Student(name, gender,age, ID, GPA);
      
         numOfStudents++;
      
      }
   }

At the main function, I've declared the student array. When I actually try to initialize that object (as an element of an array), it gives me the nullpointer exception. Can anybody help me?

This is the error message:
Exception in thread "main" java.lang.NullPointerException
at Problem.insert(Problem.java:36)
at Problem.main(Problem.java:18)

Just to add something, student class works just fine and the program compiles gracefully.
I don't know a thing about Java, but something in your code looks suspicious...

In main(), you define students:
Code:
         Student[] students = new Student[MAXIMUMSTUDENTS];
So that allocates 25 (MAXIMUMSTUDENTS) Students and make students point to that allocated memory.
Then in Insert():
Code:
                     students[numOfStudents] = new Student(name, gender,age, ID, GPA);
Looks like you're creating a new chunk of memory.

The way I would handle this is to make "student" an array of pointers to Student. Then when you create new chunks of memory, you just assign the address to the appropriate pointer in the array.

If I'm off base, then just ignore this message :)
 
This is where Java's different from C++: all objects are accessed by reference. So the array of "Student" are actually array of references to "Student". Thus, you need to "new" an object for every array elements.

I am not familiar with swing, so I don't know why your program throw such exception (which normally means you accessed something wrong, such as trying to reference a null, or using a null as an array).
 
Code:
   public class Problem
           
   {
      private static final int START=0, MAXIMUMSTUDENTS=25;
      private static int getOption,numOfStudents;
      private static Student students[];
              
      public static void main(String[] args)                           
              
      {
         Student[] students = new Student[MAXIMUMSTUDENTS];
I'd say you're redeclaring students here, overriding the class member with a local variable.
 
here:

private static Student students[];

you have create a static array but later on you tried to initialise with:-

Student[] students = new Student[MAXIMUMSTUDENTS];

here u declared a different object called 'students' which is not withing the scope of the call to the 'insert()' method.

this explains why a global reference to the (non-initialised) static array 'student' yields some null values.

try students = new Student[MAXIMUMSTUDENTS];

instead an give it a test run.

let me know if this helps.
 
Yeah, what notAFanB (EDIT: AND XMas) wrote is quite certanly the problem.

You could alternatively write

Code:
private static Student[] students = new Student[MAXIMUMSTUDENTS]

directly at the declaration of the static array - then you don't need the initialization in main().
 
Thanks Guys! You guys are awesome. My re-declaring the problem was really the problem. Now it works fine. Thanks a million.
 
Back
Top