please help...java.lang.NullPointerException
Hello!
I've researched this error message and found a lot of helpful information but I am too new to connect the dots, so to speak. If anyone has a spare moment to give me suggestions on better coding or why it's broken, I'll say Power Ball Lottery prayers for you!!
***********Below is my error:*************
Exception occurred during event dispatching:
java.lang.NullPointerException
at DietDiary$1.actionPerformed(DietDiary.java:80)
at java.awt.Button.processActionEvent(Button.java)
at java.awt.Button.processEvent(Button.java)
at java.awt.Component.dispatchEventImpl(Compiled Code)
at java.awt.Component.dispatchEvent(Compiled Code)
at java.awt.EventDispatchThread.run(EventDispatchThread.java)
*********And here is my code**********
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class DietDiary extends Applet {
//SETTING VARIABLES
TextField calorie1;
TextField calorie2;
TextField calorie3;
TextField calorie4;
TextField calorie5;
TextField calorie6;
Button totalCalories;
Label calories;
public void init (){
setLayout(new GridLayout(15,2));
add(new Label("Breakfast", Label.LEFT));
calorie1 = new TextField(7);
add(calorie1);
add(new Label("Snack", Label.LEFT));
calorie2 = new TextField(7);
add(calorie2);
add(new Label("Lunch", Label.LEFT));
calorie3 = new TextField(7);
add(calorie3);
add(new Label("Snack", Label.LEFT));
calorie4 = new TextField(7);
add(calorie4);
add(new Label("Dinner", Label.LEFT));
calorie5 = new TextField(7);
add(calorie5);
add(new Label("Snack", Label.LEFT));
calorie6 = new TextField(7);
add(calorie6);
totalCalories = new Button("Total Calories");
add(totalCalories);
//SETTING ACTIONS
ActionListener al = new ActionListener() {
public void actionPerformed(ActionEvent ae)
{
String text1 = calorie1.getText();
String text2 = calorie2.getText();
String text3 = calorie3.getText();
String text4 = calorie4.getText();
String text5 = calorie5.getText();
String text6 = calorie6.getText();
int value1 = 0;
int value2 = 0;
int value3 = 0;
int value4 = 0;
int value5 = 0;
int value6 = 0;
try
{
value1 = Integer.parseInt(text1);
value2 = Integer.parseInt(text2);
value3 = Integer.parseInt(text3);
value4 = Integer.parseInt(text4);
value5 = Integer.parseInt(text5);
value6 = Integer.parseInt(text6);
}
catch (NumberFormatException nfe)
{
return;
}
calories.setText(String.valueOf(value1 + value2 + value3 + value4 + value5 + value6));
}
};
totalCalories.addActionListener(al);
}
}

