again i need help guys

this time with a dead line {tonight} :(

anyway the user is supposed to enter "i am user i am user" & the program will output

i2

am2

user2

this is the code

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import java.util.*;

publicclass Tryextends JFrame{

private JLabel label;

private JTextField field;

private JTextArea area;

private JScrollPane scroll;

privateint count;

private String poprage="";

public Try(){

super("Application 3 / Word Occurence");

getContentPane().setBackground(Color.CYAN);

Container c = getContentPane();

c.setLayout(new FlowLayout());

label =new JLabel("Enter The Text Here");

c.add(label);

field =new JTextField(30);

field.addActionListener(

new ActionListener(){

publicvoid actionPerformed(ActionEvent e){

StringTokenizer s =new StringTokenizer(e.getActionCommand());

count = s.countTokens();

while(s.hasMoreTokens()){

StringTokenizer z =new StringTokenizer(e.getActionCommand());

for(int i =0 ; i<count ;i++){

int c=0;

if(s.equals(z.nextToken())){

c++;

}

poprage += s+"\t"+c+"\n";}

}

}

}

);

area.append(poprage);

c.add(field);

area =new JTextArea(10,30);

area.setEditable(false);

c.add(area);

scroll =new JScrollPane(area);

c.add(scroll);

setSize(500,500);

show();

}

publicstaticvoid main (String ar[]){

Try a =new Try();

a.addWindowListener(

new WindowAdapter(){

publicvoid windowClosing( WindowEvent e ){

System.exit(0);

}

}

);

}

}

it gives me an exeption

Exception in thread main java.lang.NullPointerException

at Try.><init><Try.java.55>

at Try.main<Try.java.73>

first of all i did not take exeption handling or util package or data structure , so that no 1 insults me. O.K ?

second i really need ur help , thx in advance.

poprage.

[4096 byte] By [pop-rage] at [2007-9-30 14:59:43]
# 1
Which lines are in the stack trace .. I dotn feel like counting up to 73 in that code.
Kraythe at 2007-7-5 21:32:27 > top of Java-index,Archived Forums,Java Programming...
# 2
i just handeled the exception by removing the append of the output inside the ActionListener but it seems that it makes an infinite loop ?why ? ++ help :DRegards.
pop-rage at 2007-7-5 21:32:27 > top of Java-index,Archived Forums,Java Programming...
# 3

> Exception in thread main java.lang.NullPointerException

> at Try.<init><Try.java.55>

> at Try.main<Try.java.73>

>

This gives you all the information you need.

NullPointerException generally means you tried to dereference a null reference.

It also tells you that this occured on line 55 of the Try.java file.

Just look at that line and see what could me null. Once you do that then all you

need to do is figure out why it is null. When you do that, you will know how

to fix your problem.

whoisfred at 2007-7-5 21:32:27 > top of Java-index,Archived Forums,Java Programming...
# 4

I'd recommend that you use the -g option on javac.exe when you compile. It adds debugging information to the .class file, including the line numbers.

Open your Try.java, turn on line number display in your editor, go to line 55, and see what references are being used and why they might be null.

This suggests that you didn't initialize something in the constructor.

NPE should be one of the easiest exceptions to figure out. This will be a good chance to learn how to do it.

But look at this code:

area.append(poprage); // You reference the area object here

c.add(field);

area = new JTextArea(10,30); // You initialize the reference here

area.setEditable(false);

Move the area.append line so it follows the call to "new".

%

duffymo at 2007-7-5 21:32:27 > top of Java-index,Archived Forums,Java Programming...
# 5
there was an exception in this sentensce area.append(poprage);it was over as i said but it is till not working (infinite loop as i think) ?
pop-rage at 2007-7-5 21:32:27 > top of Java-index,Archived Forums,Java Programming...
# 6

Do you really want to tokenize the action command of the text field (which I think is null by default) ?

if not then you might want to use

StringTokenizer z = new StringTokenizer(field.getText());

instead of ..

StringTokenizer z = new StringTokenizer(e.getActionCommand());

Kraythe at 2007-7-5 21:32:27 > top of Java-index,Archived Forums,Java Programming...
# 7

Okay, first, ideally, you'd have your string counting logic completely separate from any GUI, and wouldn't even write any GUI code until the string counting worked. But instructors seem to love to throw GUIs at newbies right off the bat.

Secondly, I wouldn't name a class "Try" because "try" is a keyword in Java.

The message Exception in thread main java.lang.NullPointerException

at Try.<init><Try.java.55>

at Try.main<Try.java.73>

Tells you that when line 73 called line 55 you tried to dereference a null pointer. For example: String str = null;

int len = str.length();

You can't access any methods or fields of null, as it doesn't have any methods or fields. It is, essentially, nothing.

So look at line 55 and see what could possibly be null there. If you can't figure it out, post again and let us know which line is 55.

jverd at 2007-7-5 21:32:27 > top of Java-index,Archived Forums,Java Programming...
# 8

> first of all i did not take exeption handling or util

> package or data structure , so that no 1 insults me.

> O.K ?

What do you mean you didn't "take" them? I can't imagine a whole class just for java exception handling or the util package...there are some things where you just have to read the magazine articles...

Anyway.

> it gives me an exeption

>

> Exception in thread main

> java.lang.NullPointerException

> at Try.<init><Try.java.55>

> at Try.main<Try.java.73>

This means:

On line 73, in your main method, there's a call to Try's constructor.

On line 55, in Try's constructor, there's a null pointer exception.

Look on line 55. Look for a period (.). Something to the left of it is null.

You're the one with the original source code and an editor. It should be trivially simple for you to find line 55, whereas it's a pain for anyone reading your source on this forum.

paulcw at 2007-7-5 21:32:27 > top of Java-index,Archived Forums,Java Programming...
# 9
Most homework I see assigned here is depressing. It doesnt teach them anything but syntax and API (which can be easily looked up)
Kraythe at 2007-7-5 21:32:27 > top of Java-index,Archived Forums,Java Programming...
# 10

> I'd recommend that you use the -g option on javac.exe

> when you compile. It adds debugging information to

> the .class file, including the line numbers.

>

> Open your Try.java, turn on line number display in

> your editor, go to line 55, and see what references

> are being used and why they might be null.

>

> This suggests that you didn't initialize something in

> the constructor.

>

> NPE should be one of the easiest exceptions to figure

> out. This will be a good chance to learn how to do

> it.

>

> But look at this code:

>

> > area.append(poprage); // You reference the area

> object here

>

> c.add(field);

>

> area = new JTextArea(10,30); // You initialize the

> reference here

> area.setEditable(false);

>

>

> Move the area.append line so it follows the call to

> "new".

>

> %

Yep .. that is the solution to his original post.

Kraythe at 2007-7-5 21:32:27 > top of Java-index,Archived Forums,Java Programming...
# 11
did not help & still get the infinite loop !!
pop-rage at 2007-7-5 21:32:27 > top of Java-index,Archived Forums,Java Programming...
# 12

> Most homework I see assigned here is depressing. It

> doesnt teach them anything but syntax and API (which

> can be easily looked up)

Well they have to start somewhere.

And come to think of it, when they're first starting, that's probably when they feel the most lost and are most likely to give up and ask for help on trivial matters.

paulcw at 2007-7-5 21:32:27 > top of Java-index,Archived Forums,Java Programming...
# 13
> did not help & still get the infinite loop !!You're still getting a null pointer exception? The same null pointer exception, on the same lines?
paulcw at 2007-7-5 21:32:27 > top of Java-index,Archived Forums,Java Programming...
# 14
> did not help & still get the infinite loop !!Did you trace the loop with a debugger? Please paste in the updated code. (and this time please run a code formatter first.)
Kraythe at 2007-7-5 21:32:27 > top of Java-index,Archived Forums,Java Programming...
# 15

> did not help & still get the infinite loop !!

Wait a second -

if you're in an infinite loop, that means the exception ISN'T being thrown, otherwise your execution would be over.Right? You're probably getting an infinite loop because of another correction that you tried to stick in there. Take whatever you did out and try again.

No insult intended, pop-rage, but if you thrash around like this you'll never get anywhere.

More important than fixing this trivial problem, do you understand how I arrived at the recommendation I did? That's the thing that will serve you well long after this trivial problem is forgotten.

%

duffymoa at 2007-7-19 21:20:46 > top of Java-index,Archived Forums,Java Programming...
# 16
Author: paulcw no paulcw am done with the exception i moved the sentence & the exception was over but now it does not work ?it seems as i sai before "an infinite loop" .am really lost .
pop-ragea at 2007-7-19 21:20:46 > top of Java-index,Archived Forums,Java Programming...
# 17

What do you need a Swing UI for? You can write this to work on command line input first.

Your whole problem stems from an unwillingness to break a complex thing into simpler chunks.

You don't need the Swing UI here. Throw it away until you get that essential part working on the command line.

%

duffymoa at 2007-7-19 21:20:46 > top of Java-index,Archived Forums,Java Programming...
# 18
poprage: Do they teach you guys how to use a debugger and a quality IDE like eclipse these days or are you still working with command line and text editor?
Kraythea at 2007-7-19 21:20:46 > top of Java-index,Archived Forums,Java Programming...
# 19

> What do you need a Swing UI for? You can write this

> to work on command line input first.

>

> Your whole problem stems from an unwillingness to

> break a complex thing into simpler chunks.

>

> You don't need the Swing UI here. Throw it away until

> you get that essential part working on the command

> line.

>

> %

Its, quite obviously, an assignment man. =)

