Home arrow static arrow Java Programming [Archive] - again i need help guys
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - again i need help guys
This topic has 42 replies on 3 pages.    1 | 2 | 3 | Next »

Posts:29
Registered: 11/7/03
again i need help guys  
Aug 9, 2004 1:12 PM



 
this time with a dead line {tonight} :(

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

i 2
am 2
user 2

this is the code

import java.awt.*;import java.awt.event.*;import javax.swing.*;import java.util.*; public class Try extends JFrame {		private JLabel label;	private JTextField field;	private JTextArea area;	private JScrollPane scroll;	private int 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(){								public void 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();	}			public static void main (String ar[]){				Try a = new Try();				a.addWindowListener(						new WindowAdapter(){								public void 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.
 

Posts:458
Registered: 7/1/03
Re: again i need help guys  
Aug 9, 2004 1:16 PM (reply 1 of 42)



 
Which lines are in the stack trace .. I dotn feel like counting up to 73 in that code.
 

Posts:29
Registered: 11/7/03
Re: again i need help guys  
Aug 9, 2004 1:18 PM (reply 2 of 42)



 
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 :D

Regards.
 

Posts:1,085
Registered: 4/14/03
Re: again i need help guys  
Aug 9, 2004 1:18 PM (reply 3 of 42)



 
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.
 

Posts:21,718
Registered: 98-02-20
Re: again i need help guys  
Aug 9, 2004 1:19 PM (reply 4 of 42)



 
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".

%
 

Posts:29
Registered: 11/7/03
Re: again i need help guys  
Aug 9, 2004 1:20 PM (reply 5 of 42)



 
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) ??
 

Posts:458
Registered: 7/1/03
Re: again i need help guys  
Aug 9, 2004 1:20 PM (reply 6 of 42)



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

Posts:37,103
Registered: 3/30/99
Re: again i need help guys  
Aug 9, 2004 1:20 PM (reply 7 of 42)



 
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.NullPointerExceptionat 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.
 

Posts:12,831
Registered: 2/22/00
Re: again i need help guys  
Aug 9, 2004 1:24 PM (reply 8 of 42)



 
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.
 

Posts:458
Registered: 7/1/03
Re: again i need help guys  
Aug 9, 2004 1:24 PM (reply 9 of 42)



 
Most homework I see assigned here is depressing. It doesnt teach them anything but syntax and API (which can be easily looked up)
 

Posts:458
Registered: 7/1/03
Re: again i need help guys  
Aug 9, 2004 1:25 PM (reply 10 of 42)



 
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 areaobject here c.add(field); area = new JTextArea(10,30); // You initialize thereference herearea.setEditable(false);


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

%

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

Posts:29
Registered: 11/7/03
Re: again i need help guys  
Aug 9, 2004 1:26 PM (reply 11 of 42)



 
did not help & still get the infinite loop !!
 

Posts:12,831
Registered: 2/22/00
Re: again i need help guys  
Aug 9, 2004 1:28 PM (reply 12 of 42)



 
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.
 

Posts:12,831
Registered: 2/22/00
Re: again i need help guys  
Aug 9, 2004 1:29 PM (reply 13 of 42)



 
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?
 

Posts:458
Registered: 7/1/03
Re: again i need help guys  
Aug 9, 2004 1:34 PM (reply 14 of 42)



 
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.)
 
This topic has 42 replies on 3 pages.    1 | 2 | 3 | Next »