Can anyone help with this code. I'm getting NullPointerException which I can't seem to fix. My code only reads one input line into the array then simply throws an exception complaining of null pointers, even though I have a loop to read several lines of input.
Code:
public static void main(String[] args) {
final int MAX = 5;
Golfer[] golfer = new Golfer[MAX]; // array to hold all the individual golfers
int[] holes = new int[9];
System.out.printf( "Enter golfer's name followed by 9 scores:\n" );
Scanner console = new Scanner( System.in );
int index = 0;
while ( index < MAX )
{ String inputLine = console.nextLine();
if ( inputLine.length() <= 0 ) // test for an empty string
{
break; // exit loop
}
else // parse the data in the inputLine.
{
Scanner fields = new Scanner( inputLine );
String name = fields.next(); // read the first field
for( int i = 0 ; i < holes.length ; i++ ) // read the score fields into array
{
holes[i] = fields.nextInt();
}
golfer[index] = new Golfer( name , holes );
index++;
}
} // end while loop.
......
.......
I've run your code (had to create my own Golfer class) and it ran without any errors.
The exception should tell you what file and line number the exception occurred on. Could you provide those details?
Could you provide sample input that produces the null pointer?
I used the following input, again it didn't crash
w 1 1 1 1 1 1 1 1 1
r 2 2 2 2 2 2 2 2 2
a 3 3 3 3 3 3 3 3 3
o 4 4 4 4 4 4 4 4 4
m 5 5 5 5 5 5 5 5 5
/**
Description:
The main program will prompt the user for the data entered one
line at a time. Then compute and display the total strokes
and determine the player with the lowest total.
Inputs:
The user is prompted for input strings containing a name and a set of 9 integers.
Outputs:
The lowest score is displayed, and all the input names and
holes are displayed.
*/
import java.util.Scanner;
import java.text.Format;
import java.io.*;
public class Project {
public static void main(String[] args) {
final int MAX = 5;
Golfer[] golfer = new Golfer[MAX]; // array to hold all the individual golfers
int[] holes = new int[9];
System.out.printf( "Enter golfer's name followed by 9 scores:\n" );
Scanner console = new Scanner( System.in );
int index = 0;
while ( index < MAX )
{ String inputLine = console.nextLine();
if ( inputLine.length() <= 0 ) // test for an empty string
{
break; // exit loop
}
else // parse the data in the inputLine.
{
Scanner fields = new Scanner( inputLine );
String name = fields.next(); // read the first field
for( int i = 0 ; i < holes.length ; i++ ) // read the score fields into array
{
holes[i] = fields.nextInt();
}
golfer[index] = new Golfer( name , holes );
index++;
}
} // end while loop.
if ( index > 0 )
{ System.out.printf( "%-8s%-8s%-8s%-8s%-8s%-8s%-8s%-8s%-8s%-8s%-8s%-8s",
"Name", "H-1", "H-2", "H-3", "H-4", "H-5", "H-6", "H-7",
"H-8", "H-9", "Total", "+-Par\n" );
for( int i = 0 ; i < golfer.length ; i++ )
{ golfer[i].displayStats();
System.out.printf( "%-10d%-10d", golfer[i].totalStrokes(),
golfer[i].totalStrokes() - golfer[0].totalStrokes() );
}
int low = golfer[0].totalStrokes();
for ( int i = 1 ; i < golfer.length ; i++) // determine the lowest total
{ if ( golfer[i].totalStrokes() < low )
low = golfer[i].totalStrokes();
}
System.out.printf( "%s %d", "The low score is:", low );
}
} // end main
} // end class Project
/**
Description:
This file contains the Golfer class. The Golfer class
is used to declare objects of type Golfer.
Private Instance Variables (Fields)
name - The golfer's name.
holes - The golfer's scores.
Methods:
displayStats - prints the instance field data for one golfer.
getName - return the "name" instance field.
totalStrokes - return the total strokes for holes played by the golfer.
getHoleStrokes - return the "holes" instance field.
Constructors:
This class has one constructor. The constructor is used to
initialize the instance fields with user supplied values from
the formal parameter list.
*/
class Golfer {
private String name_; // instance variable
private int[] holes_; // instance variable
public Golfer( String name, int[] holes ) // constructor
{ holes_ = new int[9];
name_ = name;
for ( int i = 0 ; i < holes.length ; i++)
{ holes_[i] = holes[i];
}
}
public void displayStats()
{
System.out.printf( "%-8s", name_ );
for ( int n: holes_ )
System.out.printf( "%-8d", n );
}
public String getName()
{
return name_;
}
public int totalStrokes()
{
int totalVal = 0;
for ( int n: holes_ )
totalVal += n;
return totalVal;
}
public int[] getHoleStrokes()
{
return holes_;
}
} // end class Golfer
What are you inputting into the scanner?
Given this:
PHP Code:
for( int i = 0 ; i < golfer.length ; i++ )
golfer[] is always of size MAX which is 5 in this case. If you input 1 entry, you will always get a nullpointerexception since golfer[1-4] will be null. You can deal with this in a number of ways, changing the array to a scalable collection like an ArrayList, adding your own counter, try/catching the exception, or detecting null. The easiest is to detect null. I'll also re-do the lowest score to Integer.MAX_VALUE so that we don't need to add two checks just for that:
PHP Code:
for (int i = 0; i < golfer.length; i++) { if (golfer[i] != null) { golfer[i].displayStats(); System.out .printf("%-10d%-10d", golfer[i].totalStrokes(), golfer[i].totalStrokes() - golfer[0].totalStrokes()); } }
int low = Integer.MAX_VALUE; for (int i = 0; i < golfer.length; i++) // determine the lowest { if (golfer[i] != null) { if (golfer[i].totalStrokes() < low) low = golfer[i].totalStrokes(); } } if (low < Integer.MAX_VALUE) { System.out.printf("%s %d", "The low score is:", low); }
Edit:
Also, I noticed that there is an explicit golfer[0] within the total strokes. This will actually need to be verified as well, since golfer[0] could be null while any other of the golfer could not be.
I know I could solve it using an ArrayList instead of using a fixed size. However, it's a question requirement.
The input is written in the code,
Code:
Inputs:
The user is prompted for input strings containing a name and a set of 9 integers.
Still, the same problem. I'm getting cut after inputting two lines,
Code:
Enter golfer's name followed by 9 scores:
par 1 2 3 4 5 6 7 8 9
me 1 2 3 4 5 6 7 9 9
Name H-1 H-2 H-3 H-4 H-5 H-6 H-7 H-8 H-9 Total +-Par
par 1 2 3 4 5 6 7 8 9 45 0
The low score is: 45
Process completed.
I think your console may be cut off. I have pretty much what you have plus the change I made, and it works fine. The only difference is that it lacks a linefeed between the golfers, which you can either add a println or just add a \n to your format.
Using the same data, I get this:
Code:
Enter golfer's name followed by 9 scores:
par 1 2 3 4 5 6 7 8 9
me 1 2 3 4 5 6 7 9 9
Name H-1 H-2 H-3 H-4 H-5 H-6 H-7 H-8 H-9 Total +-Par
par 1 2 3 4 5 6 7 8 9 45 0
me 1 2 3 4 5 6 7 9 9 46 1
The low score is: 45
The first line is intended due to the \n applied against the +-par, I moved it to a part of the format, and that lines up as expected. The only other thing missing is the Process Complete message, which I don't have.