• Post Reply Bookmark Topic Watch Topic
  • New Topic

Exception on first run  RSS feed

 
Liz Hundy
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

When I compile one of my classes and then run my program I get a null-pointer exception. The second and subsequent times that I run it there is no problem.

What could cuase this kind of behaviour?

---Liz
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you post the code?
 
Liz Hundy
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code is rather big. The error occurs in TableLayout (by Phil Herold) that I downloaded from the web. http://java.sys-con.com/read/163311.htm

Perhaps I should just try a different LayoutManager.

But I was curious to know what kind of code could generate an error on first run only.

---Liz
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The stack trace should tell you on which it occurs. When you look at that line, what is on it?
 
Liz Hundy
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Keith, for being so patient with me.

Yes, I can look at the line but it makes no sense to me at all.
It is
preferredSize.width = insets.left + insets.right + ((preferredColumnWidths.length - 1) * hGap);

in the layoutContainer method of TableLayout.

All these things are local or private variables inside TableLayout, so how can re-compiling the code make any difference? How can the fact that I have previously run the program stop them from crashing?

Well I guess there is not an easy answer. Thanks for your time.

---Liz
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the code is making modifications to Swing objects from the main application thread, the problem could simply be a race condition. When you run the application, the class files get read into your disk cache, so the second time, it starts up faster, and the same problem doesn't occur. Subtle timing changes can wreak all kinds of havoc in multithreaded programs (which any Java GUI-based program is.)

Any code that makes changes to a Swing components that might be showing on the screen must be executed on the GUI thread itself -- i.e., in an event handler. In practice, it's safe to fiddle with components in main() until you call setVisible() -- after that, it becomes dangerous.

Does this sound like a possibility?
 
Liz Hundy
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ernest Friedman-Hill:
...
Does this sound like a possibility?


Yes, a possibility, even probable. It never crossed my mind that there might be more than one thread involved.

So a second thread is starting before the constructor of my main class has finished executing.

I do not know anything about threads so I will go and do a bit of research. In the mean time any hints on how to tell what threads are running, and what might invoke them in the first place would be very welcome.

Thanks for your help.

---Liz
Still confused, but following a thread.
 
Liz Hundy
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Got it - I think. I was adding a panel before I put the components inside it. Now the TableLayout method is on thread main, not AWT-EventQueue-0.

Thanks everyone for your help.

---Liz
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!