Hi there I have been having some thread synchronization issues lately, I am trying to write a client/server application which is supposed to transfer files from one computer to another using a given socket! The thing is I keep getting a Null pointer exception in oos.writeInt(selectedFiles.length) (client side), I don't really understand why that happens since I am initializing oos in run() and then making sure it doesn't go any further before knowing the number of files to transfer to the other side..... So I figured it must be a synchronization issue, I just don't know how to solve this....

Client side code:

package fileUpload;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.SocketException;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;

public class FileUpload_Client extends JFrame implements ActionListener, Runnable{

/**
 * 
 */
private static final long serialVersionUID = 1L;
private JPanel contentPane;
private JTextField currentUpload;
private JProgressBar progressBar;
private JTextArea uploads;
private File[] selectedFiles;
private JButton upload;
private boolean selectionHasNotYetBeenMade=true;
private JLabel lblNewLabel;
private JLabel lblCurrentFilesProgress;
private Socket channel;
private ObjectInputStream ois;
private ObjectOutputStream oos;

/**
 * Create the frame.
 */
public FileUpload_Client(Socket channel) {

    this.channel=channel;

    setResizable(false);
    setTitle("Remote Administrator");
    setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    setBounds(100, 100, 450, 300);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);

    JLabel lblPleaseUploadyourFiles = new JLabel("Please upload your files by clicking the button here:");
    lblPleaseUploadyourFiles.setFont(new Font("Dialog", Font.BOLD, 10));
    lblPleaseUploadyourFiles.setBounds(12, 12, 306, 15);
    contentPane.add(lblPleaseUploadyourFiles);

    upload = new JButton("Upload");
    upload.addActionListener(this);
    upload.setBounds(317, 9, 105, 19);
    contentPane.add(upload);

    JLabel lblFileCurrentlyBeing = new JLabel("File currently being uploaded:");
    lblFileCurrentlyBeing.setFont(new Font("Dialog", Font.BOLD, 10));
    lblFileCurrentlyBeing.setBounds(12, 32, 196, 15);
    contentPane.add(lblFileCurrentlyBeing);

    currentUpload = new JTextField();
    currentUpload.setEnabled(false);
    currentUpload.setHorizontalAlignment(SwingConstants.CENTER);
    currentUpload.setOpaque(false);
    currentUpload.setEditable(false);
    currentUpload.setBounds(226, 32, 196, 14);
    contentPane.add(currentUpload);
    currentUpload.setColumns(10);

    progressBar = new JProgressBar();
    progressBar.setEnabled(false);
    progressBar.setBounds(226, 59, 196, 15);
    contentPane.add(progressBar);

    uploads = new JTextArea();
    uploads.setFont(new Font("Dialog", Font.PLAIN, 10));
    uploads.setEnabled(false);
    uploads.setEditable(false);
    uploads.setBounds(12, 111, 410, 140);
    contentPane.add(uploads);

    lblNewLabel = new JLabel("List of uploaded files:");
    lblNewLabel.setFont(new Font("Dialog", Font.BOLD, 10));
    lblNewLabel.setBounds(12, 84, 161, 15);
    contentPane.add(lblNewLabel);

    lblCurrentFilesProgress = new JLabel("Current file's progress bar:");
    lblCurrentFilesProgress.setFont(new Font("Dialog", Font.BOLD, 10));
    lblCurrentFilesProgress.setBounds(12, 59, 196, 15);
    contentPane.add(lblCurrentFilesProgress);

    setVisible(true);

}



