• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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;

 
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Saloon Keeper
Posts: 15491
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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().
     
    reply
      Bookmark Topic Watch Topic
    • New Topic