The Artima Developer Community
Sponsored Link

Java Answers Forum
Null Pointer Exception

4 replies on 1 page. Most recent reply: Aug 21, 2003 6:50 AM by Joe Parks

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 4 replies on 1 page
Chris

Posts: 2
Nickname: bigc06
Registered: Aug, 2003

Null Pointer Exception Posted: Aug 21, 2003 1:39 AM
Reply to this message Reply
Advertisement
I did this program and when I run it in my impl class I keep getting Null Pointer Exception...can someone help me





/**
Class for a one-week record of the time worked by
each employee. Uses a five-day week (Mon.-Fri.).
main has a sample application.
*/
import psJava.KeyIn;

import javax.swing.JOptionPane;

public class TestScore
{
private int nStudents;
private int totalScores = 0;
private int[][] scores;
private int[] studentScores;


public static int readInt(String prompt){
String numStr = JOptionPane.showInputDialog(prompt);
return Integer.parseInt(numStr);
}




public void testScore(int nStudents)
{
nStudents = nStudents;
scores = new int[6][nStudents];
studentScores = new int[6];
}

public void setScores( ) //This is just a stub.
{
scores[0][0] = 13; scores[0][1] = 14; scores[0][2] = 13; scores[0][3] = 14; scores[0][4] = 13;
scores[1][0] = 15; scores[1][1] = 18; scores[1][2] = 11; scores[1][3] = 18; scores[1][4] = 11;
scores[2][0] = 4; scores[2][1] = 11; scores[2][2] = 11; scores[2][3] = 17; scores[2][4] = 17;
scores[3][0] = 16; scores[3][1] = 12; scores[3][2] = 16; scores[3][3] = 16; scores[3][4] = 14;
scores[4][0] = 11; scores[4][1] = 7; scores[4][2] = 14; scores[4][3] = 12; scores[4][4] = 19;



try {
scores[5][0] = KeyIn.readInt("Please enter the score of the first exam ");
scores[5][1] = KeyIn.readInt("Please enter the score for next exam ");
scores[5][2] = KeyIn.readInt("Please enter score for the next exam ");
scores[5][3] = KeyIn.readInt("Please enter score for the fourth exam ");
scores[5][4] = KeyIn.readInt("Please enter socre for the last exam ");
}
catch (NumberFormatException ex) {

JOptionPane.showMessageDialog(null,
"Invalid integer " + scores + " - default is 1",
"Error", JOptionPane.ERROR_MESSAGE);
ex.printStackTrace();
}



catch (Exception ex) {

System.out.println(ex.getMessage());
ex.printStackTrace();
}



}

public void compute() {
computeTotStu();

}

public void computeTotStu( )
{
int student, studentScore, sum;

for (student = 0; student < 6; student++)
{//Process one day (for all students):
sum = 0;
for (studentScore = 1;
studentScore <= nStudents; studentScore++)
sum = sum + scores[student][studentScore - 1];

studentScores[student] = sum;
totalScores += sum;
}
}




public void makeTable( )
{
int row, column;
System.out.print("Exam #:" + "\t" + "\t");

for (column = 0; column < nStudents; column++)
System.out.print((column + 1)+ "\t");



System.out.println("Totals Grade");

System.out.println( );

//Sorts Total and Grade in descending order
for (int i = studentScores.length; --i >= 0; ){

for (int j = 0; j < i; j++)
{
if (studentScores[j] < studentScores[j+1])
{
int temp = studentScores[j];
studentScores[j] = studentScores[j + 1];
studentScores[j + 1] = temp;
}

}

}


for (row = 0; row < 6; row++)
{
System.out.print(studentScores[row]+ "\t");
for (column = 0; column < scores[row].length; column++)

System.out.print(scores[row][column] + "\t");

System.out.print(studentScores[row]+ "\t"+ "\t");

if (studentScores[row] >=85 && studentScores[row]<100)
System.out.println('A');


else if (studentScores[row] >=75 && studentScores[row]<85)
System.out.println('B');


else if (studentScores[row] >=65 && studentScores[row]<75)
System.out.println('C');

else if (studentScores[row] >=55 && studentScores[row]<65)
System.out.println('D');

else if (studentScores[row] >=0 && studentScores[row]<55)
System.out.println('F');



}

System.out.println( );

System.out.print("Total = " + "\t");
for (column = 0; column < nStudents; column++)
System.out.print("\t");
System.out.print(totalScores);
System.out.println();
System.out.println();
System.out.print("Average Score = " + totalScores/(nStudents + 1) + "\t");



System.out.println( );



}


private String studentName(int student)
{
String studentName = null;
switch (student)
{
case 0:
studentName = "Student 0: ";
break;
case 1:
studentName = "Student 1: ";
break;
case 2:
studentName = "Student 2: ";
break;
case 3:
studentName = "Student 3: ";
break;
case 4:
studentName = "Student 4: ";
break;
case 5:
studentName = "Student 5: ";
break;
default:
System.out.println("Fatal Error.");
System.exit(0);
break;
}

return studentName;
}

public int findMin() {

int studentScore, student = 0;
if (scores.length > 0) {
int minSoFar = scores[0][0]; // Remember first score


for (studentScore = 0; studentScore < scores.length; studentScore++) {
for (student = 0; student < scores[studentScore].length; student++) {
if (scores[studentScore][student] < minSoFar)
minSoFar = scores[studentScore][student];
}
}
return minSoFar;
}
else
return 0; // array is empty
}

public int findMax() {
// Find smallest score if array is not empty.
int studentScore, student = 0;
if (scores.length > 0) {
int maxSoFar = scores[0][0]; // Remember first score

// Remember any smaller scores.
for (studentScore = 1; studentScore < scores.length; studentScore++) {
for (student = 0; student < scores[studentScore].length; student++) {
if (scores[studentScore][student] > maxSoFar)
maxSoFar = scores[studentScore][student];
}
}
return maxSoFar;
}
else
return 0; // array is empty
}



}


