Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-08-2008, 04:55 AM
Member
 
Join Date: Apr 2008
Posts: 1
irishsea2828 is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 04-08-2008, 05:23 PM
Chris.Brown.SPE's Avatar
Member
 
Join Date: Apr 2008
Location: State College, PA
Posts: 50
Chris.Brown.SPE is on a distinguished road
Send a message via AIM to Chris.Brown.SPE
In your section that reads as follows:

Code:
try { Scanner inFile = new Scanner(new FileReader("football.txt")); } catch( FileNotFoundException e ) { System.exit( 1 ); }
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.
Code:
try { fin = new FileInputStream("football.txt"); } catch( FileNotFoundException e ) { System.exit( 1 ); }
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
help with these errors oceansdepth New To Java 3 04-16-2008 06:55 PM
Errors I don't understand MattyB New To Java 4 04-02-2008 01:55 AM
I have 3 errors after compiling coco Database 2 10-18-2007 11:32 AM
Trying to catch thread errors yelllow4u New To Java 2 08-07-2007 04:52 PM
Errors in constructor ai_2007 Advanced Java 0 07-01-2007 07:35 PM


All times are GMT +3. The time now is 11:57 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org