Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: can someone help with this error?

  1. #1
    Junior Member
    Join Date
    Feb 2014
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default can someone help with this error?

    Hi I'm a beginner in java. I have some problem with my code.
    can someone help me?

    import java.util.Scanner;
     
     
    public class HW11of2 { 
     
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		Scanner console = new Scanner(System.in); 
    		student[] students=new student[4];
    		for(int i=0;i<students.length;i++){
     
    			System.out.println("Please enter the name: ");  
    	        students[i].name = console.nextLine();  
    			System.out.println("Please enter a number for the id: ");  
    	        students[i].id = console.nextInt();  
    	        int sum=0;
    	        for(int j=0;j<5;j++){
    	        	System.out.println("Please enter the grade: ");  
    		        students[i].grades[j] = console.nextInt();
    		        sum=sum+ students[i].grades[j];
    	        }
    	        students[i].avg=sum/5;
    		}
    		int avgtotal=0;
    		 for(int i=0;i<students.length;i++){
    			 avgtotal=avgtotal + students[i].grades[2];
    		 }
    		 System.out.println(avgtotal);
    		 int min=min(students[0].grades);
    		 for(int i=1;i<students.length;i++){
    			 int temp=min(students[i].grades);
    			 if(temp<min){
    				 min=temp;
    			 }
    		 }
    		 int max=max(students[0].grades);
    		 for(int i=1;i<students.length;i++){
    			 int temp=max(students[i].grades);
    			 if(temp>max){
    				 max=temp;
    			 }
    		 }
            console.close();
    	}
     
    	public static int min(int[] x){
    		int min=x[0];
    		for (int i=0; i<x.length;i++)
    		{
    			if (x[i]<min)
    			{
    				min=x[i];
    			}
    		}
    		return min;
    	}
    	public static int max(int[] x){
    		int max=x[0];
    		for (int i=0; i<x.length;i++)
    		{
    			if (x[i]>max)
    			{
    				max=x[i];
    			}
    		}
    		return max;
    	}
    	public class student
    	{
    		int id;
    		String name;
    		int[] grades=new int[5];
    		int avg;
    	}
    }

    and the error I get is:
    Exception in thread "main" java.lang.NullPointerException
    	at HW11of2.main(HW11of2.java:13)

    Thanks!


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: can someone help with this error?

    Welcome to the forum! Thanks for taking the time to learn to post your code correctly and ask a complete question.

    In Java, class names begin with capital letters, even inner classes.

    This line:

    student[] students = new student[4];

    creates an array that is large enough to hold 4 Student objects, but it doesn't add create or add any student objects to the array.

    So in this and similar subsequent lines:

    students[i].name = console.nextLine();

    the reference to students[i] is null. Each element of students[] must first be filled with a Student instance, as in:

    students[i] = new Student();

    Hope this helps.

  3. #3
    Junior Member
    Join Date
    Feb 2014
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: can someone help with this error?

    Thanks!
    that helped and also I've forgot to put static where the class of students.

Similar Threads

  1. CheckBox/Spinner error Error
    By vandy22 in forum Android Development
    Replies: 1
    Last Post: February 15th, 2014, 04:05 AM
  2. Replies: 3
    Last Post: November 30th, 2013, 05:52 PM
  3. Problems with A* Map Search - GC Overload Error and Null Error
    By puneeth.meruva in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 18th, 2013, 03:01 PM
  4. Replies: 4
    Last Post: October 15th, 2013, 04:33 AM
  5. Please! Help me to this error "ERROR CANNOT FIND SYMBOL"
    By mharck in forum Object Oriented Programming
    Replies: 8
    Last Post: July 3rd, 2012, 09:20 AM