@Override
public void run() {

    try {
        ois = new ObjectInputStream(channel.getInputStream());
    } catch (IOException e2) {
        // TODO Auto-generated catch block
        e2.printStackTrace();
    }

    try {
        oos = new ObjectOutputStream(channel.getOutputStream());
    } catch (IOException e2) {
        // TODO Auto-generated catch block
        e2.printStackTrace();
    }

    System.out.println("ois and oos initialized");


    while(selectionHasNotYetBeenMade){  }


    for(int i=0; i<selectedFiles.length; i++){

    //Sending over file's name
    try {
        oos.writeObject((String)selectedFiles[i].getName());
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    currentUpload.setText(selectedFiles[i].getName());

    try {
        oos.writeLong(selectedFiles[i].length());
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    //Sending over its data
    FileInputStream in = null;
    try {
        in = new FileInputStream(selectedFiles[i]);
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    byte[] buff = null;
    try {
        buff = new byte[channel.getSendBufferSize()];
    } catch (SocketException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    int bytesReadNow = 0;
    int bytesReadUntilNow = 0;

    try {
        while((bytesReadNow = in.read(buff))>0)
        {
            oos.write(buff,0,bytesReadNow);
            oos.flush();
            bytesReadUntilNow += bytesReadNow;
            progressBar.setValue((int)(bytesReadUntilNow/selectedFiles[i].length()));
        }
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    progressBar.setValue(100);

    try {
        in.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    currentUpload.setText("");

    uploads.append(selectedFiles[i].getName()+"\n");

    }

    try {
        channel.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

@Override
public void actionPerformed(ActionEvent e) {

    JFileChooser files_chooser = new JFileChooser();
    files_chooser.setMultiSelectionEnabled(true);
    files_chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);

    if(files_chooser.showDialog(this, "Upload")== JFileChooser.APPROVE_OPTION){
        selectedFiles=files_chooser.getSelectedFiles();
        uploads.setEnabled(true);
        currentUpload.setEnabled(true);
        try {
            oos.writeInt(selectedFiles.length);
            oos.flush();
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        selectionHasNotYetBeenMade=false;
    }

}
}

Server side code:

package fileUpload;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Font;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.SocketException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;

public class FileUpload_Server extends JFrame implements Runnable{

private JPanel contentPane;
private JTextField currentDownload;
private JProgressBar progressBar;
private JTextArea uploads;
private ObjectInputStream ois;
private ObjectOutputStream oos;
private Socket channel;

/**
 * Create the frame.
 */
public FileUpload_Server(Socket channel) {

    this.channel=channel;

    setResizable(false);
    setTitle("Remote Administrator");
    setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    setBounds(100, 100, 450, 300);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);

    JLabel lblFileCurrentlyBeing = new JLabel("File currently being downloaded:");
    lblFileCurrentlyBeing.setFont(new Font("Dialog", Font.BOLD, 10));
    lblFileCurrentlyBeing.setBounds(12, 13, 196, 19);
    contentPane.add(lblFileCurrentlyBeing);

    currentDownload = new JTextField();
    currentDownload.setEnabled(false);
    currentDownload.setHorizontalAlignment(SwingConstants.CENTER);
    currentDownload.setOpaque(false);
    currentDownload.setEditable(false);
    currentDownload.setBounds(226, 12, 196, 19);
    contentPane.add(currentDownload);
    currentDownload.setColumns(10);

    progressBar = new JProgressBar();
    progressBar.setEnabled(false);
    progressBar.setBounds(12, 44, 410, 14);
    contentPane.add(progressBar);

    uploads = new JTextArea();
    uploads.setEnabled(false);
    uploads.setEditable(false);
    uploads.setBounds(12, 96, 410, 155);
    contentPane.add(uploads);

    JLabel lblListOfDownloaded = new JLabel("List of downloaded files:");
    lblListOfDownloaded.setBounds(12, 70, 196, 15);
    contentPane.add(lblListOfDownloaded);

    setVisible(true);

}

@Override
public void run() {

try {
        ois = new ObjectInputStream(channel.getInputStream());
    } catch (IOException e4) {
        // TODO Auto-generated catch block
        e4.printStackTrace();
    }

    try {
        oos = new ObjectOutputStream(channel.getOutputStream());
    } catch (IOException e4) {
        // TODO Auto-generated catch block
        e4.printStackTrace();
    }

    System.out.println("ois and oos initialized");


    int howMany = 0;
    try {
        howMany = ois.readInt();
    } catch (IOException e3) {
        // TODO Auto-generated catch block
        e3.printStackTrace();
    }

    File downloadDirectory = new File((new File(".")).getParentFile(),"Uploads");

    downloadDirectory.mkdir();

    for(int i=0; i<howMany; i++){

    //Creating new file

    String currentDownloadFileName = null;
    try {
        currentDownloadFileName = (String) ois.readObject();
    } catch (IOException e3) {
        // TODO Auto-generated catch block
        e3.printStackTrace();
    } catch (ClassNotFoundException e3) {
        // TODO Auto-generated catch block
        e3.printStackTrace();
    }

    File receivedFile = new File(downloadDirectory,currentDownloadFileName);

    currentDownload.setText(currentDownloadFileName);

    long length = 0;
    try {
        length = ois.readLong();
    } catch (IOException e2) {
        // TODO Auto-generated catch block
        e2.printStackTrace();
    }

    //Receiving its data
    FileOutputStream wr = null;
    try {
        wr = new FileOutputStream(receivedFile);
    } catch (FileNotFoundException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    byte[] outBuffer = null;
    try {
        outBuffer = new byte[channel.getReceiveBufferSize()];
    } catch (SocketException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    int bytesReceivedNow = 0;
    int bytesReceivedUntilNow = 0;

    try {
        while((bytesReceivedNow = ois.read(outBuffer))>0)
        {
            wr.write(outBuffer,0,bytesReceivedNow);
            bytesReceivedUntilNow += bytesReceivedNow;
            progressBar.setValue((int)(bytesReceivedUntilNow/length));
        }
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    progressBar.setValue(100);

    try {
        wr.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    currentDownload.setText("");

    uploads.append(receivedFile.getName()+"\n");

    }

    try {
        channel.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}
}

The program just doesn't seem to get to the run() method...

link|improve this question
This should probably be migrated to stackoverflow. – John H Dec 1 '11 at 14:45
As a note you should not re-post this question on Stackoverflow, it will be migrated for you soon. – Mokubai Dec 1 '11 at 14:48
since I'm sorta new here, could someone please explain the difference between StackOverflow and SuperUser? – user106168 Dec 1 '11 at 15:05
Please thoroughly read our FAQ and you will find the answer, especially in "What kind of questions should I not ask here?". Then, read the FAQ of Stack Overflow. – slhck Dec 1 '11 at 15:20
1  
Super User is meant for questions on computer hardware & software, Stack Overflow is meant for Programming questions. Please review these posts on Stack Overflow. If you're still having problems, post in Stack Overflow with details like an error log, stace trace etc – Sathya Dec 1 '11 at 15:46
feedback

closed as off topic by ChrisF, Mokubai, slhck, sblair, Sathya Dec 1 '11 at 15:33

Questions on Super User are expected to generally relate to computer software or computer hardware, within the scope defined in the faq.

Browse other questions tagged or ask your own question.