5 Replies - 327 Views - Last Post: 21 January 2013 - 04:30 AM Rate Topic: -----

#1 ShadwClone  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 19-January 13

Exception in thread "main" java.lang.NullPointerException

Posted 19 January 2013 - 07:08 PM

Hello, I am working on a code that reads a file of data then outputs specific information pertaining to that file.

I have no problems with compiling my program as it is, but I keep getting this runtime error
Exception in thread "main" java.lang.NullPointerException
	at Section.nPassing(Hw05.java:86)
	at Hw05.main(Hw05.java:28)



The output I am striving for is:
Enter name of a file of Students: Student3

In forward order:
Kevin (53)
Susan (82)
Nguyen (75)

That Section contains 3 Student(s).

That Section contains 2 passing Student(s).

The highest grade is 82.



My code thus far is as follows:
import java.io.*;
import java.util.Scanner;
/////////////////////////////////////////////////////////////////////////
class Hw05
{
//-----------------------------------------------------------------------
   public static void main (String [] args) throws Exception
   {
      Scanner kb = new Scanner(System.in);

      System.out.print("\nEnter name of a file of Students: ");
      String filename = kb.nextLine();

      Section sec = new Section(filename);

      System.out.println("\nIn forward order:");
      sec.print(System.out);

      System.out.println("\nThat Section contains " +
                         sec.howMany() +
                         " Student(s).");

      System.out.println("\nThat Section contains " +
                         sec.nPassing() +
                         " passing Student(s).");

      System.out.println("\nThe highest grade is " +
                         sec.highestGrade() + ".");
   } 
//-----------------------------------------------------------------------
} // end class Hw05
/////////////////////////////////////////////////////////////////////////
class Section
{
   private Student [] a;
   private int used;
   private static final int INIT_SIZE = 20;
//-----------------------------------------------------------------------
   public Section ( String filename ) throws Exception
   {
      Scanner sc = new Scanner(new File(filename));

      a = new Student[INIT_SIZE];
      used = 0;

      while ( sc.hasNext() )
      {
         Student s = Student.read(null,sc);

         if ( used == a.length )
         {
            Student[] newA = new Student[2*a.length+1];
            for ( int i = 0 ; i < used ; i++ ) newA[i] = a[i];
            newA[used] = s;
            used++;
            a = newA;
         }
         else
         {
            a[used] = s;
            used++;
         }
      }
   }
//-----------------------------------------------------------------------
   public void print ( PrintStream ps )
   {
      for ( int i = 0 ; i < used ; i++ )
      {
         ps.println(a[i]);
      }
   }
//-----------------------------------------------------------------------
   public int howMany() { return used; }
//-----------------------------------------------------------------------
	public int nPassing()
	{
		int passing = 0;
		for ( int i = 0; i < a.length; i++ )
		{
			if ( a[i].getGrade() >= 60 ) 
				passing++;
		}
		return passing;
	}
//-----------------------------------------------------------------------
	public int highestGrade()
	{
		int highest = a[0].getGrade();
		if ( used == 0 )
			throw new Error
				("Attempt to find the maximum of an empty Section");
		else
		{
			
			for ( int x = 0; x < a.length; x++ )
			{
				if ( a[x].getGrade() > highest )
					highest = a[x].getGrade();
			}
		}
		return highest;
	}
} // end class Section
/////////////////////////////////////////////////////////////////////////
class Student
{
   private String name;
   private int grade;
//-----------------------------------------------------------------------
   public Student ( String name, int grade )
   {
      this.name = name;
      this.grade = grade;
   }
//-----------------------------------------------------------------------
   public String toString ()
   {
      return name + " (" + grade + ")";
   }
//-----------------------------------------------------------------------
   public String getName() { return name; }
//-----------------------------------------------------------------------
   public int getGrade() { return grade; }
//-----------------------------------------------------------------------
   public void setGrade( int newGrade ) { grade = newGrade; }
//-----------------------------------------------------------------------
   public static Student read ( PrintStream ps, Scanner sc ) 
   {
      if ( ps != null ) ps.println("Reading a Student record ...");
      if ( ps != null ) ps.print("Enter the name: ");
      String name = sc.nextLine();
      if ( ps != null ) ps.print("Enter the grade: ");
      int grade = sc.nextInt(); sc.nextLine();
      return new Student(name,grade);
   }
//-----------------------------------------------------------------------
} // end class Student
/////////////////////////////////////////////////////////////////////////



