线程运行方法中的Java-Null指针异常

时间:2017-03-19 21:10:38

标签: java multithreading networking

我的程序中出现空指针异常,我不知道为什么。我有一个JavaFX UI,它为客户端上传文件提供了一些约束信息,我将它们作为线程类中的Constraints类的实例。当我选择实例并调用方法来查看值时,我可以看到它们,但是在run方法中,当我尝试通过套接字发送它们时,我打印它们并且什么也看不见。这是我的UI代码:

package Server;

/**
* Created by Ahmed on 15/03/2017 at 10:26 PM.
*/
/**
* Sample Skeleton for 'serverHome.fxml' Controller Class
*/

import constraintPackage.Constraints;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.TextField;
import javafx.stage.DirectoryChooser;
import javafx.stage.Stage;

import java.io.File;
import java.io.Serializable;
import java.util.ArrayList;

public class serverHomeUI{
private WorkerThread workerThread = new WorkerThread();
private serverHome serverHome; //this is insignificant here
private boolean fileTypeButtonSelection = false;
private boolean numFileButtonSelection = false;
private boolean fileSizeButtonSelection = false;
private boolean delayButtonSelection = false;
private Constraints constraints = new Constraints();
void setServerHome(serverHome serverHome, String path){
    this.serverHome = serverHome;
    rootLocation.setText(path);
}

/*void setWorkerThread(WorkerThread workerThread){
    this.workerThread = workerThread;
}*/

@FXML // fx:id="rootLocation"
private TextField rootLocation; // Value injected by FXMLLoader

@FXML // fx:id="browseButton"
private Button browseButton; // Value injected by FXMLLoader

@FXML // fx:id="cFile"
private CheckBox cFile; // Value injected by FXMLLoader

@FXML // fx:id="cppFile"
private CheckBox cppFile; // Value injected by FXMLLoader

@FXML // fx:id="javaFile"
private CheckBox javaFile; // Value injected by FXMLLoader

@FXML // fx:id="pyFile"
private CheckBox pyFile; // Value injected by FXMLLoader

@FXML // fx:id="phpFile"
private CheckBox phpFile; // Value injected by FXMLLoader

@FXML // fx:id="fileTypeButton"
private Button fileTypeButton; // Value injected by FXMLLoader

@FXML // fx:id="numFile"
private TextField numFile; // Value injected by FXMLLoader

@FXML // fx:id="numFileButton"
private Button numFileButton; // Value injected by FXMLLoader

@FXML // fx:id="maxFileSize"
private TextField maxFileSize; // Value injected by FXMLLoader

@FXML // fx:id="maxFileSizeButton"
private Button maxFileSizeButton; // Value injected by FXMLLoader

@FXML // fx:id="stdID"
private TextField stdID; // Value injected by FXMLLoader

@FXML // fx:id="stdIDButton"
private Button stdIDButton; // Value injected by FXMLLoader

@FXML // fx:id="delay"
private TextField delay; // Value injected by FXMLLoader

@FXML // fx:id="delayButton"
private Button delayButton; // Value injected by FXMLLoader

@FXML
void browseButtonFired(ActionEvent event) {
    DirectoryChooser directoryChooser = new DirectoryChooser();
    directoryChooser.setTitle("Choose Root Directory");
    Stage stage = serverHome.stage;
    File selectedDirectory = directoryChooser.showDialog(stage);
    if(selectedDirectory != null){
        rootLocation.setText(selectedDirectory.getAbsolutePath());
        serverHome.setRoot(rootLocation.getText());
    }
}

@FXML
void delayButtonFired(ActionEvent event) {
    if(delay.getText() != null){
        delayButtonSelection = true;
        constraints.setDelayTime(Integer.parseInt(delay.getText()));
        if(numFileButtonSelection && fileTypeButtonSelection && fileSizeButtonSelection && delayButtonSelection){
            workerThread.sendConstraints(constraints);
        }
    }
}

@FXML
void fileTypeButtonFired(ActionEvent event) {
    ArrayList<String> ticked = new ArrayList<>();
    int i = 0;
    if(cFile.isSelected() || cppFile.isSelected() || javaFile.isSelected() || pyFile.isSelected() || phpFile.isSelected()){
        if(cFile.isSelected()){
            ticked.add("c");
        }
        else if(!cFile.isSelected()){
            ticked.remove("c");
        }
        else if(cppFile.isSelected()){
            ticked.add("cpp");
        }

        else if(!cppFile.isSelected()){
            ticked.remove("cpp");
        }

        else if(javaFile.isSelected()){
            ticked.add("java");
        }

        else if(!javaFile.isSelected()){
            ticked.remove("java");
        }

        else if(pyFile.isSelected()){
            ticked.add("py");
        }

        else if(!pyFile.isSelected()){
            ticked.remove("py");
        }

        else if(phpFile.isSelected()){
            ticked.add("php");
        }

        else if(!phpFile.isSelected()){
            ticked.remove("php");
        }

        fileTypeButtonSelection = true;
        constraints.setFileTypes(ticked);
        if(numFileButtonSelection && fileTypeButtonSelection && fileSizeButtonSelection && delayButtonSelection){
            workerThread.sendConstraints(constraints);
        }
    }
}

@FXML
void maxFileSizeButtonFired(ActionEvent event) {
    if(maxFileSize.getText() != null){
        fileSizeButtonSelection = true;
        constraints.setMaxFileSize(Float.parseFloat(maxFileSize.getText()));
        if(numFileButtonSelection && fileTypeButtonSelection && fileSizeButtonSelection && delayButtonSelection){
            workerThread.sendConstraints(constraints);
        }
    }
}

@FXML
void numFileButtonFired(ActionEvent event) {
    if(numFile.getText() != null){
        numFileButtonSelection = true;
        constraints.setMaxNumFile(Integer.parseInt(numFile.getText()));
        if(numFileButtonSelection && fileTypeButtonSelection && fileSizeButtonSelection && delayButtonSelection){
            workerThread.sendConstraints(constraints);
        }
    }
}

@FXML
void stdIDButtonFired(ActionEvent event) {

}

}

