The Artima Developer Community
Sponsored Link

Java Answers Forum
I get this error...

4 replies on 1 page. Most recent reply: Nov 22, 2002 11:53 AM by Matt Gerrans

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
guy

Posts: 4
Nickname: guy123123
Registered: Nov, 2002

I get this error... Posted: Nov 21, 2002 7:43 AM
Reply to this message Reply
Advertisement
Exception occurred during event dispatching:
java.lang.NullPointerException:
at EApplet.actionPerformed(EApplet.java:82)
at java.awt.Button.processActionEvent(Button.java:308)
at java.awt.Button.processEvent(Button.java:281)
at java.awt.Component.dispatchEventImpl(Compiled Code)
at java.awt.Component.dispatchEvent(Compiled Code)
at java.awt.EventQueue.dispatchEvent(Compiled Code)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:68)

Heres my program:


import java.awt.*;
import java.awt.event.*;
import java.applet.*;
 
public class EApplet extends Applet implements ActionListener
{
private Button keysArray[];
private Panel keyPad;
private TextField lcdField;
private TextField QuestionField;
private Label ScoreLabel;
private int Score;
private int i;
private String Questions[];
private int Answers[];
private boolean foundKey;
 
public void init()
{
	foundKey = false;
	int i = 1;
	int[] Answers = new int[5];
	String[] Questions = new String[5];
	Questions[1] = "A Tomato is a vegetable.";
	Answers[1] = 0;
	Questions[2] = "You love this game.";
	Answers[2] = 1;
	Questions[3] = "Troy is an Idiot!";
	Answers[3] = 1;
	Questions[4] = "The sun is warm.";
	Answers[4] = 1;
	lcdField = new TextField(20);
	QuestionField = new TextField(20);
	
	ScoreLabel = new Label("0");
	Score = 0;
 
	keyPad = new Panel();
	keysArray = new Button[3];
 
 
	
	setLayout(new BorderLayout());
 
	lcdField.setEditable(false);
	QuestionField.setEditable(false);
 
 
	
 
keysArray[1] = new Button("YES");
 
keysArray[2] = new Button("NO");
 
keyPad.setLayout(new GridLayout(1,2));
 
 
	keyPad.add(keysArray[1]);
	keyPad.add(keysArray[2]);
 
 
 
 
add(lcdField, BorderLayout.NORTH);
add(QuestionField, BorderLayout.SOUTH);
add(ScoreLabel, BorderLayout.EAST);
add(keyPad, BorderLayout.CENTER);
QuestionField.setText(" " + (Questions[i]));
 
 
	keysArray[1].addActionListener(this);
 
	keysArray[2].addActionListener(this);
 
}
public void actionPerformed(ActionEvent e)
{
	foundKey = false;
	
 
	
		QuestionField.setText(""+Questions[i]);
		
		if(e.getSource() == keysArray[1])
		{
			foundKey = true;
			
			if(Answers[i] == 1)
			{
			
			lcdField.setText("You are Correct!!");
			Score += 1;
				ScoreLabel.setText(" " + (Score));
			}
			if(Answers[i] == 0)
			{
		
			Score -= 1;
			ScoreLabel.setText(" " + (Score));
			lcdField.setText("You are Incorrect!!");
			}
		}
		if(e.getSource() == keysArray[2])
		{
 
		lcdField.setText(""+(Answers[i]));
		foundKey = true;
 
		if(Answers[i] == 0)
			{
			
			lcdField.setText("You are Correct!!");
			Score += 1;
			ScoreLabel.setText(" " + (Score));
			}
			if(Answers[i] == 1)
			{
			
			Score -= 1;
			ScoreLabel.setText(" " + (Score));
			lcdField.setText("You are Incorrect!!");
			}
		
}
if(Score == 4)
	{
	lcdField.setText("You WIN!!!");
	}
	else
	{
	lcdField.setText("You lose...");
	}
}
	
 
 
 
 
}
 


Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: I get this error... Posted: Nov 21, 2002 12:51 PM
Reply to this message Reply
The main problem is that you re-declared the Questions array in the init() method, instead of filling up the one that is already a member of the class. So all you did is create a new array with the same name that was local to the method. When the init() method completes, that array goes away. The Questions array member variable remains null, of course, so when it is accessed, you get the exception.

A few more tips:
- You can initialize the array with string right when you created it.
- You seem to have a mismatch between your array size (5) and the number of items in the arrays (4).
- I would use a Map for this instead of two arrays and read the data from a file.

guy

Posts: 4
Nickname: guy123123
Registered: Nov, 2002

Re: I get this error... Posted: Nov 22, 2002 7:41 AM
Reply to this message Reply
Well i dont really understand. I am in a java class where i have a teacher who doesnt know java... and all i do is copy programs out of a book and see if they work right. So i decided to try to make my own program. The problem is i havent been taught anything and i know relatively little about the language. I tried to fix it, but i think i just messed it up more. Thanks for ur help. ill just learn more and come back to this program later.