Chris

Posts: 2
Nickname: bigc06
Registered: Aug, 2003

Re: Null Pointer Exception Posted: Aug 21, 2003 1:40 AM
Reply to this message Reply
here is my impl class



public class TestScoreImpl {


public static void main(String[] args)
{


TestScore tS = new TestScore();
tS.setScores( );
tS.compute();







System.exit(0);
}


}

David

Posts: 150
Nickname: archangel
Registered: Jul, 2003

Re: Null Pointer Exception Posted: Aug 21, 2003 1:58 AM
Reply to this message Reply
You could post the exception stack trace and save us the hassle of having to download your code.

David

Posts: 150
Nickname: archangel
Registered: Jul, 2003

Re: Null Pointer Exception Posted: Aug 21, 2003 1:59 AM
Reply to this message Reply
It doesn't help either that you're using a class which you haven't posted - KeyIn.

Joe Parks

Posts: 107
Nickname: joeparks
Registered: Aug, 2003

Re: Null Pointer Exception Posted: Aug 21, 2003 6:50 AM
Reply to this message Reply
KeyIn is an unnecessary import. The readInt(String message) method is defined in this class.

The NullPointer occurs when you attempt to assign values to an unitialized array.
You have defined what should be a constructor--TestScore(int nStudents)--as a public method that returns void. Thus, the initialization code is never executed. Here is a working version:
import javax.swing.JOptionPane;
 
public class TestScore {
    private int nStudents;
    private int totalScores = 0;
    private int[][] scores;
    private int[] studentScores;
 
 
    public static int readInt(String prompt) {
        String numStr = JOptionPane.showInputDialog(prompt);
        return Integer.parseInt(numStr);
    }
 
 
    public TestScore(int nStudents) {
        this.nStudents = nStudents;
        scores = new int[6][nStudents];
        studentScores = new int[6];
    }
 
    public void setScores() //This is just a stub.
    {
        scores[0][0] = 13;
        scores[0][1] = 14;
        scores[0][2] = 13;
        scores[0][3] = 14;
        scores[0][4] = 13;
        scores[1][0] = 15;
        scores[1][1] = 18;
        scores[1][2] = 11;
        scores[1][3] = 18;
        scores[1][4] = 11;
        scores[2][0] = 4;
        scores[2][1] = 11;
        scores[2][2] = 11;
        scores[2][3] = 17;
        scores[2][4] = 17;
        scores[3][0] = 16;
        scores[3][1] = 12;
        scores[3][2] = 16;
        scores[3][3] = 16;
        scores[3][4] = 14;
        scores[4][0] = 11;
        scores[4][1] = 7;
        scores[4][2] = 14;
        scores[4][3] = 12;
        scores[4][4] = 19;
 
 
        try {
            scores[5][0] = readInt("Please enter the score of the first exam ");
            scores[5][1] = readInt("Please enter the score for next exam ");
            scores[5][2] = readInt("Please enter score for the next exam ");
            scores[5][3] = readInt("Please enter score for the fourth exam ");
            scores[5][4] = readInt("Please enter socre for the last exam ");
        } catch (NumberFormatException ex) {
 
            JOptionPane.showMessageDialog(null,
                    "Invalid integer " + scores + " - default is 1",
                    "Error", JOptionPane.ERROR_MESSAGE);
            ex.printStackTrace();
        } catch (Exception ex) {
 
            System.out.println(ex.getMessage());
            ex.printStackTrace();
        }
 
 
    }
 
