PDA

View Full Version : URGENT Java swing/awt help



Luke has no name
December 3rd, 2008, 09:06 PM
I have a final due TODAY on a similar project, and I'm getting runtime errors. Put in text for first and last name, and a number between 40 and 50 for the hours worked.

This is legal, you aren't helping me cheat in a class, I promise. But My issue with the final is giving a similar issue.

My email is lukehasnoname@gmail.com if you do not have a UF account.

jdong
December 3rd, 2008, 09:14 PM
Sigh, homework help is really not encouraged here. It's a homework assignment for you to do, and the process helps you learn. I will reluctantly keep the thread open here, but please don't have someone else give you an obvious answer.


If you can provide a helpful hint towards the correct answer, that's somewhat more appropriate, but I suggest the OP to consult his course help resources if this really isn't cheating.

Luke has no name
December 3rd, 2008, 09:23 PM
The assignment I posted is something I've already turned in. A current assignment also contains editing a JTextArea, and my professor didn't even know exactly what the problem was.

I'm getting a NullPointerException when appending a String to a JTextArea. Anyone know how that can happen?

jdong
December 3rd, 2008, 09:27 PM
Sorry, that hint was a bit mean.

Here's a better hint:

Have you tried using a debugger to follow the code path?

Luke has no name
December 3rd, 2008, 09:40 PM
Ya, but the path is a bit obscure, and my experience with swing and the java debugger are both not up to par...

My current plan is to continue programming the new project and hope to God I don't run into obscure exceptions. Maybe this code will be better than the old stuff.

jdong
December 3rd, 2008, 09:43 PM
Ah, but the debugger REALLY is your friend. Let's learn how to use it.

For reference, here is the stacktrace:


Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at lab65804.MyGui$ButtonHandler.actionPerformed(MyGui .java:124)
at javax.swing.AbstractButton.fireActionPerformed(Abs tractButton.java:2012)
at javax.swing.AbstractButton$Handler.actionPerformed (AbstractButton.java:2335)
at javax.swing.DefaultButtonModel.fireActionPerformed (DefaultButtonModel.java:404)
at javax.swing.DefaultButtonModel.setPressed(DefaultB uttonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(BasicButtonListener.java:253)
at java.awt.Component.processMouseEvent(Component.jav a:6108)
at javax.swing.JComponent.processMouseEvent(JComponen t.java:3276)
at java.awt.Component.processEvent(Component.java:587 3)
at java.awt.Container.processEvent(Container.java:210 5)
at java.awt.Component.dispatchEventImpl(Component.jav a:4469)
at java.awt.Container.dispatchEventImpl(Container.jav a:2163)
at java.awt.Component.dispatchEvent(Component.java:42 95)
at java.awt.LightweightDispatcher.retargetMouseEvent( Container.java:4461)
at java.awt.LightweightDispatcher.processMouseEvent(C ontainer.java:4125)
at java.awt.LightweightDispatcher.dispatchEvent(Conta iner.java:4055)
at java.awt.Container.dispatchEventImpl(Container.jav a:2149)
at java.awt.Window.dispatchEventImpl(Window.java:2478 )
at java.awt.Component.dispatchEvent(Component.java:42 95)
at java.awt.EventQueue.dispatchEvent(EventQueue.java: 604)
at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:275)
at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:200)
at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:190)
at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:185)
at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:177)
at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:138)



I bolded the only part you should pay attention to. Now, do you want to pick a good place to set a breakpoint and take a closer look?


(NOTE: I may have been mean enough to change your code so that my stack trace looks different from yours. But use your head.)

jdong
December 6th, 2008, 04:00 AM
So, have you had a chance to figure it out yet? :)

geirha
December 6th, 2008, 01:32 PM
If you can spot the problem in this code, you'll be able to spot the problem in your own code.


public class Test
{
private String a;

public Test()
{
String a= "Hello, World!";
}
public String toString()
{
return a;
}

public static void main(String args[])
{
Test t= new Test();
System.out.println(t);
}
}

Reiger
December 6th, 2008, 04:21 PM
Ooh I love those. Usually it's the other way around for me. (Complaining about duplicate variable names, preferably of different types.) :p

jdong
December 6th, 2008, 04:42 PM
If you can spot the problem in this code, you'll be able to spot the problem in your own code.


public class Test
{
private String a;

public Test()
{
String a= "Hello, World!";
}
public String toString()
{
return a.toString();
}

public static void main(String args[])
{
Test t= new Test();
System.out.println(t);
}
}



Now it even throws the same exception in the same fashion :)

drubin
December 7th, 2008, 10:53 PM
If you can spot the problem in this code, you'll be able to spot the problem in your own code.
[code]


Nice example, But seriously these types of errors can be stared at for ages with out seeing it ;p.

/me <3 netbeans's informational messages for this

jdong
December 7th, 2008, 10:56 PM
Nice example, But seriously these types of errors can be stared at for ages with out seeing it ;p.

/me <3 netbeans's informational messages for this

Back to Luke, setting a breakpoint at the beginning of the event handler and mousing over jtaOutput would've told you it was null. When you know *how* to look for it, it's really apparent. The problem IMO is a lot of people really don't know how to debug... reading your code like proofreading an English paper is an extremely ineffective form of troubleshooting, but that's what most people tend to do!