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 11 of 11

Thread: keep getting main method error need some help =]

  1. #1
    Junior Member
    Join Date
    Mar 2014
    Posts
    26
    My Mood
    Inspired
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default keep getting main method error need some help =]

    i am trying to insert 5 students ids and a
    course number for each student id. Then it prints all
    5 student ids and the course name associated with
    each student id.

    but im having trouble with inserting the main method because i have no idea what to input.


    package assignment10;
     
    import java.util.Scanner;
     
    public class BannerWebArray 
    {
    	public static void main(String[] args)
    	{
     
    		Scanner scan=new Scanner(System.in);
    		int studentId,courseId,i;
    		String courseName;
    		BannerUser[] bannerUser=new BannerUser[5];
    		for (i=0;i<5;i++)
    			bannerUser[i]=new BannerUser();
     
    		for (i=0;i<5;i++)
    		{
    			System.out.print("Enter student id:");
    			studentId=scan.nextInt();
    			System.out.print("Enter course id: 1 for Math, 2 for English");
    			courseId=scan.nextInt();
     
    			bannerUser[i].setUserId(studentId);
    			courseName=bannerUser[i].register(courseId);
    			bannerUser[i].setCourseName(courseName);
     
    			System.out.println("Students registered for Math:");
    			for (i=0;i<5;i++)
    			{
     
    				courseName=bannerUser[i].getcourseName();
    				if (courseName.equals("Math"))
    					System.out.println(bannerUser[i].getUserId());
    			}
     
    			System.out.println("Student registered for English");
    			for (i=0;i<5;i++)
    			{
    				courseName=bannerUser[i].getcourseName();
    				if (courseName.equals("English"))
    					System.out.println(bannerUser[i].getUserId());
    			}
    		}
    	}
     
    }

    error message:
    Enter student id:12345
    Enter course id: 1 for Math, 2 for English2
    Students registered for Math:
    Exception in thread "main" java.lang.NullPointerException
    at assignment10.BannerWebArray.main(BannerWebArray.ja va:33)


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,384
    Thanks
    49
    Thanked 2,208 Times in 2,181 Posts

    Default Re: keep getting main method error need some help =]

    Exception in thread "main" java.lang.NullPointerException
    at assignment10.BannerWebArray.main(BannerWebArray.ja va:33)
    There is a null value on line 33 when it is executed. Look at line 33, find where the null value is and then backtrack in the code to see where it came from. If you can't see what variable has a null value, add a println() statement to print out the value of all the variables on line 33.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Mar 2014
    Posts
    26
    My Mood
    Inspired
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default Re: keep getting main method error need some help =]

    now it shows:
    Enter student id:1234
    Enter course id: 1 for Math, 2 for English2
    Students registered for Math:
    Exception in thread "main" java.lang.NullPointerException
    at bannerwabarray.BannerWebArray.main(BannerWebArray. java:32)
    line 31 to 34:
    package bannerwabarray;
     
    import java.util.Scanner;
     
    public class BannerWebArray {
    	public static void main(String[] args)
    	{
     
    		Scanner scan=new Scanner(System.in);
    		int studentId,courseId,i;
    		String courseName;
    		BannerUser[] bannerUser=new BannerUser[5];
    		for (i=0;i<5;i++)
    			bannerUser[i]=new BannerUser();
     
    		for (i=0;i<5;i++)
    		{
    			System.out.print("Enter student id:");
    			studentId=scan.nextInt();
    			System.out.print("Enter course id: 1 for Math, 2 for English");
    			courseId=scan.nextInt();
     
    			bannerUser[i].setUserId(studentId);
    			courseName=bannerUser[i].register(courseId);
    			bannerUser[i].setCourseName(courseName);
     
    			System.out.println("Students registered for Math:");
    			for (i=0;i<5;i++)
    			{
     
    				courseName=bannerUser[i].getcourseName();
    				if (courseName.equals("Math"))
    					System.out.println(bannerUser[i].getUserId());
    			}
     
    			System.out.println("Student registered for English");
    			for (i=0;i<5;i++)
    			{
    				courseName=bannerUser[i].getcourseName();
    				if (courseName.equals("English"))
    					System.out.println(bannerUser[i].getUserId());
    			}
    		}
    	}
     
    }

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,384
    Thanks
    49
    Thanked 2,208 Times in 2,181 Posts

    Default Re: keep getting main method error need some help =]

    Exception in thread "main" java.lang.NullPointerException
    at bannerwabarray.BannerWebArray.main(BannerWebArray. java:32)
    There is a null value on line 32 when it is executed. Look at line 32, find where the null value is and then backtrack in the code to see where it came from. If you can't see what variable has a null value, add a println() statement to print out the value of all the variables on line 32.

    What statement is on line 32?
    What variable on that line has a null value? Did you print it?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Mar 2014
    Posts
    26
    My Mood
    Inspired
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default Re: keep getting main method error need some help =]

    i seriously dont see it . im so lost.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,384
    Thanks
    49
    Thanked 2,208 Times in 2,181 Posts

    Default Re: keep getting main method error need some help =]

    Can you find line 32? And post it here?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Mar 2014
    Posts
    26
    My Mood
    Inspired
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default Re: keep getting main method error need some help =]

    if (courseName.equals("Math"))

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,384
    Thanks
    49
    Thanked 2,208 Times in 2,181 Posts

    Default Re: keep getting main method error need some help =]

    What is the value of courseName? Add a println() just before line 32 to print out its value.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Mar 2014
    Posts
    26
    My Mood
    Inspired
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default Re: keep getting main method error need some help =]

    Enter student id:1234
    Enter course id: 1 for Math, 2 for English1
    Students registered for Math:
    null
    Exception in thread "main" java.lang.NullPointerException
    at bannerwabarray.BannerWebArray.main(BannerWebArray. java:33)


    so math has the null right?

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,384
    Thanks
    49
    Thanked 2,208 Times in 2,181 Posts

    Default Re: keep getting main method error need some help =]

    so math has the null right?
    What was printed?
    Was that null from printing courseName?
    What was the value of i when the null was found? Add the printing of i to what is printed.

    Does the getCourseName() method return null?

    Where does the BannerUser object get values for its contents?
    Does the code in the BannerUser class make sure it has a full set of values for all its variables?
    If you don't understand my answer, don't ignore it, ask a question.

  11. The Following User Says Thank You to Norm For This Useful Post:

    ATB (April 26th, 2014)

  12. #11
    Junior Member
    Join Date
    Mar 2014
    Posts
    26
    My Mood
    Inspired
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default Re: keep getting main method error need some help =]

    my computer glitched and all my work got deleted so i had to redo other assigned problems. will be back as soon as i catch back up

Similar Threads

  1. Main Method Error
    By KMCD00 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: July 16th, 2013, 01:55 AM
  2. Error: Main method not found! Please help!
    By adnan.alvee in forum What's Wrong With My Code?
    Replies: 12
    Last Post: March 11th, 2013, 07:03 PM
  3. Replies: 3
    Last Post: October 31st, 2011, 12:42 AM
  4. How do I call a method from the main method?
    By JavaStudent1988 in forum Java Theory & Questions
    Replies: 5
    Last Post: October 19th, 2011, 08:37 PM
  5. Replies: 6
    Last Post: November 12th, 2010, 03:40 AM

Tags for this Thread