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

Thread: Null Exception Error & Help with PrintWriter

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    5
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Null Exception Error & Help with PrintWriter

    Hi all - First time poster. I've run into a little trouble with my Java assignment. The idea of the program is to grade exams.
    The program takes a text file that contains:
    - First line is the correct answers
    - Following lines contain student ID's separated by a " " and their answers to the exam.

    It grades each exam, calculates the score to the .00 place and the letter grade.

    The results should be written to an output file of the same name as the input, only with a '.out' extension in the format:
    "StudentID Grade LetterGRADE"

    My Problems:
    - I get a "Exception in thread "main" java.lang.NullPointerException at Grader.main(Grader.java:36)" upon completion of the program.
    - The Program works (as far as I can tell by the commented System.out.println on line 52) but it DOES NOT actually write anything to the answers.out file (it creates the file but it is empty)

    Here is the 'answers.txt' input file:
    DACABDBDABAEDEDBBEDEBCCBABCDDB
    885265785 DACABDBDABAEDEDBBEDEBCCCABCDDB
    576034603 DACABDBDABAEDEDBBEDEBCCBABCDDB
    702860517 EEEEEEEEEEEEEEEEEEEEEEEEEEEEEE
    765841670 DACABDBDABAEDEDBAEDEBCCBABCDDB
    024022503 DACABDBDABAEDEDBBEDEBCCBABCDDB
    204735425 ACABDBDABAEDEDBBEDEBCCBABCDDBD
    203521084 DACABDBDABAEDEDBBEDEBCCBABCDDB
    802426407 DACABDBDABAEDEDBBEDEBCCBABCDDB
    432176337 CCDCCDCCCCACCCDCCCCCCCCBCCCCCC
    180867781 DACABDBDABAEDEDBBEDEBCCBABCDDB
    027781302 DACABCBDABAEDEDBBEDDBCCBABCDDB
    438787273 DACABDBDABAEDEDBBEDEBCCBABCDDB
    831352188 DACABDBDABAEDEDBBEDEBCCBABCDDB
    360447237 DACABDBDABAEDEDBBEDEBCCBABCDDB
    316480278 AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
    151351806 DACABDBDABAEDEDBBEDEBCCBABCDDB
    447056164 DACABDBDABAEDEDBBEDEBCCBABCDDB
    527577044 DACABDBDABAEDEDBBEDEBCCBABCDDB
    072704112 DACABDBDCBAEDEDBEEDEBCCBBBCDDB
    353334086 DACABDBDABAEDEDBBEDEBCCBABCDDB
    642777412 DDDDDDDDDDDDDDDDDDDDDDDDDDDDDD
    040217240 DACABDBDABAEDEDBBEDEBCCBABCDDB
    843525013 DACABBBDAAAEDEDBBEDCCCCBABCDDB
    183524468 DACABDBDABAEDEDBBEDEBCCBABCDDB
    042176072 BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB
    108523756 DACABDBDABAEDEDBBEDEBCCBABCDDB
    567838552 DACABDBDABAEDEDBBEDEBCCBABCDDB
    360447238 DADABDBDAAAEDEDCBEDEBCCBDBCDDB
    360447240 AACABDCDABAEDEDBBADEBCCCABCDDB
    And my code so far:
    import java.io.*;
    import javax.swing.JOptionPane;
     
    public class Grader
    {
    	public static void main ( String[] args )
    	{
    		String line,
    			   file;
    		char[] key,
    				answers;
    		char letter;
    		String[] fields;
    		String ans,
    				id;
    		int i = 0,
    			count = 0;
    		double grade,
    			   correct=0;
     
    		file =	JOptionPane.showInputDialog(null, "Please enter the name of your '.txt' answers file in the correct format");
    		try
    		{
    			BufferedReader br = new BufferedReader (new FileReader(file + ".txt"));
    			PrintWriter pw = new PrintWriter( new FileWriter(file +".out"));
    			line = br.readLine();
    			key = line.toCharArray();
    			while (line != null)
    			{
    				line = br.readLine();
    				fields = line.split(" ");
    				id = fields[0];
    				ans = fields[1];
    				answers = ans.toCharArray();
    				while (count < answers.length)
    				{
    					if (answers[i] == key[i])
    					{
    						correct++;
    					}
    					count++;
    					i++;
    				}
    				grade = 100*(correct/30);
    				letter = (char)letterGrade(grade);
    				//System.out.println(id + " " + String.format( "%1$.2f", grade) + " " + letter + "\n");
    				pw.println(id + " " + String.format( "%1$.2f", grade) + " " + letter + "\n");
    				count = 0;
    				correct = 0;
    				i = 0;
    			}
    			br.close();
    			pw.close();
    		}
     
    		catch( IOException e)
    		{
    			System.out.println("File ERROR");
    		}
     
    	/*	try
    		{
    			FileWriter fw = new FileWriter ("answers.out");
    			PrintWriter pw = new PrintWriter (fw);
    		}
    		catch( IOException e)
    		{
    			System.out.println("ERROR");
    		}*/
    	}
     
    	public static char letterGrade (double grade)
    	{
    		char a = 'A',
    			 b = 'B',
    			 c = 'C',
    			 d = 'D',
    			 f = 'F';
     
    		if (grade >= 90)
    			{return a;}
    		else if (grade >= 80 && grade < 90)
    			{return b;}
    		else if (grade >= 70 && grade < 80)
    			{return c;}
    		else if (grade >= 60 && grade < 70)
    			{return d;}
    		else
    			{return f;}
     
    	}
    }

    Any help appreciated, Thank you!


  2. #2
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Null Exception Error & Help with PrintWriter

    What is Line 36 according to your Compiler?
    State that line or trace yourself, you are passing or comparing null to something. Backtrack to see, how and from where this null is appearing.

  3. The Following User Says Thank You to Mr.777 For This Useful Post:

    willo (November 29th, 2011)

  4. #3
    Junior Member
    Join Date
    Nov 2011
    Posts
    5
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Null Exception Error & Help with PrintWriter

    #36 to me is :
    				fields = line.split(" ");

  5. #4
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Null Exception Error & Help with PrintWriter

    Quote Originally Posted by willo View Post
    #36 to me is :
    				fields = line.split(" ");
    What if line="" ?

  6. The Following User Says Thank You to Mr.777 For This Useful Post:

    willo (November 29th, 2011)

  7. #5
    Junior Member
    Join Date
    Nov 2011
    Posts
    5
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Null Exception Error & Help with PrintWriter

    Sorry, I don't follow what your asking..

  8. #6
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Null Exception Error & Help with PrintWriter

    Print the line right above the line number 36.
    System.out.println(line);
    field=line.split(" ");

  9. The Following User Says Thank You to Mr.777 For This Useful Post:

    willo (November 29th, 2011)

  10. #7
    Junior Member
    Join Date
    Nov 2011
    Posts
    5
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Null Exception Error & Help with PrintWriter

    It spams the 1st line, the 'key', until you crash the program

  11. #8
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Null Exception Error & Help with PrintWriter

    It crashes coz there is null in the line variable (local).
    And when it tries to split it, it crashes coz null can not be split down.
    You will ofcourse ask i have a check line!=null. It will never be unless you have nothing to read from the file and everytime you read inside the loop so as soon as null occurs, it is inside the loop and you expect it not enter loop. It will not enter loop the next time. But this time it will execute and this line
    field = line.split(" ");
    will throw an exception as line = null


    I hope you got this now.

  12. The Following User Says Thank You to Mr.777 For This Useful Post:

    willo (November 29th, 2011)

  13. #9
    Junior Member
    Join Date
    Nov 2011
    Posts
    5
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Null Exception Error & Help with PrintWriter

    EDIT: Ha, I'm dumb. I got it, Thanks for pointing me in the right direction.
    Last edited by willo; November 29th, 2011 at 10:22 AM.

Similar Threads

  1. Null Pointer Exception error
    By Keitho55 in forum Exceptions
    Replies: 3
    Last Post: November 18th, 2011, 02:29 AM
  2. NULL POINTER EXCEPTION error
    By beefwithrice in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 28th, 2011, 06:26 AM
  3. [SOLVED] Error: Null Exception on Array of classes
    By g000we in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 15th, 2010, 08:14 AM
  4. JComboBox null pointer Exception error
    By F_Arches in forum AWT / Java Swing
    Replies: 2
    Last Post: November 29th, 2009, 01:32 PM
  5. Getting Null Pointer Exception in runtime
    By RoadRunner in forum Exceptions
    Replies: 1
    Last Post: April 26th, 2009, 01:21 PM