Is This A Good Question/Topic? 0
  • +

Replies To: Exception in thread "main" java.lang.NullPointerException

#2 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3175
  • View blog
  • Posts: 10,629
  • Joined: 12-December 12

Re: Exception in thread "main" java.lang.NullPointerException

Posted 19 January 2013 - 07:27 PM

Do the line numbers 86 and 28 correspond to the same line numbers in your posted code?
Was This Post Helpful? 0
  • +
  • -

#3 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1370
  • View blog
  • Posts: 3,021
  • Joined: 05-April 11

Re: Exception in thread "main" java.lang.NullPointerException

Posted 19 January 2013 - 07:27 PM

The line numbers you posted does not match the code
Do you mind telling us what lines are causing the problems?

Anyway. You are increasing the size of your array with 2*a.length+1 which is fine, but you do not take into account that some indexes in your array may be null
What if you run into a place in the array which is null in the loop below? You do not take this into consideration
for ( int x = 0; x < a.length; x++ )
{
    if ( a[x].getGrade() > highest )
        highest = a[x].getGrade();
}


Was This Post Helpful? 0
  • +
  • -

#4 ShadwClone  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 19-January 13

Re: Exception in thread "main" java.lang.NullPointerException

Posted 21 January 2013 - 01:43 AM

Oh, because the first few lines of the code is information that I have for an assignment.. such as my name and class number, etc.

In reality, line 86 is
 if ( a[i].getGrade() >= 60 )  


and line 28 is
  System.out.println("\nThat Section contains " +
                         sec.nPassing() +
                         " passing Student(s)."); 

Was This Post Helpful? 0
  • +
  • -

#5 ShadwClone  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 19-January 13

Re: Exception in thread "main" java.lang.NullPointerException

Posted 21 January 2013 - 01:58 AM

View PostCasiOo, on 19 January 2013 - 07:27 PM, said:

Anyway. You are increasing the size of your array with 2*a.length+1 which is fine, but you do not take into account that some indexes in your array may be null
What if you run into a place in the array which is null in the loop below? You do not take this into consideration
for ( int x = 0; x < a.length; x++ )
{
    if ( a[x].getGrade() > highest )
        highest = a[x].getGrade();
}



Wouldn't that problem automatically be solved by the x < a.length feature?
I mean, if length is 3, for example, then wouldn't the counter go 0, 1, 2, then stop and move on?
Was This Post Helpful? 0
  • +
  • -

#6 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2197
  • View blog
  • Posts: 5,224
  • Joined: 10-September 10

Re: Exception in thread "main" java.lang.NullPointerException

Posted 21 January 2013 - 04:30 AM

The Arrays length property returns the number of elements the array CAN hold, not the number of non-null elements currently in the array. In your case, the Student[] array, a[], was defined to hold 20 Student objects, all of which (20) will initially be set to null. Then, if you only populate the array a[] with 3 Student objects,

a[0] = new Student();
a[1] = new Student();
a[2] = new Student();

a[3] through a[19] will still be null, and the loop

for ( int i = 0 ; i < a.length ; i++ )

will iterate through all 20 elements, including the ones that are null.

The ArrayList.size() method returns the number of objects actually in the ArrayList. You could achieve a similar behavior using arrays by keeping a count of the number of students in the array and using that count as the control variable when iterating the array.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1