Results 1 to 2 of 2
- 04-08-2008, 02:55 AM #1
Member
- Join Date
- Apr 2008
- Posts
- 1
- Rep Power
- 0
Errors driving me crazy! although compiles fine
I am working on a project for an online class - I am teaching myself really! My last assignment I cannot get to work. I had a friend who "knows" what he is doing help me. Well that didn't work out too well. I really am trying but, there really is no teacher with this online class. I can't get questions answered in time and stuff goes past due. I am getting this error:
Exception in thread "main" java.lang.NullPointerException
at java.io.Reader.<init>(Reader.java:61)
at java.io.InputStreamReader.<init>(InputStreamReader .java:55)
at java.util.Scanner.<init>(Scanner.java:590)
at ttest.main(ttest.java:54)
----jGRASP wedge2: exit code for process is 1.
----jGRASP: operation complete.
-----------------------------------------------------------------------
This is my code:
import java.util.*;
import java.io.*;
public class ttest
{
static Scanner console = new Scanner(System.in);
public static void main(String[] args)throws IOException
{
FileInputStream fin = null; // input file reference
PrintStream floser = null; // output file references
PrintStream fwinner = null;
Scanner rs; // record scanner
Scanner ls; // line scanner
String inputrec; // full record buffer
int wins; // data read from each record
int losses;
double pctg;
String team;
String best = null; // track best/worst team(s)
String worst = null;
double worst_pctg = 2.0; // track best/worst pctgs
double best_pctg = -1.0;
int winner_count = 0; // counters for winning/losing records
int loser_count = 0;
// should check args.length and if not == 1 generate error
try
{
Scanner inFile = new Scanner(new FileReader("football.txt"));
}
catch( FileNotFoundException e )
{
System.exit( 1 );
}
try
{
floser = new PrintStream( new FileOutputStream( "loser.txt" ) );
fwinner = new PrintStream( new FileOutputStream( "winner.txt" ) );
}
catch( FileNotFoundException e )
{
System.out.printf( "unable to open an output file: %s\n", e.toString() );
System.exit( 1 );
}
try
{
rs = new Scanner( fin );
while( rs.hasNext( ) )
{
inputrec = rs.nextLine( ); /* read next line */
ls = new Scanner( inputrec ); /* prevents stumble if record has more than expected */
team = ls.next( );
wins = ls.nextInt();
losses = ls.nextInt();
if( wins + losses > 0 )
pctg = ((double) wins)/(wins + losses);
else
pctg = 0.0;
if( pctg > .5 )
{
if( pctg > best_pctg )
{
best_pctg = pctg;
best = team;
}
else
if( pctg == best_pctg )
{
best += ", " + team;
}
fwinner.printf( "%10s %2d %2d %5.3f\n", team, wins, losses, pctg );
winner_count++;
}
else
{
if( pctg < worst_pctg )
{
worst_pctg = pctg;
worst = team;
}
else
if( pctg == worst_pctg )
{
worst += ", " + team;
}
floser.printf( "%10s %2d %2d %5.3f\n", team, wins, losses, pctg );
loser_count++;
}
}
fin.close( );
floser.close( );
fwinner.close( );
}
catch( IOException e ) {
System.out.printf( "I/O error: %s\n", e.toString() );
System.exit( 1 );
}
System.out.printf( "%d teams have winning records; %d teams have losing records\n", winner_count, loser_count );
System.out.printf( "Team(s) with best percentage: %5.3f %s\n", best_pctg, best );
System.out.printf( "Team(s) with worst percentage: %5.3f %s\n", worst_pctg, worst );
}
}
---------------------------------------------------------------
The assignment is:
Create a Java program to read in an unknown number of lines from a data file. You will need to create the data file. The contents of the file can be found at the bottom of this document. This file contains a football team's name, the number of games they have won, and the number of games they have lost.
Your program should accomplish the following tasks:
1. Process all data until it reaches the end-of-file. Calculate the win percentage for each team.
2. Output to a file ("top.txt") a listing of all teams with a win percentage greater than .500. This file should contain the team name and the win percentage.
3. Output to a file ("bottom.txt") a listing of all teams with a win percentage of .500 or lower. This file should contain the team name and the win percentage.
4. Count and print to the screen the number of teams with a record greater then .500 and the number of teams with a record of .500 and below, each appropriately labeled.
5. Output in a message box: the team with the highest win percentage and the team with the lowest win percentage, each appropriately labeled. If there is a tie for the highest win percentage or a tie for the lowest win percentage, you must output all of the teams.
Dallas 5 2
Philadelphia 4 3
Washington 3 4
NY_Giants 3 4
Minnesota 6 1
Green_Bay 3 4
- 04-08-2008, 03:23 PM #2
In your section that reads as follows:
You are using a new input stream that you never use again. You should be using the "fin" that you previously declared and are using in the rest of your program. Since you set inFile as your txt file and then try to use fin without ever setting it to your txt file it is giving a null pointer exception. Change that section to the following and it will work.Java Code:try { Scanner inFile = new Scanner(new FileReader("football.txt")); } catch( FileNotFoundException e ) { System.exit( 1 ); }
Java Code:try { fin = new FileInputStream("football.txt"); } catch( FileNotFoundException e ) { System.exit( 1 ); }
Similar Threads
-
help with these errors
By oceansdepth in forum New To JavaReplies: 3Last Post: 04-16-2008, 04:55 PM -
Errors I don't understand
By MattyB in forum New To JavaReplies: 4Last Post: 04-01-2008, 11:55 PM -
I have 3 errors after compiling
By coco in forum JDBCReplies: 2Last Post: 10-18-2007, 09:32 AM -
Trying to catch thread errors
By yelllow4u in forum New To JavaReplies: 2Last Post: 08-07-2007, 02:52 PM -
Errors in constructor
By ai_2007 in forum Advanced JavaReplies: 0Last Post: 07-01-2007, 05:35 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks