Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Temer

[java] Funny Exception

This topic is 6454 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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()); } }

Share this post


Link to post
Share on other sites
Advertisement
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!