Kraythea at 2007-7-19 21:20:46 > top of Java-index,Archived Forums,Java Programming...
# 20
"... am done with the exception..."Because of the earlier advice you were given?Are you addressing your questions to paulcw now because he's "nice"?%
duffymoa at 2007-7-19 21:20:46 > top of Java-index,Archived Forums,Java Programming...
# 21

> Its, quite obviously, an assignment man. =)

Obviously.

But get the essential part of the assignment working and THEN put it in the Swing UI. Once you have that working, you can concentrate on the Swing part.

Divide and conquer, baby. That's what computer science (and all problem solving) is all about.

%

duffymoa at 2007-7-19 21:20:46 > top of Java-index,Archived Forums,Java Programming...
# 22

> Do they teach you guys how to use a debugger and a

> quality IDE like eclipse these days or are you still

> working with command line and text editor?

Gawd, don't recommend Eclipse. He doesn't know Java yet.It'll just be two complex things he doesn't know. CLASSPATH is hard enough without throwing Eclipse in the mix.

%

duffymoa at 2007-7-19 21:20:46 > top of Java-index,Archived Forums,Java Programming...
# 23

> no paulcw am done with the exception i moved the

> sentence & the exception was over but now it does not

> work ?

Well then as others have said, the previous change(s) did work and now you're on to another problem (a problem that you always had but you didn't notice before because of the NullPointerException).