这是我的线程代码

class WorkerThread implements Runnable
{
private Hashtable<String, Integer> hashTable = new Hashtable<>();
private Socket socket;
private InputStream inputStream = null;
private OutputStream outputStream = null;
private InputStreamReader inputStreamReader = null;
private BufferedReader bufferedReader = null;
private PrintWriter printWriter = null;
private ObjectOutputStream objectOutputStream = null;
private ObjectInputStream objectInputStream = null;
private constraintCreated = false;
private Constraints constraints1;
private int id = 0;

void sendConstraints(Constraints constraints){
    System.out.println("hello bai, constraint false");
    constraints1 = constraints;
    constraintCreated = true;
    System.out.println("constraint true");
    System.out.println(this.constraints1.getDelayTime());//these two lines give
    System.out.println(this.constraints1.getFileNum());//proper output
}

public WorkerThread(){

}
public WorkerThread(Socket s)
{
    this.socket = s;

    try {
        inputStream = socket.getInputStream();
        outputStream = socket.getOutputStream();
        objectInputStream = new ObjectInputStream(inputStream);
        objectOutputStream = new ObjectOutputStream(outputStream);
        inputStreamReader= new InputStreamReader(inputStream);
        bufferedReader = new BufferedReader(inputStreamReader);
        printWriter = new PrintWriter(outputStream);

    } catch (IOException e) {
        e.printStackTrace();
    }

    String clientIP = s.getInetAddress().toString();

    String studentID = null;
    int stdID = 0;

    try {
        studentID = bufferedReader.readLine();
        stdID = Integer.parseInt(studentID);
    } catch (IOException e) {
        e.printStackTrace();
    }

    hashTable.put(clientIP, stdID);
    System.out.println(root1); //root1="G:\Akib Ahmed\Pdf\3-2\(CSE 321, 322) Computer Networks\Lab\Offline 1 (Code Repository)\src\Server\Root"
    File file = new File(root1);
    if(!file.exists()){
        boolean a = file.mkdir();
        file = new File(root1 + "\\"+stdID);
        if(!file.exists()) {
            boolean b = file.mkdir();
        }
    }

    this.id = id;
}

public void run() {
    Constraints constraints = new Constraints();

    if (printWriter != null) {
        printWriter.print("Your id is: " + this.id);
        printWriter.println();
        printWriter.flush();
    }

    String str = null;
    while (true) {
        try {
            System.out.println("inside while loops");
            str = bufferedReader.readLine();
            System.out.println(str);
            if (str != null) {
                if(str.equals("Constraints")){ //reading str by bufferedReader
                    System.out.println("going to send constraints");


                    //while(!constraintCreated) {if(constraints1 == null) System.out.println("something");} //this went on an inifinite loop
                    if(constraintCreated) System.out.println("really!"); //constraintCreated is false here, although I have assigned it true earlier
                    System.out.println(constraints1.getDelayTime()); //null pointer exception and compilation never goes further
                    ArrayList<String> filetyp = constraints1.getFileTypes();
                    objectOutputStream.writeObject(filetyp);
                    objectOutputStream.flush();
                    printWriter.write(String.valueOf(constraints1.getFileSize()));
                    printWriter.println();
                    printWriter.flush();
                    printWriter.write(String.valueOf(constraints1.getFileNum()));
                    printWriter.println();
                    printWriter.flush();
                    printWriter.write(String.valueOf(constraints1.getDelayTime()));
                    printWriter.println();
                    printWriter.flush();
                    System.out.println(constraints1.getDelayTime());
                    System.out.println("sent Constraints");
                }
        }
        else{
                System.out.println("here????");
            }
        } catch (Exception e) {
            e.printStackTrace();
            System.err.println("Problem in communicating with the client [" + id + "]. Terminating worker thread.");
            break;
        }
    }

    ShowOff.workerThreadCount--;
    System.out.println("Client [" + id + "] is now terminating. No. of worker threads = "
            + ShowOff.workerThreadCount);
}
}

0 个答案:

没有答案