0

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.

import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ButtonGroup;
import javax.swing.JComponent;
import javax.swing.JProgressBar;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class ProgressMonitor extends Thread{

    JProgressBar progBar ;
    Object[] component ;

    public ProgressMonitor(JProgressBar progBar, Object... component){
        this.component = component;
        this.progBar = progBar;
    }

    @Override
    public void run() {
        progMon(progBar, component);
        try {
            sleep(2000);
        } catch (InterruptedException ex) {
            Logger.getLogger(ProgressMonitor.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void progMon(JProgressBar progBar, Object... component){
        int items = component.length;
        float incVal = 100/items;
        float progVal = 0;

        for (int i = 0; i < component.length ; i++) {
            if(component[i].getClass().equals(JTextField.class)){
                if(!((JTextField)component[i]).getText().trim().equals("")){
                    progVal += incVal;
                }
            }else if(component[i].getClass().equals(JTextArea.class)){
                if(!((JTextArea)component[i]).getText().trim().equals("")){
                    progVal += incVal;
                }
            }else if(component[i].getClass().equals(ButtonGroup.class)){
                if(((ButtonGroup)component[i]).getSelection() != null){
                    progVal += incVal;
                }
            }



        }
        System.out.println(Math.round(progVal));
        progBar.setValue(Math.round(progVal));
    }

}

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;

public class progress extends javax.swing.JFrame {

    /**
     * Creates new form progress
     */
    public progress() {
        initComponents();

    }

    private void initComponents() {

        jProgressBar1 = new javax.swing.JProgressBar();
        jTextField1 = new javax.swing.JTextField();
        jTextField2 = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jTextField2.setText(" ");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(124, 124, 124)
                        .addComponent(jProgressBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 432, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(212, 212, 212)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jTextField1)
                            .addComponent(jTextField2, javax.swing.GroupLayout.DEFAULT_SIZE, 222, Short.MAX_VALUE))))
                .addContainerGap(110, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(38, 38, 38)
                .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 137, Short.MAX_VALUE)
                .addComponent(jProgressBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(32, 32, 32))
        );

        pack();
    }                      

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {

        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {

                new progress().setVisible(true);
            }

        });

        new ProgressMonitor(jProgressBar1, jTextField1, jTextField2).start();
    }


    public static javax.swing.JProgressBar jProgressBar1;
    public static javax.swing.JTextField jTextField1;
    public static javax.swing.JTextField jTextField2;

}
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
Tharindu Sathischandra
  • 1,654
  • 1
  • 15
  • 37
  • Look at the line throwing the exception -- check the variables **carefully**, and you'll see what you're doing wrong. Hint: look up [variable shadowing and NullPointerException](https://www.google.com/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=site:stackoverflow.com+java+variable+shadowing+nullpointerexception) for more on this. – Hovercraft Full Of Eels Oct 02 '16 at 17:24
  • Also understand that the heuristic for NullPointerExceptions is almost always the same: You should critically read your exception's stacktrace to find the line of code at fault, the line that throws the exception, and then inspect that line carefully, find out which variable is null, and then trace back into your code to see why. You will run into these again and again, trust me. – Hovercraft Full Of Eels Oct 02 '16 at 17:30

0 Answers0