You thought that you'd just type in your code and it would work fine, didn't you? Ah, the classic first-timer panic is setting in now...

Get used to it. Most of your time as a programmer you're going to be hunting down bugs. If you're a professional programmer, you'll spend more time in boring meetings, and after that you'll spend a lot of time trying decode cryptic business-ese documents. But after that you'll be debugging. Don't be discouraged if your code doesn't work at first...it never does...

> it seems as i sai before "an infinite loop" .

How do you know it's an infinite loop? Did you run it through a debugger? Are you printing some kind of trace?

paulcwa at 2007-7-19 21:20:46 > top of Java-index,Archived Forums,Java Programming...
# 24
Eclipse is quite easy to use right out of the box. I managed to explain it to my 14 year old brother (who is just starting) in abotu half an hour.
Kraythea at 2007-7-19 21:20:46 > top of Java-index,Archived Forums,Java Programming...
# 25

>

> Author: paulcw

>

> no paulcw am done with the exception i moved the

> sentence & the exception was over but now it does not

> work ?

>

> it seems as i sai before "an infinite loop" .

>

> am really lost .

Post the revised code.

Kraythea at 2007-7-19 21:20:46 > top of Java-index,Archived Forums,Java Programming...
# 26
i hope the new nick is treating you well./k1
komonea at 2007-7-19 21:20:46 > top of Java-index,Archived Forums,Java Programming...
# 27

> Are you addressing your questions to paulcw now

> because he's "nice"?

No need for insults, duffymo.

I'm not nice. I'm a real bastard, trying to get on the next "most arrogant poster" list. Listen:

STOMP! iiiieeeee...

I just stomped on an adorable puppy. That's how big a bastard I am.

paulcwa at 2007-7-19 21:20:46 > top of Java-index,Archived Forums,Java Programming...
# 28
LOL!You've convinced me, paulcw. You move to the top of the arrogant list!(Please don't hurt me....or my basket of puppies)%
duffymoa at 2007-7-19 21:20:46 > top of Java-index,Archived Forums,Java Programming...
# 29

> Eclipse is quite easy to use right out of the box. I

> managed to explain it to my 14 year old brother (who

> is just starting) in abotu half an hour.

I agree that Eclipse can be pretty easy to use.

CLASSPATH is the #1 newbie problem that I see.

Failing to understand that Eclipse ignores the system CLASSPATH and demands that users add JARs to the build path in an Eclipse-specific way is another classic newbie problem.

