Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Error in Progress Bar

  1. #1
    Junior Member
    Join Date
    Sep 2014
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Error in Progress Bar

    Hi Guys having a error in progress bar ...

    i want to see the progress upon checking the sha1 of files

    this is the code:

    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
    package program1;
     
    /**
     *
     * @author JosephP
     */
    //Import packages
    import javax.swing.*;
    import java.awt.*;
     
    import java.awt.event.*;
     
    public class Program1{ //Main class
    	//Declare GUI components
    	static JFrame frmMain;
    	static Container pane;
    	static JButton btnDo;
    	static JProgressBar barDo;
     
    	public static void main (String[] args){ //Main void
    		//Set Look and Feel
    		try {UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());}
    		catch (Exception e) {}
     
    		//Create all components
    		frmMain = new JFrame("Sample progress bar application");
    		frmMain.setSize(300, 100); //Window size 300x100 pixels
    		pane = frmMain.getContentPane();
    		pane.setLayout(null); //Use the null layout
    		frmMain.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Exit when X is clicked
    		btnDo = new JButton("Go!");
    		barDo = new JProgressBar(0, 100); //Min value: 0 Max value: 100	
     
    		//Add components to pane
    		pane.add(btnDo);
    		pane.add(barDo);
     
    		//Position controls (X, Y, width, height)
    		barDo.setBounds(10, 10, 280, 20);
    		btnDo.setBounds(100, 35, 100, 25);
     
    		//Make frame visible
    		frmMain.setResizable(false); //No resize
    		frmMain.setVisible(true);
     
    		//Add action listeners
    		btnDo.addActionListener(new btnDoAction()); //Add the button's action
    	}
     
    	//The action
    	public static class btnDoAction implements ActionListener{
    		public void actionPerformed (ActionEvent e){
    			new Thread(new thread1()).start(); //Start the thread
    		}
    	}
     
    	//The thread
    	public static class thread1 implements Runnable{
    		public void run(){
    			for (int i=0; i<=100; i++){ //Progressively increment variable i
    				barDo.setValue(i); //Set value
    				barDo.repaint(); //Refresh graphics
    				try{Thread.sleep(50);} //Sleep 50 milliseconds
    				catch (InterruptedException err){}
    			}
    		}
    	}
    }

    this is the error

    Exception in thread "Thread-2" java.lang.NullPointerException
    	at program1.ProgressBar$thread1.run(ProgressBar.java:98)
    	at java.lang.Thread.run(Thread.java:744)


  2. #2
    Junior Member
    Join Date
    Sep 2014
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default WHATS WRONG IN THIS CODE FOR MY PROGRESS BAR

    hi guys can you help me what is wrong in this program

    i want to have a progress bar when getting the sha1 of all files in a folder

    this is the code :

    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
    package program1;
     
    /**
     *
     * @author JosephP
     */
    //Import packages
    import javax.swing.*;
    import java.awt.*;
     
    import java.awt.event.*;
     
    public class Program1{ //Main class
    	//Declare GUI components
    	static JFrame frmMain;
    	static Container pane;
    	static JButton btnDo;
    	static JProgressBar barDo;
     
    	public static void main (String[] args){ //Main void
    		//Set Look and Feel
    		try {UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());}
    		catch (Exception e) {}
     
    		//Create all components
    		frmMain = new JFrame("Sample progress bar application");
    		frmMain.setSize(300, 100); //Window size 300x100 pixels
    		pane = frmMain.getContentPane();
    		pane.setLayout(null); //Use the null layout
    		frmMain.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Exit when X is clicked
    		btnDo = new JButton("Go!");
    		barDo = new JProgressBar(0, 100); //Min value: 0 Max value: 100	
     
    		//Add components to pane
    		pane.add(btnDo);
    		pane.add(barDo);
     
    		//Position controls (X, Y, width, height)
    		barDo.setBounds(10, 10, 280, 20);
    		btnDo.setBounds(100, 35, 100, 25);
     
    		//Make frame visible
    		frmMain.setResizable(false); //No resize
    		frmMain.setVisible(true);
     
    		//Add action listeners
    		btnDo.addActionListener(new btnDoAction()); //Add the button's action
    	}
     
    	//The action
    	public static class btnDoAction implements ActionListener{
    		public void actionPerformed (ActionEvent e){
    			new Thread(new thread1()).start(); //Start the thread
    		}
    	}
     
    	//The thread
    	public static class thread1 implements Runnable{
    		public void run(){
    			for (int i=0; i<=100; i++){ //Progressively increment variable i
    				barDo.setValue(i); //Set value
    				barDo.repaint(); //Refresh graphics
    				try{Thread.sleep(50);} //Sleep 50 milliseconds
    				catch (InterruptedException err){}
    			}
    		}
    	}
    }


    this is the result

     Exception in thread "Thread-2" java.lang.NullPointerException
    	at program1.ProgressBar$thread1.run(ProgressBar.java:98)
    	at java.lang.Thread.run(Thread.java:744)

    thanks

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,407
    Thanks
    53
    Thanked 2,399 Times in 2,352 Posts

    Default Re: Error in Progress Bar

    Duplicate threads merged.

    Exception in thread "Thread-2" java.lang.NullPointerException
    at program1.ProgressBar$thread1.run(ProgressBar.java: 98)
    What variable on line 98 has a null value? Then backtrack to see why the value is null.
    If you don't understand my answer, don't ignore it, ask a question.

  4. #4
    Junior Member
    Join Date
    Sep 2014
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Error in Progress Bar

    this is the code in variable 98

    barDo.setValue(i);

    this is in the

    for (int i=0; i<=100; i++){ //Progressively increment variable i
    barDo.setValue(i); //Set value
    barDo.repaint(); //Refresh graphics
    DeduplicateFiles(filepaths);
    try{Thread.sleep(50);} //Sleep 50 milliseconds
    catch (InterruptedException err){}
    }

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,407
    Thanks
    53
    Thanked 2,399 Times in 2,352 Posts

    Default Re: Error in Progress Bar

    What variable has the null value that is causing the NullPointerException? The value in that variable is what needs to be fixed.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Junior Member
    Join Date
    Sep 2014
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Error in Progress Bar

    variable is 1 because he calling in the loop

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,407
    Thanks
    53
    Thanked 2,399 Times in 2,352 Posts

    Default Re: Error in Progress Bar

    The NullPointerException is caused by a null value somewhere. You need to find where the null value is and change the code to get rid of the null value.

    variable is 1
    What variable is that?
    A value of 1 is not null.

    You need to post the code that has the error. The posted code has 74 lines, but the error message is on line 98. That means the posted code did not cause the error.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. progress bar in primefaces
    By feli in forum What's Wrong With My Code?
    Replies: 0
    Last Post: July 18th, 2013, 02:16 AM
  2. Progress bar
    By tardis_ in forum AWT / Java Swing
    Replies: 2
    Last Post: June 25th, 2013, 03:15 PM
  3. Progress Bar
    By h9000458359@gmail.com in forum Java Theory & Questions
    Replies: 4
    Last Post: December 26th, 2012, 01:04 PM
  4. [Swing-Progress Bar] Can't resize progress bar
    By Grabar in forum What's Wrong With My Code?
    Replies: 0
    Last Post: March 14th, 2010, 12:27 PM
  5. Progress bar while uploading a file in web application
    By jazz2k8 in forum AWT / Java Swing
    Replies: 4
    Last Post: July 8th, 2008, 07:32 AM