guy

Posts: 4
Nickname: guy123123
Registered: Nov, 2002

Re: I get this error... Posted: Nov 22, 2002 7:43 AM
Reply to this message Reply
wait.... i went back erased 2 lines of code and it worked sorta.. thanks again

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: I get this error... Posted: Nov 22, 2002 11:53 AM
Reply to this message Reply
Here is a slightly modified version of your program to illustrate what I meant. When you write "private int x;" inside your class definition, you've declared a member variable x. If you write "x = 5;" in your init() method then you modify that member variable. However, if you write "int x = 5;" in your init() method, you've just created another variable which resides inside the scope of the method and set it to 5, but the member variable you declared before is unaffected.

You'll see below what I meant about initializing the variables where you declare them, also.

By the way, I added a main, which puts the applet into a JFrame, so that the thing can run stand-alone, without a web page or appletviewer (just "java EApplet" will run it).

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class EApplet extends Applet implements ActionListener
{
   private Button keysArray[];
   private Panel keyPad;
   private TextField lcdField;
   private TextField QuestionField;
   private Label ScoreLabel;
   private int Score;
   private int i;
   private String Questions[];
 
   String[] Questions = { "A Tomato is a vegetable.",
                          "There are gray elephants in Denmark.",
                          "Certain politicians really are morons.",
                          "The sun is warm.",
                          "Ontogeny recapitulates phylogeny."};
   int[] Answers = {0,  // Nope, it's a fruit.
                    1,  // Obviously.
                    1,  // Of course.
                    0,  // No, it is hot!
                    1}; // Natually.
   private int Answers[];
   private boolean foundKey;
 
   public void init()
   {  
      foundKey = false;
      int i = 1;
      /*int[] Answers = new int[5];

      String[] Questions = new String[5];
      Questions[1] = "A Tomato is a vegetable.";
      Answers[1] = 0;
      Questions[2] = "You love this game.";
      Answers[2] = 1;
      Questions[3] = "Troy is an Idiot!";
      Answers[3] = 1;
      Questions[4] = "The sun is warm.";
      Answers[4] = 1;
      */
      lcdField = new TextField(20);
      QuestionField = new TextField(20);
      ScoreLabel = new Label("0");
      Score = 0;
      keyPad = new Panel();
      keysArray = new Button[3];
      setLayout(new BorderLayout());
      lcdField.setEditable(false);
      QuestionField.setEditable(false);
      keysArray[1] = new Button("YES");
      keysArray[2] = new Button("NO");
      keyPad.setLayout(new GridLayout(1,2)); keyPad.add(keysArray[1]);
      keyPad.add(keysArray[2]);add(lcdField, BorderLayout.NORTH);
      add(QuestionField, BorderLayout.SOUTH);
      add(ScoreLabel, BorderLayout.EAST);
      add(keyPad, BorderLayout.CENTER);
      QuestionField.setText(" " + (Questions[i]));
      keysArray[1].addActionListener(this);
      keysArray[2].addActionListener(this);
   }
 
   public void actionPerformed(ActionEvent e)
   {
      foundKey = false;
      System.out.println( "Question: " + Questions[i] );
      QuestionField.setText( Questions[i] );
      if(e.getSource() == keysArray[1])
      {
         foundKey = true;
         if(Answers[i] == 1)
         {
            lcdField.setText("You are Correct!!");
            Score += 1;
            ScoreLabel.setText(" " + (Score));
         }
         
         if(Answers[i] == 0)
         {
            Score -= 1;
            ScoreLabel.setText(" " + (Score));
            lcdField.setText("You are Incorrect!!");
         }
      }
      
      if(e.getSource() == keysArray[2])
      {
         lcdField.setText(""+(Answers[i]));
         foundKey = true;
         if(Answers[i] == 0)
         {
            lcdField.setText("You are Correct!!");
            Score += 1;
            ScoreLabel.setText(" " + (Score));
         }
         
         if(Answers[i] == 1)
         {
            Score -= 1;
            ScoreLabel.setText(" " + (Score));
            lcdField.setText("You are Incorrect!!");
         }
      }
      
      if(Score == 4)
      {
         lcdField.setText("You WIN!!!");
      }
      else
      {
         lcdField.setText("You lose...");
      }
   }
 
   public static void main(String[] args) 
   {
      Applet applet = new EApplet();
      javax.swing.JFrame frame = new javax.swing.JFrame("EApplet");
 
      // To close the application:
      frame.setDefaultCloseOperation( javax.swing.JFrame.EXIT_ON_CLOSE );
      frame.getContentPane().add(applet);
      frame.setSize(400,300);
      applet.init();
      applet.start();
      frame.setVisible(true);
   }
}

Flat View: This topic has 4 replies on 1 page
Topic: Java Programming Previous Topic   Next Topic Topic: Convert long date to byte array

Sponsored Links



Google
  Web Artima.com   

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