[java] Funny Exception

Started by
3 comments, last by Temer 23 years, 2 months ago
Hello I am working on this program to take out digits in a sentence and replace them with the digit''s word. The program compiles but I get this funny exception. Could someone take a look at it for me? I am pasting it. import java.awt.*; import java.applet.*; import javax.swing.*; import java.util.*; import java.awt.event.*; public class DigiConverter extends Applet implements ActionListener { //use boolean isDigit(char ch) TextField enteredString; TextField result; StringBuffer temp; StringBuffer fin; Button act; char c; String[] dig = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"}; public void init() { fin = new StringBuffer(100); temp = new StringBuffer(100); enteredString = new TextField(50); result = new TextField(50); act = new Button("Enter"); add(result); add(enteredString); add(act); act.addActionListener(new DigiConverter()); } public void actionPerformed(ActionEvent e) { String s = enteredString.getText(); temp.append(s.trim()); for(int i=0; i < temp.length(); i++) { c = temp.charAt(i); if(Character.isDigit(c)) fin.append(dig); else fin.append(c); } result.setText(fin.toString()); } }
Advertisement
what was the exception?
I loaded this up and it compiled fine as you said.. but the applet wouldn''t initialize. Is that the prob you were having? If so your browser/viewer either doesn''t support the swing class or cant find your class file. I appended the html file and got it to open but wasn''t sure what you had planned from there. I did notice ya hadnt made the variables private. I was wondering if there was a reason for that>? Cant spend any mroe time looking at it at the moment .. trying to get my Card game going let me know if any of that helped.. I may have a chance again later

GF

If ya need to know how to append the HTML let me know
acutally pretty simple...

what you do is:
act.addActionListener(new DigiConverter());

this means... there is a new instance of the DigiConverter. And that''s why there is the Nullpointer Exception. What happens is, that you press the button, and actionPerformed(...) of the NEW instance is called. This instance doesn''t have ANY enteredString set, all variables are null, and... yeah, if they wouldn''t be null they at least wouldn''t have the information you want it to have.

Simply write
act.addActionListener(this);
and it will work. I mean... there will be no Exception anymore, didn''t have a look at what the program really does (or doesn''t).

so long,
Letsch


act.addActionListener(this);


yah I did that too forgot to write it, sorry

This topic is closed to new replies.

Advertisement