Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Null pointer exception when parameter passing to a subclass of thread class

 
Tharindu Sathischandra
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a java application which having a registration form which includes some JTextFields.

When I have filled some number of text fields, a progress bar should show the percentage of the amount of text fields filled.

So I made a class that receives parameters for progress bar, and objects (which will be cast to JTextField) as follows.


But when I constructed object of this class as,

new ProgressMonitor(jProgressBar1, jTextField1, jTextField2).start();
I got the error,

Exception in thread "Thread-1" java.lang.NullPointerException
The class which I called the method is as follows;

 
Campbell Ritchie
Sheriff
Posts: 51432
87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You appear never to have posted code, so you don't know about the code tags, which I added to your code, and doesn't it look better Always use the code tags.
I would have sorted out the lines that are too long or the excess empty lines, but left them unchanged so as to maintain your line numbers. The long lines are caused by your using the automatic GUI builder on your IDE.
Please supply the full stack trace which will allow you to work out why you are suffering that exception. The cure for null pointer exceptions is to replace the null by something “real”.
I can see at least three other errors.
Why have you got static fields at the end of the second code block?
Why are you creating a percentage by dividing 100? You will almost certainly get the wrong result because of the conventions of integer arithmetic.
Why are you using Thread#sleep? Which thread is that sleep call on?

The correct Java® style is for all class names to start with CapitalLetters.
It is usually bad design to extend GUI classes.
 
Stephan van Hulst
Bartender
Pie
Posts: 6503
83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Besides the comments Campbell made, here are a few more:

  • Classes shouldn't extend Thread.
  • Your fields should be private, and if possible final.
  • Your classes should be strongly typed. Avoid declaring variables of type Object.
  • If you have to expect the types of objects to perform logic, you're asking for trouble. Don't use getClass().
  • Don't call pack() from your constructor, directly or indirectly. Your GUI class should have a separate method in which you display the GUI.
  • If you're on Java 8, you can a lambda instead of an anonymous Runnable.

  • For the last two points, this is what it would look like:

    The show() method should call pack()/setSize() and setVisible().
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic