Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException

810545Nov 4 2010 — edited Nov 5 2010
Hi everyone,

Have just completed a Java paper at University which was pretty tough. I can understand how most things go together but I still have a little way to go before I can say I am fully competent with Java. With that in mind please don't ridicule me too much if my error results from some "obvious mistake", am still taking things in :).

Anyway to the point... I have tried to make a Java App that calculates some cricket statistics. It compiles ok but I get a runtime error that includes "Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException" in it, after I have ran CricketBattingApplication.java and clicked on the "Get your batting stats button". Here is the code in question:

CricketBattingPanel.java_

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.DecimalFormat;
import javax.swing.text.*;
import java.util.InputMismatchException;

class JTextFieldLimit extends PlainDocument {
private int limit;
private boolean toUppercase = false;

JTextFieldLimit(int limit) {
super();
this.limit = limit;
}

JTextFieldLimit(int limit, boolean upper) {
super();
this.limit = limit;
toUppercase = upper;
}

public void insertString
(int offset, String str, AttributeSet attr)
throws BadLocationException {
if (str == null) return;

if ((getLength() + str.length()) <= limit) {
if (toUppercase) str = str.toUpperCase();
super.insertString(offset, str, attr);
}
}
}

public class CricketBattingPanel extends JPanel{

private JTextField runsScoredTF, notOutsTF, inningsBattedTF, ballsFacedTF;
private JButton getStats;
private double battingAverage, strikeRate;
private int lowestScore, highestScore;
private JLabel enterRunsScored, enterNotOuts, enterInningsBatted, enterBallsFaced, seperateByComma,
seperateByComma2;
private String s1="", s2="", s3="", s4="";
private int[] runsScored;
private int notOuts, inningsBatted, ballsFaced;

public CricketBattingPanel(){

getStats = new JButton("Get your batting stats");
Listener l = new Listener();
getStats.addActionListener(l);

setBackground(Color.green);
setPreferredSize(new Dimension(290, 600));

runsScoredTF = new JTextField(10);
notOutsTF = new JTextField(10);
inningsBattedTF = new JTextField(10);
ballsFacedTF = new JTextField(10);

runsScoredTF.setDocument(new JTextFieldLimit(50));
notOutsTF.setDocument(new JTextFieldLimit(50));
inningsBattedTF.setDocument(new JTextFieldLimit(50));
ballsFacedTF.setDocument(new JTextFieldLimit(50));

enterRunsScored = new JLabel("Enter number of runs scored in each innings");
enterNotOuts = new JLabel("Enter number of not-outs");
enterInningsBatted = new JLabel("Enter number of innings batted");
enterBallsFaced = new JLabel("Enter number of balls faced");
seperateByComma = new JLabel("(" +"Seperate runs scored" + " (" + "in each innings");
seperateByComma2 = new JLabel("with a comma" + "):");

add(enterRunsScored);
add(seperateByComma);
add(seperateByComma2);
add(runsScoredTF);
add(enterNotOuts);
add(notOutsTF);
add(enterInningsBatted);
add(inningsBattedTF);
add(enterBallsFaced);
add(ballsFacedTF);
add(getStats);

}

public double getAverage(){
double average;
int totalRunsScored = 0;
for(int i = 0; i < runsScored.length; i++){

totalRunsScored += runsScored;


}
average = (double)totalRunsScored/(inningsBatted - notOuts);
return average;
}

public double getStrikeRate(){
int totalRunsScored = 0;
double strikeRate;
for(int i = 0; i < runsScored.length; i++){

totalRunsScored += runsScored[i];


}
strikeRate = (double)totalRunsScored / ballsFaced;
return strikeRate;
}

public int getHighestScore(){
int index = 0;
int largest;
largest = runsScored[0];
for(int i = 0; i < runsScored.length; i++){

if(largest < runsScored[i]){
largest = runsScored[i];
index = i;
}


}
return index;
}

public int getLowestScore(){
int index = 0;
int min = runsScored[0];
for(int i = 0; i < runsScored.length; i++){

if(min > runsScored[i]){
min = runsScored[i];
index = i;
}


}
return index;
}


public void paintComponent(Graphics page){
super.paintComponent(page);
page.drawString(s1, 10, 350);
page.drawString(s2, 10, 370);
page.drawString(s3, 10, 390);
page.drawString(s4, 10, 410);
}

private class Listener implements ActionListener{
public void actionPerformed(ActionEvent event){

if(event.getSource() == getStats){
s1 = "Your average is " + getAverage();
s2 = "Your strike rate is " + getStrikeRate();
s3 = "Your lowest score is " + getLowestScore();
s4 = "Your highest score is " + getHighestScore();
repaint();
}

}
}
}

CricketBattingApplication.java_ +(use this to run the code)+

import javax.swing.JFrame;

public class CricketBattingApplication{

public static void main(String[] args){
JFrame frame = new JFrame("Cricket Batting Stat Machine");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new CricketBattingPanel());
frame.pack();
frame.setVisible(true);

}
}

If someone could fix the code and tell me how they did it and why certain code was used that would be really good. This is probably the hardest app I have tried to make in Java so far. Also if there is anything else you think should be added/fixed etc feel free to do that also. If you do this for me you are free to use the code wherever, just would like to learn how to make good Java apps and hone my Java skills etc. I have made a fully working app that converts temperatures from F to C, and has try-catch blocks etc so the program doesn't crash when there is inappropriate input.

Cheers and thankyou in advance!
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 2 2010
Added on Nov 4 2010
4 comments
1,458 views