Maybe pop-rage and your little brother will get away with it because the simple classes they're writing won't require 3rd party JARs. (OR they're both smart enough to figure out CLASSPATH.) If those are both true, then I withdraw my comment.

But the fact that pop-rage is having problems figuring out a NPE problem suggests to me that Eclipse will just muddy the waters. You don't need a debugger to figure out a NPE. (Might help with that infinite loop, though.) A few well-placed System.out.println statements might get him there, too. Shouldn't need a fancy debugger to figure out this problem.

I'll be happy to be proven wrong.

%

duffymoa at 2007-7-19 21:20:46 > top of Java-index,Archived Forums,Java Programming...
# 30

Well that is just how to use eclipse. Not a big deal when you teach it right.

Not a slam or anything, just as a comment I wish to make the follwoing observation:

As for labeling people "Arrogant" that is usually a synonym for "Successful and Correct" and so I take it as a compliment. Usually it is those calling others arrogant that have the problem (self confidence defficiency) and not the "arrogant" one.

Kraythea at 2007-7-19 21:20:50 > top of Java-index,Archived Forums,Java Programming...
# 31
As for pop-rage .. he is just in last minute panic and flailing around a bit. Actually tools like eclipse would help him a great deal because they would give him more power to attack the issue and learn why it is hapening.
Kraythea at 2007-7-19 21:20:50 > top of Java-index,Archived Forums,Java Programming...
# 32

> As for labeling people "Arrogant" that is usually a

> synonym for "Successful and Correct" and so I take it

> as a compliment. Usually it is those calling others

> arrogant that have the problem (self confidence

> defficiency) and not the "arrogant" one.

Yeah I was making a joke about that "most arrogant poster" list that some sad angry person put up a couple times in the past. The people highest on the list end up being the people who give the best answers (but don't do others' homework for them). They also put a fair amount of effort into answering questions on this forum as well. So view "arrogant" with a sense of irony here.

paulcwa at 2007-7-19 21:20:50 > top of Java-index,Archived Forums,Java Programming...
# 33

> Well that is just how to use eclipse. Not a big deal when you teach it right.

Agreed.

> Not a slam or anything, just as a comment I wish to

> make the follwoing observation:

>

> As for labeling people "Arrogant" that is usually a

> synonym for "Successful and Correct" and so I take it

> as a compliment. Usually it is those calling others

> arrogant that have the problem (self confidence

> defficiency) and not the "arrogant" one.

I'm not sure if you know about iamsomeoneelse and his infamous "most arrogant" list, but that's what paulcw and I are joking about. I'm not calling him "arrogant" in the least.

%

duffymoa at 2007-7-19 21:20:50 > top of Java-index,Archived Forums,Java Programming...
# 34

try {

throw AnnihilatorException();

} catch {

throw AnnihilatorException();

}

komonea at 2007-7-19 21:20:50 > top of Java-index,Archived Forums,Java Programming...
# 35

> As for labeling people "Arrogant" that is usually a

> synonym for "Successful and Correct" and so I take it

> as a compliment. Usually it is those calling others

> arrogant that have the problem (self confidence

> defficiency) and not the "arrogant" one.

The whole arrogant thing was in reference to the "most arrogant" list posted periodically by "iamsomeoneelse." Sorry for wasting your time if you knew that already. Personally I am sharply disappointed that I fell off the list this time. I think if you post on here enough you might have a shot at getting on there. I know it's one of my goals.

nasch_a at 2007-7-19 21:20:50 > top of Java-index,Archived Forums,Java Programming...
# 36
Yikes, one minute late and I'm already duplicating two others' work.
nasch_a at 2007-7-19 21:20:51 > top of Java-index,Archived Forums,Java Programming...
# 37
} finally {throw new AnnihilatorException();}
komonea at 2007-7-19 21:20:51 > top of Java-index,Archived Forums,Java Programming...
# 38

> As for pop-rage .. he is just in last minute panic and

> flailing around a bit. Actually tools like eclipse

> would help him a great deal because they would give

> him more power to attack the issue and learn why it is

> hapening.

The best tool he has for attacking this problem should be between his ears.

%

duffymoa at 2007-7-19 21:20:51 > top of Java-index,Archived Forums,Java Programming...
# 39

> > As for pop-rage .. he is just in last minute panic

> and

> > flailing around a bit. Actually tools like eclipse

> > would help him a great deal because they would give

> > him more power to attack the issue and learn why it

> is

> > hapening.

>

> The best tool he has for attacking this problem should

> be between his ears.

>

> %

>

Typing with the nose helps programming? Ill try that. Thanks. =)

Kraythea at 2007-7-19 21:20:51 > top of Java-index,Archived Forums,Java Programming...
# 40

> } finally {

> throw new AnnihilatorException();

> }

Bad komone, bad. Never throw or return from finally. Go borrow yawmark's fish and slap yourself with it. ;-)

jverda at 2007-7-19 21:20:51 > top of Java-index,Archived Forums,Java Programming...
# 41
> Typing with the nose helps programming? Ill try that.> Thanks. =)Well, you can't go as fast, so it provides opportunity and incentive to think before typing.
jverda at 2007-7-19 21:20:51 > top of Java-index,Archived Forums,Java Programming...
# 42
You mistake the intention. In this case it was justified!/k1
komonea at 2007-7-19 21:20:51 > top of Java-index,Archived Forums,Java Programming...