    public void compute() {
        computeTotStu();
 
    }
 
    public void computeTotStu() {
        int student, studentScore, sum;
 
        for (student = 0; student < 6; student++) {//Process one day (for all students):
            sum = 0;
            for (studentScore = 1;
                 studentScore <= nStudents; studentScore++)
                sum = sum + scores[student][studentScore - 1];
 
            studentScores[student] = sum;
            totalScores += sum;
        }
    }
 
 
    public void makeTable() {
        int row, column;
        System.out.print("Exam #:" + "\t" + "\t");
 
        for (column = 0; column < nStudents; column++)
            System.out.print((column + 1) + "\t");
 
 
        System.out.println("Totals Grade");
 
        System.out.println();
 
//Sorts Total and Grade in descending order
        for (int i = studentScores.length; --i >= 0;) {
 
            for (int j = 0; j < i; j++) {
                if (studentScores[j] < studentScores[j + 1]) {
                    int temp = studentScores[j];
                    studentScores[j] = studentScores[j + 1];
                    studentScores[j + 1] = temp;
                }
 
            }
 
        }
 
 
        for (row = 0; row < 6; row++) {
            System.out.print(studentScores[row] + "\t");
            for (column = 0; column < scores[row].length; column++)
 
                System.out.print(scores[row][column] + "\t");
 
            System.out.print(studentScores[row] + "\t" + "\t");
 
            if (studentScores[row] >= 85 && studentScores[row] < 100)
                System.out.println('A');
 
 
            else if (studentScores[row] >= 75 && studentScores[row] < 85)
                System.out.println('B');
 
 
            else if (studentScores[row] >= 65 && studentScores[row] < 75)
                System.out.println('C');
 
            else if (studentScores[row] >= 55 && studentScores[row] < 65)
                System.out.println('D');
 
            else if (studentScores[row] >= 0 && studentScores[row] < 55)
                System.out.println('F');
 
 
        }
 
        System.out.println();
 
        System.out.print("Total = " + "\t");
        for (column = 0; column < nStudents; column++)
            System.out.print("\t");
        System.out.print(totalScores);
        System.out.println();
        System.out.println();
        System.out.print("Average Score = " + totalScores / (nStudents + 1) + "\t");
 
 
        System.out.println();
 
 
    }
 
    /**
      * I don't know the point of this.  It is private and
      * it is never called.
      * It is also kind of silly.  If it's meant to provide a label,
      * String concatenation, I think, would be more appropriate.
      * Then you wouldn't have a "fatal error" if you have
      * more than 5 scores.
      */
    private String studentName(int student) {
        String studentName = null;
        switch (student) {
            case 0:
                studentName = "Student 0: ";
                break;
            case 1:
                studentName = "Student 1: ";
                break;
            case 2:
                studentName = "Student 2: ";
                break;
            case 3:
                studentName = "Student 3: ";
                break;
            case 4:
                studentName = "Student 4: ";
                break;
            case 5:
                studentName = "Student 5: ";
                break;
            default:
                System.out.println("Fatal Error.");
                System.exit(0);
                break;
        }
 
        return studentName;
    }
 
    public int findMin() {
 
        int studentScore, student = 0;
        if (scores.length > 0) {
            int minSoFar = scores[0][0]; // Remember first score
 
 
            for (studentScore = 0; studentScore < scores.length; studentScore++) {
                for (student = 0; student < scores[studentScore].length; student++) {
                    if (scores[studentScore][student] < minSoFar)
                        minSoFar = scores[studentScore][student];
                }
            }
            return minSoFar;
        } else
            return 0; // array is empty
    }
 
    public int findMax() {
// Find smallest score if array is not empty.
        int studentScore, student = 0;
        if (scores.length > 0) {
            int maxSoFar = scores[0][0]; // Remember first score
 
// Remember any smaller scores.
            for (studentScore = 1; studentScore < scores.length; studentScore++) {
                for (student = 0; student < scores[studentScore].length; student++) {
                    if (scores[studentScore][student] > maxSoFar)
                        maxSoFar = scores[studentScore][student];
                }
            }
            return maxSoFar;
        } else
            return 0; // array is empty
    }
 
    public static void main(String[] args) {
        TestScore testScore = new TestScore(5);
        testScore.setScores();
        testScore.compute();
        testScore.makeTable();
        System.exit(0);
    }
}

Flat View: This topic has 4 replies on 1 page
Topic: what we have to download to run servlets and jsp Previous Topic   Next Topic Topic: readLine()

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use