Results 1 to 6 of 6
Like Tree2Likes
  • 2 Post By aortell24

Thread: Null pointer Exception

  1. #1
    aortell24 is offline Member
    Join Date
    Jun 2012
    Location
    St.louis, Missouri
    Posts
    49
    Rep Power
    0

    Default Null pointer Exception

    Hello everyone I am getting a null pointer exception Pointing to line 57 of my Exercise29_15 class which is adding a property change listener.Any help would be greatly appreciated.Thank you.

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.beans.PropertyChangeEvent;
    import java.beans.PropertyChangeListener;
    import java.io.*;
    /**
     * Created by IntelliJ IDEA.
     * User: aortell24
     * Date: 7/17/12
     * Time: 10:07 AM
     * To change this template use File | Settings | File Templates.
     */
    public class Exercise29_15 extends JFrame{
        JLabel label1 = new JLabel();
        JLabel label2 = new JLabel();
        JTextField textField1 = new JTextField();
        JTextField textField2 = new JTextField();
        JButton browseBtn = new JButton();
        JButton startBtn = new JButton();
        JPanel panel1 = new JPanel();
        JPanel panel2 = new JPanel();
        JPanel panel3 = new JPanel();
        File file;
        JMenuBar menuBar = new JMenuBar();
        JProgressBar progressBar = new JProgressBar();
        SplitFileTask task; 
    
        public Exercise29_15()
        {
            this.setLayout(new GridLayout(3, 0));
            progressBar.setStringPainted(true);
            progressBar.setValue(0);
            progressBar.setMaximum(100);
            this.setJMenuBar(menuBar);
            menuBar.add(progressBar);
            browseBtn.setText("Browse");
            browseBtn.addActionListener(new buttonEventHandler());
            panel1.setLayout(new GridLayout(0,3));
            panel1.add(label1);
            label1.setText("Enter or choose a file:");
            panel1.add(textField1);
            panel1.add(browseBtn);
            this.add(panel1);
            panel2.setLayout(new GridLayout(0, 2));
            label2.setText(("Specify the number of smaller files:"));
            panel2.add(label2);
            panel2.add(textField2);
            this.add(panel2);
            panel3.setLayout(new GridLayout(1, 0));
            startBtn.setText("Start");
            panel3.add(startBtn);
            this.add(panel3);
            startBtn.addActionListener(new buttonEventHandler());
    
            task.addPropertyChangeListener(new PropertyChangeListener()
            {
                @Override
                public void propertyChange(PropertyChangeEvent propertyChangeEvent)
                {
                    if("progress".equals(propertyChangeEvent))
                        progressBar.setValue((Integer)propertyChangeEvent.getNewValue());
                }
            });
    
        }
    
        class buttonEventHandler implements ActionListener
        {
            int numberOfPieces ;
            final JFileChooser fileChooser = new JFileChooser();
    
            public void actionPerformed(ActionEvent actionEvent)
            {
                if(actionEvent.getSource().equals(browseBtn))
                {
                    if (fileChooser.showOpenDialog(null)==JFileChooser.APPROVE_OPTION) {
                        file = fileChooser.getSelectedFile();
                        textField1.setText(file.getAbsolutePath());
    
                    }
                }
                else if(actionEvent.getSource().equals(startBtn))
                {
                    String fileName = (textField2.getText());
                    if(textField1.getText().equals(""))return;
                    numberOfPieces= Integer.parseInt(textField2.getText()) ;
                    task =  new SplitFileTask(fileName,numberOfPieces);
                }
            }
        }
    }
    public class SplitFileTask extends SwingWorker<Integer,Integer> {
        int numberOfPieces;
        JTextField textField1;
        String fileName = "";
    
    
        public SplitFileTask()
        {
        }
        public SplitFileTask(String fileName, int numberOfPieces)
        {
            this.fileName = fileName;
            this.numberOfPieces = numberOfPieces;
        }
        public void splitFile(String fileName, int numberOfPieces)
        {
            try {
                BufferedInputStream input = new BufferedInputStream(
                        new FileInputStream(new File(fileName)));
    
                System.out.println("File size: " + input.available() + " bytes");
                long fileSize = input.available();
                int splitFileSize = (int) Math.ceil(1.0 * fileSize / numberOfPieces);
    
                for (int i = 1; i <= numberOfPieces; i++) {
                    BufferedOutputStream output = new BufferedOutputStream(
                            new FileOutputStream(new File(fileName + "." + i)));
                    int value;
                    int count = 0;
                    while (count++ < splitFileSize && (value = input.read()) != -1) {
                        output.write(value);
                    }
                    output.close();
                }
    
                input.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    
        @Override
        protected Integer doInBackground() throws Exception
        {
            return null;
        }
    }
    public class Application {
        static JProgressBar progressBar = new JProgressBar();
        public static void main(String[] args)
        {
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run()
                {
                    Exercise29_15 frame = new Exercise29_15();
    
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                    frame.pack();
    
                }
            });
        }
    }
    here is the error I am getting:
    Java Code:
      
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    	at Exercise29_15.<init>(Exercise29_15.java:57)
    	at Application$1.run(Application.java:20)
    	at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:226)
    	at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:647)
    	at java.awt.EventQueue.access$000(EventQueue.java:96)
    	at java.awt.EventQueue$1.run(EventQueue.java:608)
    	at java.awt.EventQueue$1.run(EventQueue.java:606)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:105)
    	at java.awt.EventQueue.dispatchEvent(EventQueue.java:617)
    	at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:275)
    	at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:200)
    	at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:190)
    	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:185)
    	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:177)
    	at java.awt.EventDispatchThread.run(EventDispatchThread.java:138)

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,000
    Blog Entries
    7
    Rep Power
    20

    Default Re: Null pointer Exception

    The member variable 'task' doesn't contain a value yet; you have to fix that.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    aortell24 is offline Member
    Join Date
    Jun 2012
    Location
    St.louis, Missouri
    Posts
    49
    Rep Power
    0

    Default Re: Null pointer Exception

    I figured it out Sorry.

  4. #4
    aortell24 is offline Member
    Join Date
    Jun 2012
    Location
    St.louis, Missouri
    Posts
    49
    Rep Power
    0

    Default Re: Null pointer Exception

    Thanks

  5. #5
    aortell24 is offline Member
    Join Date
    Jun 2012
    Location
    St.louis, Missouri
    Posts
    49
    Rep Power
    0

    Default Re: Null pointer Exception

    Thanks for the help on an unrelated topic I am having a hard time trying to pass the file name from a JFilechooser and an Integer value to my splitfile class.I am trying to run the progressbar on a seperate thread this is my first program with multiple threads.Could you give my a few pointers.Thanks .here is what I have add.
    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.beans.PropertyChangeEvent;
    import java.beans.PropertyChangeListener;
    import java.io.*;
    /**
     * Created by IntelliJ IDEA.
     * User: aortell24
     * Date: 7/17/12
     * Time: 10:07 AM
     * To change this template use File | Settings | File Templates.
     */
    public class Exercise29_15 extends JFrame{
        JLabel label1 = new JLabel();
        JLabel label2 = new JLabel();
        JTextField textField1 = new JTextField();
        JTextField textField2 = new JTextField();
        JButton browseBtn = new JButton();
        JButton startBtn = new JButton();
        JPanel panel1 = new JPanel();
        JPanel panel2 = new JPanel();
        JPanel panel3 = new JPanel();
        File file;
        JMenuBar menuBar = new JMenuBar();
        JProgressBar progressBar = new JProgressBar();
        SplitFileTask task;
        final JFileChooser fileChooser = new JFileChooser();
    
        public Exercise29_15()
        {
            this.setLayout(new GridLayout(3, 0));
            progressBar.setStringPainted(true);
            progressBar.setValue(0);
            progressBar.setMaximum(100);
            this.setJMenuBar(menuBar);
            menuBar.add(progressBar);
            browseBtn.setText("Browse");
            browseBtn.addActionListener(new buttonEventHandler());
            panel1.setLayout(new GridLayout(0,3));
            panel1.add(label1);
            label1.setText("Enter or choose a file:");
            panel1.add(textField1);
            panel1.add(browseBtn);
            this.add(panel1);
            panel2.setLayout(new GridLayout(0, 2));
            label2.setText(("Specify the number of smaller files:"));
            panel2.add(label2);
            panel2.add(textField2);
            this.add(panel2);
            panel3.setLayout(new GridLayout(1, 0));
            startBtn.setText("Start");
            panel3.add(startBtn);
            this.add(panel3);
            startBtn.addActionListener(new buttonEventHandler());
            String fileName =  String.valueOf(fileChooser.getSelectedFile());
            
            task =  new SplitFileTask(fileName,Integer.parseInt(textField2.getText()));
            
            task.addPropertyChangeListener(new PropertyChangeListener()
            {
                @Override
                public void propertyChange(PropertyChangeEvent propertyChangeEvent)
                {
                    if("progress".equals(propertyChangeEvent))
                        progressBar.setValue((Integer)propertyChangeEvent.getNewValue());
                }
            });
    
        }
    
        class buttonEventHandler implements ActionListener
        {
            int numberOfPieces ;
            
            public void actionPerformed(ActionEvent actionEvent)
            {
                if(actionEvent.getSource().equals(browseBtn))
                {
                    if (fileChooser.showOpenDialog(null)==JFileChooser.APPROVE_OPTION) {
                        file = fileChooser.getSelectedFile();
                        textField1.setText(file.getAbsolutePath());
    
                    }
                }
                else if(actionEvent.getSource().equals(startBtn))
                {
                    
                    if(textField1.getText().equals(""))return;
                    numberOfPieces= Integer.parseInt(textField2.getText()) ;
                    task(textField1.getText(),numberOfPieces);
                }
            }
        }
    }
    public class SplitFileTask extends SwingWorker<Integer,Integer> {
        int numberOfPieces;
        JTextField textField1;
        String fileName = "";
    
    
        public SplitFileTask()
        {
        }
        public SplitFileTask(String fileName, int numberOfPieces)
        {
            this.fileName = fileName;
            this.numberOfPieces = numberOfPieces;
        }
        public void splitFile(String fileName, int numberOfPieces)
        {
            try {
                BufferedInputStream input = new BufferedInputStream(
                        new FileInputStream(new File(fileName)));
    
                System.out.println("File size: " + input.available() + " bytes");
                long fileSize = input.available();
                int splitFileSize = (int) Math.ceil(1.0 * fileSize / numberOfPieces);
    
                for (int i = 1; i <= numberOfPieces; i++) {
                    BufferedOutputStream output = new BufferedOutputStream(
                            new FileOutputStream(new File(fileName + "." + i)));
                    int value;
                    int count = 0;
                    while (count++ < splitFileSize && (value = input.read()) != -1) {
                        output.write(value);
                    }
                    output.close();
                }
    
                input.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    
        @Override
        protected Integer doInBackground() throws Exception
        {
            return null;
        }
    }
    public class Application {
        static JProgressBar progressBar = new JProgressBar();
        public static void main(String[] args)
        {
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run()
                {
                    Exercise29_15 frame = new Exercise29_15();
    
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                    frame.pack();
    
                }
            });
        }
    }
    Last edited by aortell24; 07-20-2012 at 10:07 PM.

  6. #6
    aortell24 is offline Member
    Join Date
    Jun 2012
    Location
    St.louis, Missouri
    Posts
    49
    Rep Power
    0

    Default Re: Null pointer Exception

    Never mind I figured it out .I am quite sorry I will start looking over my code better before I bother you guys again.
    DarrylBurke and JosAH like this.

Similar Threads

  1. Null Pointer Exception HELP!?
    By 2wyked in forum New To Java
    Replies: 3
    Last Post: 04-04-2011, 01:41 AM
  2. Null Pointer exception (Again !!)
    By mobosecomin in forum New To Java
    Replies: 6
    Last Post: 03-29-2011, 05:04 PM
  3. Null pointer exception?
    By coffee in forum New To Java
    Replies: 4
    Last Post: 08-03-2009, 03:22 AM
  4. Help with null pointer exception
    By gammaman in forum New To Java
    Replies: 4
    Last Post: 07-14-2009, 12:23 AM
  5. getting a null pointer exception
    By Rjava in forum XML
    Replies: 4
    Last Post: 07-16-2008, 05:56 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •