Jump to content

  • Log In with Google      Sign In   
  • Create Account


[java] Funny Exception


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
4 replies to this topic

#1 Temer   Members   -  Reputation: 122

Like
Likes
Like

Posted 13 February 2001 - 06:53 AM

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©) fin.append(dig); else fin.append©; } result.setText(fin.toString()); } }

Sponsor:

#2 ToddH   Members   -  Reputation: 122

Like
Likes
Like

Posted 14 February 2001 - 09:46 AM

what was the exception?

#3 GameFiend   Members   -  Reputation: 122

Like
Likes
Like

Posted 14 February 2001 - 12:45 PM

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

#4 letsch   Members   -  Reputation: 122

Like
Likes
Like

Posted 14 February 2001 - 12:59 PM

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

#5 GameFiend   Members   -  Reputation: 122

Like
Likes
Like

Posted 14 February 2001 - 01:29 PM



act.addActionListener(this);


yah I did that too forgot to write it, sorry




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS