Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.
Trending News
java null pointer exception error...?
here is a simple program. that takes five results from the user and calculates the average. After that it rates each result out of 100. But i get pointer exception error at this two lines
if (result[i] >=90 && result[i] <= 100)
myInstance.determineGrade();
import java.util.*;
import javax.swing.JOptionPane;
import java.util.Scanner;
import java.io.*;
public class Main {
public int sum=0,i;
public double aVERAGE;
public String grade[];
public int[] result;
/** Creates a new instance of Main */
public Main() {
}
static Scanner sc = new Scanner(System.in);
/*static Scanner sc = new Scanner(System.in);*
* @param args the command line arguments
*/
public static void main(String[] args) throws IOException {
Main myInstance = new Main();
myInstance.calcAverage();
myInstance.determineGrade();
}
public void calcAverage(){
System.out.print("Please enter five test results\n");
Scanner keyboard = new Scanner(System.in);
int[] result = new int[5];
for(i=0;i<5;i++){
System.out.print("Please enter the " + (i+1) + " result \n");
result[i] = keyboard.nextInt();
sum += result[i];
}
aVERAGE = (double) sum/5;
System.out.print(aVERAGE);
}
public void determineGrade(){
String[] grade = new String[5];
for (i=0;i<5;i++){
if (result[i] >=90 && result[i] <= 100)
grade[i] = "A";
else if (result[i] >=80 && result[i] <= 89)
grade[i] = "B";
else if (result[i]>= 70 && result[i] <= 79)
grade[i] = "C";
else if (result[i] >= 60 && result[i]<= 69)
grade[i] = "D";
else grade[i] = "F";
System.out.print(" " + grade[i] );
}
6 Answers
- 1 decade agoFavorite Answer
In your function calcAverage(), you have a "int[] result = new int[5];" declaration. This makes result a local variable and never instantiates the class variable. Drop the int[] and your program will work just fine.
Rest of the code looks fine.
public void calcAverage(){
System.out.print("Please enter five test results\n");
Scanner keyboard = new Scanner(System.in);
result = new int[5]; // changed this line
for(i=0;i<5;i++){
..... rest as is....
Good luck!
- Anonymous1 decade ago
I don't know java but this only creates 5 elements in the array, I beleive
int[] result = new int[5];
But here you are going through 6 elements. I expect result[5] is causing a problem.
for (i=0;i<5;i++){
if (result[i] >=90 && result[i] <= 100)
- deonejuanLv 71 decade ago
u gotta scope problem
public void calcAverage(){
int[] result = new int[5];
}
public void determineGrade() {
// i don't know result[i] from Adam
// the curly braces makes me blind
// could try public void determineGrade( int[] results ) {
and call determineGrade @ end of calcAverage( results );
}
and it is always...
for ( int i = 0...
- 1 decade ago
TRY CHANGE
public int[] result;
TO
public int[] result=new int[5];
Source(s): http://java2everyone.blogspot.com/ - How do you think about the answers? You can sign in to vote the answer.
- 1 decade ago
computer programming service,web site design,homework help
custome website design etc, at